原创  C#中打印控件 收藏

  [DllImport("gdi32.dll")]
  public static extern long BitBlt(
   IntPtr hdcDest, int nXDest, int nYDest, int nWidth, int nHeight, IntPtr hdcSrc, int nXSrc, int nYSrc, System.Int32 dwRop);
 
  Bitmap memoryImage;
  private void sb_print_Click(object sender, System.EventArgs e)
  {
   System.Drawing.Printing.PrintDocument printDocument1 = new PrintDocument();
   this.printDocument1.PrintPage += new System.Drawing.Printing.PrintPageEventHandler(this.printDocument1_PrintPage);
   //  更改即将打印控件的外观等
   ...
   pc_main.Refresh();

   // 打印内容写入Bitmap
   Graphics mygraphics = pc_main.CreateGraphics();
   Size s = pc_main.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, pc_main.ClientRectangle.Width, pc_main.ClientRectangle.Height, dc1, 0, 0, 13369376);
   mygraphics.ReleaseHdc(dc1);
   memoryGraphics.ReleaseHdc(dc2);

   // 还原打印控件外观
   ...
   pc_main.Refresh();


   printDocument1.Print();
  }

  private void printDocument1_PrintPage(object sender, System.Drawing.Printing.PrintPageEventArgs e)
  {
   e.Graphics.DrawImage(memoryImage, 0, 0);

  }

发表于 @ 2005年08月10日 18:50:00 | 评论( loading... ) | 编辑| 举报| 收藏

旧一篇:读取foxpro格式的dbf文件 | 新一篇:程序设置打印机配置

  • 发表评论
  • 评论内容:
  •  
Copyright © nickycheng
Powered by CSDN Blog