PrintPreviewDialog修改可能吗?

C. *_*fin 5 c# printing dialog preview

目前,我正在做的是:

  1. 使用内置的.NET PrintPreviewDialog
  2. 将我自己的Click处理程序附加到Print按钮,允许用户在最终打印之前选择打印机.

这一切都工作,但OnprintToolStripButtonClick事件仍在将文档发送到默认打印机,然后用户选择实际的打印机并单击打印(这有效,但是他们首先从默认打印机上获得额外的副本处理程序).

我可以删除这个内置的Click处理程序吗?我已经尝试了这里提到的其他方法,关于使用EventHandlerList来删除处理程序,但它不适用于内置打印事件.以下是我当前代码的副本,以防有助于澄清:

// ... Irrelevant code before this
private PrintPreviewDialog ppdlg;

ToolStrip ts = new ToolStrip();
ts.Name = "wrongToolStrip";
foreach (Control ctl in ppdlg.Controls)
{
   if (ctl.Name.Equals("toolStrip1"))
   {
      ts = ctl as ToolStrip;
      break;
   }
}
ToolStripButton printButton = new ToolStripButton();
foreach (ToolStripItem tsi in ts.Items)
{
   if (tsi.Name.Equals("printToolStripButton"))
   {
      printButton = tsi as ToolStripButton;
   }
}
printButton.Click += new EventHandler(this.SelectPrinterAfterPreview);
// ... Irrelevant code afterwards omitted


// Here is the Handler for choosing a Printer that gets called after the
// PrintPreviewDialog's "Print" button is clicked.
private void SelectPrinterAfterPreview(object sender, EventArgs e)
{
   frmMainPage frmMain = (frmMainPage)this.MdiParent;
   if (frmMain.printDialog1.ShowDialog() == DialogResult.OK)
   {
      pd.PrinterSettings.PrinterName = frmMain.printDialog1.PrinterSettings.PrinterName;
      pd.PrinterSettings.Copies = frmMain.printDialog1.PrinterSettings.Copies;
      pd.Print();
   }
}
Run Code Online (Sandbox Code Playgroud)

anc*_*dra 6

由于您可以访问工具栏中的按钮,因此请删除旧的打印按钮并添加自己的按钮.从默认打印按钮分配图像,您已全部设置.代码看起来像这样:

ts.Items.Remove(printButton);
ToolStripButton b = new ToolStripButton();
b.ImageIndex = printButton.ImageIndex;
b.Visible = true;
ts.Items.Insert(0, b);
b.Click += new EventHandler(this.SelectPrinterAfterPreview);
Run Code Online (Sandbox Code Playgroud)