我正在使用 CreateGraphics 函数打印我的活动表单(formMain),如下代码
环境:经典模式下的windows 7专业版,VisuaStudio 2008
[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)
{
if (MessageBox.Show("test", "test", MessageBoxButtons.YesNo) == DialogResult.Yes)
{
CaptureScreen();
printDocument1.Print();
}
}
case1 : MessageBox 默认显示位置时,formMain 可以清晰打印
情况2: 但是,如果使用鼠标将messageBox移出默认位置,则打印结果是脏的。它现在在 mainForm 中包含 messageBox(图形)。