我刚刚创建了一个用于请求零件的数据库应用程序。
它有几个表单,一个供请求者使用,一个供主管批准,一个用于购买批准,另一个供办事员用来知道要订购什么。
现在我是无纸化的铁杆粉丝,但我的雇主真的很喜欢他们的论文。有没有一个简单的方法来WYSIWYG复制我的Windows窗体到纸上?
我还应该补充说,我只能使用2.0 .Net框架
谢谢
发布于 2009-03-11 17:54:29
这是一个可以做你想做的事情的code sample from MSDN:
[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();
}有一些警告-这里没有例外检查,而且您需要在完全信任下运行才能使用非托管Windows但这可能是打印BitBlt窗体表单的最简单方法,只要它显示在屏幕上。
发布于 2009-03-11 18:05:17
这里有一个快速的方法。您可以清理代码,使其符合您的需求:
public static class FormExtensions
{
public static void PrintForm(this Form f)
{
PrintDocument doc = new PrintDocument();
doc.PrintPage += (o, e) =>
{
Bitmap image = new Bitmap(f.ClientRectangle.Width, f.ClientRectangle.Height);
f.DrawToBitmap(image, f.ClientRectangle);
e.Graphics.DrawImage(image, e.PageBounds);
};
doc.Print();
}
}这会将表单拉伸到页面的大小。如果需要,您可以调整DrawImage方法调用的第二个参数,将其绘制到其他位置。
https://stackoverflow.com/questions/635627
复制相似问题