PrintPreviewDialog 可以修改吗?

发布于 2024-09-01 16:04:50 字数 1457 浏览 1 评论 0原文

目前,我正在做的是:

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

这一切都有效,但是,在用户选择实际打印机并单击“打印”之前,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();
   }
}

Currently, what I'm doing is this:

  1. Using the built-in .NET PrintPreviewDialog
  2. Attaching my own Click handler to the Print button, which allows for the user to select the printer before finally printing.

This all WORKS, HOWEVER, the OnprintToolStripButtonClick event is still sending the document to the default printer BEFORE the user gets to choose the actual printer and click Print (which works, but they're getting an extra copy on the default printer first from the old Handler).

Can I remove this built-in Click handler? I've tried the other methods mentioned on here in regards to using an EventHandlerList to remove the handlers, but it doesn't work for the built-in printing event. Here is a copy of my current code in case it helps clarify:

// ... 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();
   }
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

念﹏祤嫣 2024-09-08 16:04:50

由于您可以访问工具条中的按钮,因此请删除旧的打印按钮并添加您自己的按钮。从默认打印按钮分配图像,一切就完成了。代码看起来像这样:

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);

Since you have access to the buttons in the toolstrip, remove the old print button and add your own. Assign the image from the default print button and you are all set. The code woudl look something like this:

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);
卖梦商人 2024-09-08 16:04:50

我认为替换按钮或使用 PrintPreviewDialog 中的控件名称不是一个好的选择。

从 Net1 到 Net2 更改了工具栏的名称。下一个版本还可以更改它或其他控件的名称。

PrintPreviewDialog是一个非常简单的Form来封装PrintPreviewControl。

您可以构建一个新表单并放置自己的按钮并实现您的功能。

您可以在代码项目 (CoolPrintPreviewDialog) An Enhaced PrintPreviewDialog 中找到一些 PrintPreview 对话框。

在我的 PrvDialog 上,当用户按下“打印”按钮时,我会显示一个 PageSelDialog,以允许用户选择要打印的范围(当前页、某些页、所有页、取消)。

其他解决方案是覆盖 OnBeginPrint / 从 PrintDocument 订阅事件 BeginPrint。
在这里您可以显示 PageSelDialog,取消打印并更改 DefaultPageSettings PrintRange、FromPage、ToPage。

对于此选项,您需要知道何时是 PrintToPrinter、Preview 或 Print From PrintButon。
PrintController.IsPreview,解析预览选项。

I think replace buttons or use the Control Names from PrintPreviewDialog is´nt a good option.

From Net1 to Net2 changes the name for the ToolBar. Next version can also change it or the name for other controls.

The PrintPreviewDialog is a very simple Form to encapsulate PrintPreviewControl.

You can build a new Form and put own buttons and implement your funcionality.

You can find some Dialogs for PrintPreview at Code-Project (CoolPrintPreviewDialog) An Enhaced PrintPreviewDialog.

On my PrvDialog, when user press the Print Button I show a PageSelDialog to allow the user select Range to Print (Current page, Some Pages, All-Pages, Cancel).

Other solution is override OnBeginPrint / suscribe event BeginPrint from PrintDocument.
Here you can show the PageSelDialog, cancel the Print and alter the DefaultPageSettings PrintRange, FromPage, ToPage.

For this Option you need know when is PrintToPrinter, Preview or Print From PrintButon.
PrintController.IsPreview, resolve for Preview Option.

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