【发布时间】:2016-03-13 01:56:11
【问题描述】:
我正在尝试自动打印一系列 Windows 窗体。我不需要给他们看。我在互联网上找到的代码示例仅在我使用show() 显示表单时才有效!我需要用数据初始化表单并将其发送到打印机这是我正在使用的代码:
public partial class Form2_withoutShow : Form{
PrintDocument PrintDoc;
Bitmap memImage;
public Form2_withoutShow (Data data)
{
InitializeComponent();
/*
Initialize Data (texboxes, charts ect.) here
*/
this.PrintDoc = new PrintDocument();
this.PrintDoc.PrintPage += PrintDoc_PrintPage;
this.PrintDoc.DefaultPageSettings.Landscape = true;
}
public void Print()
{
this.PrintDoc.Print();
}
void PrintDoc_PrintPage(object sender, PrintPageEventArgs e)
{
int x = SystemInformation.WorkingArea.X;
int y = SystemInformation.WorkingArea.Y;
int width = this.Width;
int height = this.Height;
Rectangle bounds = new Rectangle(x, y, width, height);
Bitmap img = new Bitmap(width, height);
this.DrawToBitmap(img, bounds);
Point p = new Point(10, 10);
e.Graphics.DrawImage(img, p);
}
private void Form2_withoutShow_Load(object sender, EventArgs e)
{
// remove TITLEBar
this.ControlBox = false;
this.Text = String.Empty;
}
}
我在for循环中从另一个类调用Print()方法,并通过构造函数传递要初始化的数据。
MSDN example 捕获屏幕上应显示表单的部分。这对我不起作用。如果我不调用show(),我现在使用的方法只会产生空窗口的打印。如何在不调用show() 方法的情况下将数据放入表单?像在显示窗口时最小化窗口这样的方法也不起作用,因为这也是打印结果:最小化的窗口。
【问题讨论】:
-
你解决了吗?
-
不,我不是,我的目标是打印协议,我认为这可以使用填充数据的 gui 来完成。我最终使用 MigraDocs 创建了一个 PDF 并将其发送到打印机......
-
@ErAcube 有一个答案。你检查了吗?
-
是的,感谢您的指出。我刚刚注意到了。
标签: c# .net winforms data-binding printing