【发布时间】:2013-08-18 04:30:23
【问题描述】:
我对 C# 还很陌生,但我终于启动并运行了我的第一个程序,我需要将其打印出来。它是一个窗口表单,在不同的选项卡控件上包含信息和计算,有点像 Excel。使用 copyfromscreen 方法可以正常打印当前正在查看的页面,但我无法正确打印其他页面。我想一次打印大约 20 个标签。我找到了一种将控件内容打印到文本文件中的方法,但我更希望能够打印表单的样子。谢谢。
Bitmap memoryImage;
Bitmap memoryImage2;
private void CaptureScreen()
{
Graphics myGraphics = this.CreateGraphics();
Size s = tabControlMain.Size;
s.Width = s.Width + 20;
memoryImage = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics = Graphics.FromImage(memoryImage);
memoryGraphics.CopyFromScreen(this.Location.X+15, this.Location.Y+80, 0, 0, s);
tabControlMain.SelectedIndex = 1;
memoryImage2 = new Bitmap(s.Width, s.Height, myGraphics);
Graphics memoryGraphics2 = Graphics.FromImage(memoryImage2);
memoryGraphics2.CopyFromScreen(this.Location.X + 15, this.Location.Y + 80, 0, 0, s);
}
private void printDocumentReal_PrintPage(System.Object sender, System.Drawing.Printing.PrintPageEventArgs e)
{
e.Graphics.DrawImage(memoryImage, 0, 0);
e.Graphics.DrawImage(memoryImage2, 0, 550);
}
private void printToolStripButton_Click(object sender, EventArgs e)
{
CaptureScreen();
printDocumentReal.Print();
}
【问题讨论】:
-
只捕获
TabPage好吗?或者你需要捕获整个TabControl? -
是的,我只需要能够打印 20 个左右的 TabPages 中的每一个
-
使用 PrintPageEventArgs.HasMorePages 属性来打印多个页面已在有关 PrintDocument 的 MSDN 库文章中得到了很好的介绍。只需计算页面,选择正确的图形来绘制。您还需要 BeginPrint 事件来重置计数器。请注意屏幕截图很难看,打印机的分辨率远高于屏幕的分辨率,因此每个屏幕像素都会在纸上变成一个 6x6 的斑点。