在 PrintPreviewControl 上显示 PrintDocument 的所有页面
我在 PrintDocument 中创建多个页面并在 PrintPreviewControl 中显示它们时遇到问题。我可以很容易地创建一个页面,但将多个页面串在一起却很难。
我最终将使用 GDI 绘制几页内容,但我无法让这样的东西按预期工作。
private PrintDocument doc = new PrintDocument();
private string[] printMe = new string[]{ "page1", "page2", "page3" );
private int pageCount = 0;
private void FormLoad(object sender, EventArgs e)
{
doc.PrintPage += new PrintPageEventHandler(PrintPage);
PrintPreviewControl.Document = doc;
}
private void doc_BeginPrint(object sender, PrintEventArgs e){ pageCount = 0; }
private void PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString(drawMe[pageCount++], "Lucida Console", Brushes.Black, new Point(20,20));
e.HasMorePages = (pageCount printMe.Length );
}
这个想法是创建 3 个单独的页面,并将其显示在 PrintPreview 控件中。我缺少什么?
I'm having trouble creating multiple pages in a PrintDocument and displaying them within a PrintPreviewControl. I can create a single page easily enough, but stringing together multiple pages is eluding me.
I'm going to eventually draw several pages of stuff using GDI, but I can't get something like this to work as expected.
private PrintDocument doc = new PrintDocument();
private string[] printMe = new string[]{ "page1", "page2", "page3" );
private int pageCount = 0;
private void FormLoad(object sender, EventArgs e)
{
doc.PrintPage += new PrintPageEventHandler(PrintPage);
PrintPreviewControl.Document = doc;
}
private void doc_BeginPrint(object sender, PrintEventArgs e){ pageCount = 0; }
private void PrintPage(object sender, PrintPageEventArgs e)
{
Graphics g = e.Graphics;
g.DrawString(drawMe[pageCount++], "Lucida Console", Brushes.Black, new Point(20,20));
e.HasMorePages = (pageCount printMe.Length );
}
The idea being that 3 separate pages are created, and displayed within the PrintPreview control. What am I missing?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您的代码片段在分配 e.HasMorePages 的关键点处被完全破坏。您的代码中有一个明显的问题:您需要实现 BeginPrint 事件处理程序以将页计数器重置回 0。
Your code snippet got mangled exactly at the critical point, where you assign e.HasMorePages. There's one glaring problem in your code: you need to implement a BeginPrint event handler to reset the page counter back to 0.
我不确定如何默认显示所有页面,但您可以通过设置
Columns
属性(在 中找到)在PrintPreviewControl
中显示多个页面Properties 窗口的 Layout 部分和/或 Behavior 部分中的Rows
属性的值高于1.I'm not sure how to show all pages by default, but you can show more than one page in the
PrintPreviewControl
by setting theColumns
property, found in the Layout section of the Properties window, and/or theRows
property, found in the Behavior section, to a value higher than 1.