我正在显示一些大页面的打印预览窗口。这些渲染需要一些时间,所以我想显示比默认的“第 1/1 页”消息更多的进度信息。
我创建了一个简单的 windows 窗体项目,显示了我当前的方法:
(您可以创建一个包含两个表单的新 Windows 表单项目,如果您愿意,可以复制粘贴)
public event EventHandler PrintReady;
public event ProgressChangedEventHandler ProgressChanged;
private void button1_Click(object sender, EventArgs e)
{
var printDocument = new PrintDocument();
printDocument.PrintPage += new PrintPageEventHandler(printDocument_PrintPage);
printDocument.EndPrint += new PrintEventHandler(printDocument_EndPrint);
var printPreviewDialog = new PrintPreviewDialog();
printPreviewDialog.Document = printDocument;
// Create the 'progress' form.
var form2 = new Form2(this);
form2.Show();
printPreviewDialog.ShowDialog();
}
void printDocument_PrintPage(object sender, PrintPageEventArgs e)
{
for (int i = 0; i < 100; i++)
{
// This is where I do some rendering.
Thread.Sleep(20);
if (this.ProgressChanged != null)
{
this.ProgressChanged.Invoke(this, new ProgressChangedEventArgs(i, null));
}
}
}
void printDocument_EndPrint(object sender, PrintEventArgs e)
{
if (this.PrintReady != null)
{
this.PrintReady.Invoke(this, null);
}
}
然后当然是我的进度表:
public Form2(Form1 form1)
{
form1.ProgressChanged += new ProgressChangedEventHandler(ProgressChangedHandler);
form1.PrintReady += new EventHandler(PrintReadyHandler);
InitializeComponent();
}
void PrintReadyHandler(object sender, EventArgs e)
{
this.Close();
}
void ProgressChangedHandler(object sender, ProgressChangedEventArgs e)
{
progressBar1.Value = e.ProgressPercentage;
}
'this.Close()' 也关闭了我的打印预览!如果我删除该行,则仅显示打印预览并保持打开状态。所以 Close() 函数正在关闭进度窗口和 PrintPreview 窗口。
为什么会这样?