我对打印一无所知,希望大虾能够给源码以及必要的注释!!谢谢 各位了!
帮帮忙嘛!

解决方案 »

  1.   

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
       Graphics mygraphics = this.CreateGraphics();
       Size s = this.Size;
       memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
       Graphics memoryGraphics = Graphics.FromImage(memoryImage);
       IntPtr dc1 = mygraphics.GetHdc();
       IntPtr dc2 = memoryGraphics.GetHdc();
       BitBlt(dc2, 0, 0, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 0, 0, 13369376);
       mygraphics.ReleaseHdc(dc1);
       memoryGraphics.ReleaseHdc(dc2);
    }
    private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
       e.Graphics.DrawImage(memoryImage, 0, 0);
    }
    private void printButton_Click(System.Object sender, System.EventArgs e)
    {
       CaptureScreen();
       printDocument1.Print();
      

  2.   

    [System.Runtime.InteropServices.DllImport("gdi32.dll")]
    public static extern long BitBlt (IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, int dwRop);
    private Bitmap memoryImage;
    private void CaptureScreen()
    {
    Graphics mygraphics = this.CreateGraphics();
    Size s = this.Size;
    memoryImage = new Bitmap(s.Width, s.Height, mygraphics);
    Graphics memoryGraphics = Graphics.FromImage(memoryImage);
    IntPtr dc1 = mygraphics.GetHdc();
    IntPtr dc2 = memoryGraphics.GetHdc();
    //BitBlt(dc2, 20, 55, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 20, 55, 13369376);
    BitBlt(dc2, 20, 70, 560, 430, dc1, 20, 70, 13369376);
    //BitBlt(dc2, 20, 70, this.ClientRectangle.Width, this.ClientRectangle.Height, dc1, 20, 70, 13369376);
    mygraphics.ReleaseHdc(dc1);
    memoryGraphics.ReleaseHdc(dc2);
    }
    private void printDocument1_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    e.Graphics.DrawImage(memoryImage, 0, 0);
    }
    private void button2_Click(System.Object sender, System.EventArgs e)
    {
    CaptureScreen();
    printPreviewDialog1.Document=printDocument1;
    printPreviewDialog1.Show();
    }
      

  3.   

    页面设置:
    private void button1_Click(object sender, System.EventArgs e)
    {
    try
    {
    pageSetupDialog1.Document=printDocument1;
    pageSetupDialog1.ShowDialog();
    }
    catch
    {
    MessageBox.Show("没有安装打印机");
    }
    }打印预览:
    private void button2_Click(object sender, System.EventArgs e)
    {
    if(mPrintFont==null)
    {
    mPrintFont=new Font("新宋体",10);
    }
    mPrintRecordCount=0;
    mCurrentPageNo=0;
    mTotalPageCount=0;
    printPreviewDialog1.Document=printDocument1;
    printPreviewDialog1.ShowDialog();
    }
    设置字体:
    private void button3_Click(object sender, System.EventArgs e)
    {
    fontDialog1.ShowDialog();
    mPrintFont=fontDialog1.Font;

    }打印:
    private void button4_Click(object sender, System.EventArgs e)
    {
    printDialog1.Document=printDocument1;
    if(mPrintFont==null)
    {
    mPrintFont=new Font("新宋体",10);
    }
    mPrintRecordCount=0;
    mCurrentPageNo=0;
    mTotalPageCount=0;
    DialogResult result=printDialog1.ShowDialog();
    if(result==DialogResult.OK)
    {
    printDocument1.Print();
    }            
    } private float PrintPageHeader(System.Drawing.Printing.PrintPageEventArgs e)
    {
    float yPos=e.MarginBounds.Top;
    string line=null;
    Font printTitleFont=new Font("宋体",18,FontStyle.Bold);
    Font printHeaderFont=new Font("宋体",18,FontStyle.Bold);
    StringFormat sfCenter=new StringFormat();
    sfCenter.Alignment=StringAlignment.Center;
    StringFormat sfRight=new StringFormat();
    sfRight.Alignment=StringAlignment.Far;
    StringFormat sfLeft=new StringFormat();
    sfLeft.Alignment=StringAlignment.Near;
    line="教师管理系统";
    e.Graphics.DrawString(line,printTitleFont,Brushes.Black,new RectangleF(e.MarginBounds.Left,e.MarginBounds.Top,e.MarginBounds.Width,e.MarginBounds.Height),sfCenter);
    yPos+=printTitleFont.GetHeight(e.Graphics);
    line="教师管理报表";
    e.Graphics.DrawString(line,printTitleFont,Brushes.Black,new RectangleF(e.MarginBounds.Left,e.MarginBounds.Top+printTitleFont.GetHeight(e.Graphics),e.MarginBounds.Width,e.MarginBounds.Height),sfCenter);
    e.Graphics.DrawString("打印日期:"+DateTime.Now.ToString("yyyy-MM-dd",null),mPrintFont,Brushes.Black,new RectangleF(e.MarginBounds.Left,e.MarginBounds.Top+printTitleFont.GetHeight(e.Graphics),e.MarginBounds.Width,e.MarginBounds.Height),sfLeft);
    yPos+=printHeaderFont.GetHeight(e.Graphics);
    try
    {
    //第一行
    e.Graphics.DrawLine(new Pen(Color.Black,4),e.MarginBounds.Left,yPos,e.MarginBounds.Right,yPos);
    yPos+=20;
    //列头
    float xPos=e.MarginBounds.Left;
    e.Graphics.DrawString("教师号",mPrintFont,Brushes.Black,new RectangleF(xPos,yPos,e.Graphics.MeasureString("教师号",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    xPos+=e.Graphics.MeasureString("教师号",mPrintFont).Width;
    e.Graphics.DrawString("姓名",mPrintFont,Brushes.Black,new RectangleF(xPos+30,yPos,e.Graphics.MeasureString("姓名",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    xPos+=e.Graphics.MeasureString("性别",mPrintFont).Width;
    e.Graphics.DrawString("性别",mPrintFont,Brushes.Black,new RectangleF(xPos+60,yPos,e.Graphics.MeasureString("性别",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    xPos+=e.Graphics.MeasureString("年龄",mPrintFont).Width;
    e.Graphics.DrawString("年龄",mPrintFont,Brushes.Black,new RectangleF(xPos+90,yPos,e.Graphics.MeasureString("年龄",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    yPos+=mPrintFont.GetHeight(e.Graphics);
    //第二行
    e.Graphics.DrawLine(new Pen(Color.Black,4),e.MarginBounds.Left,yPos,e.MarginBounds.Right,yPos);
    yPos+=20;
    //打印页数
    float lpp=(e.MarginBounds.Height-yPos)/mPrintFont.GetHeight(e.Graphics);
    mTotalPageCount=(ds1.Tables["teacher1"].Rows.Count/Convert.ToInt32(lpp))+1;
    e.Graphics.DrawString("页数:"+mCurrentPageNo.ToString()+"/"+mTotalPageCount.ToString(),mPrintFont,Brushes.Black,new RectangleF(e.MarginBounds.Left,e.MarginBounds.Top,e.MarginBounds.Width,e.MarginBounds.Height),sfRight);
    return yPos;
    }
    catch
    {
    MessageBox.Show("没有输入查询条件,请从新输入!");
    return 0;
    }
    }
    private void printDocument1_PrintPage_1(object sender, System.Drawing.Printing.PrintPageEventArgs e)
    {
    string line=null;
    line="教师管理系统";
    Font printTitleFont=new Font("宋体",18,FontStyle.Bold);
    StringFormat sfCenter=new StringFormat();
    sfCenter.Alignment=StringAlignment.Center;
    e.Graphics.DrawString(line,printTitleFont,Brushes.Black,new RectangleF(e.MarginBounds.Left,e.MarginBounds.Top,e.MarginBounds.Width,e.MarginBounds.Height),sfCenter);
    float lpp=0;
    float yPos=0;
    float leftMargin=e.MarginBounds.Left;
    float topMargin=e.MarginBounds.Top;
    float pageHeaderHiegh=0;
    int count=0;
    StringFormat sfLeft=new StringFormat();
    sfLeft.Alignment=StringAlignment.Near;
    mCurrentPageNo++;                                              
    pageHeaderHiegh=PrintPageHeader(e);
    lpp=(e.MarginBounds.Height-pageHeaderHiegh)/mPrintFont.GetHeight(e.Graphics);
    try
    {
    while(count<lpp&&(mPrintRecordCount<ds1.Tables["teacher1"].Rows.Count))
    {
    float xPos=e.MarginBounds.Left;
    yPos=pageHeaderHiegh+(count*mPrintFont.GetHeight(e.Graphics));
    e.Graphics.DrawString(ds1.Tables["teacher1"].Rows[mPrintRecordCount]["教师号"].ToString(),mPrintFont,Brushes.Black,new RectangleF(xPos,yPos,e.Graphics.MeasureString("教师号",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    xPos+=e.Graphics.MeasureString("教师号",mPrintFont).Width;
    e.Graphics.DrawString(ds1.Tables["teacher1"].Rows[mPrintRecordCount]["姓名"].ToString(),mPrintFont,Brushes.Black,new RectangleF(xPos+30,yPos,e.Graphics.MeasureString("姓名",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    xPos+=e.Graphics.MeasureString("姓名",mPrintFont).Width;
    e.Graphics.DrawString(ds1.Tables["teacher1"].Rows[mPrintRecordCount]["性别"].ToString(),mPrintFont,Brushes.Black,new RectangleF(xPos+60,yPos,e.Graphics.MeasureString("性别",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    xPos+=e.Graphics.MeasureString("性别",mPrintFont).Width;
    e.Graphics.DrawString(ds1.Tables["teacher1"].Rows[mPrintRecordCount]["年龄"].ToString(),mPrintFont,Brushes.Black,new RectangleF(xPos+90,yPos,e.Graphics.MeasureString("年龄",mPrintFont).Width,mPrintFont.GetHeight(e.Graphics)),sfLeft);
    count++;
    mPrintRecordCount++;
    if(mPrintRecordCount<ds1.Tables["teacher1"].Rows.Count)
    e.HasMorePages=true;
    else
    e.HasMorePages=false;   
    }
    }
    catch
    {
    MessageBox.Show("没有输入查询条件,请从新输入!");
    }
    }