关闭另一个表单时,C#打印预览将关闭

时间:2012-11-19 12:10:09

标签: c# winforms printing progress

我正在显示某些大页面的打印预览窗口。这些渲染需要一些时间,所以我想显示比默认情况更多的进度信息.Page 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;
}

&#39; this.Close()&#39;还关闭我的打印预览!如果我删除该行,则只显示打印预览并保持打开状态。所以Close()函数正在关闭进度窗口和PrintPreview窗口。

为什么会这样?

0 个答案:

没有答案