分类:
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(); }
本示例需要:
该示例代码替换现有的事件处理程序。单击 printButton
时,就会打印该窗体。
以下情况可能会导致异常:
为了运行此示例,您必须具有执行非托管代码和访问打印机的权限。