Chinaunix首页 | 论坛 | 博客
  • 博客访问: 362411
  • 博文数量: 94
  • 博客积分: 2500
  • 博客等级: 少校
  • 技术积分: 823
  • 用 户 组: 普通用户
  • 注册时间: 2006-05-04 16:49
文章分类

全部博文(94)

文章存档

2015年(1)

2011年(1)

2010年(3)

2008年(8)

2007年(55)

2006年(26)

我的朋友

分类:

2007-11-12 22:05:07

此示例说明如何打印当前窗体的副本。

示例

[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();
}

编译代码

本示例需要:

  • 名为 printDocument1 且包含 PrintPage 事件处理程序的 PrintDocument 组件。
  • 名为 printButton 且包含 Click 事件处理程序的 Button 对象。

该示例代码替换现有的事件处理程序。单击 printButton 时,就会打印该窗体。

可靠编程

以下情况可能会导致异常:

  • 您没有访问该打印机的权限。
  • 您没有使用非托管代码的权限。
  • 没有安装打印机。

安全性

为了运行此示例,您必须具有执行非托管代码和访问打印机的权限。

阅读(732) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~