在 PrintPreviewControl 上显示 PrintDocument 的所有页面

发布于 2024-08-20 06:01:04 字数 818 浏览 4 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

一梦浮鱼 2024-08-27 06:01:04

您的代码片段在分配 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.

金橙橙 2024-08-27 06:01:04

我不确定如何默认显示所有页面,但您可以通过设置 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 the Columns property, found in the Layout section of the Properties window, and/or the Rows property, found in the Behavior section, to a value higher than 1.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文