如何在打印预览对话框中添加打印对话框? [英] How to add print dialog to the printpreviewdialog?

查看:124
本文介绍了如何在打印预览对话框中添加打印对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的老板要我创建具有打印功能的窗口表单,但他想在预览后打印 datagridview

My boss wants me to create the window form that has printing function, but he wants to print the datagridview after preview.

因此,现在我鼓励了这个问题,当单击 printpreviewdialog 上的打印按钮时,我无法打印多张纸或选择打印机或进行任何更改。单击按钮,将直接打印纸张。因此,我希望加入 printpreviewdialog printdialog

So now I encourage the problem, I cannot print multiple set of paper or choose the printer or make any changes when click the print button on printpreviewdialog.When I click the button, it will direct print the paper. So I wish to join the printpreviewdialog and printdialog.

为什么 printpreviewdialog printdialog 只能用于不同的按钮?当需要单击一个按钮进行预览并单击另一个按钮以打印多套打印机并进行打印机更改时,缺乏可用性。

Why the printpreviewdialog and printdialog can only be used in different buttons? It's lack of usibility when needed to click one button to preview and click another button to print multiple set and make changes of printer.

有谁能帮助我?

printdialog

printdialog

DialogResult result = printDialog1.ShowDialog();
            // If the result is OK then print the document.
            if (result == DialogResult.OK)
            {
                position = 0;
                pageno = 1;
                printDocument2.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
                printDocument2.OriginAtMargins = true;
                printPreviewDialog1.Document = printDocument2;
                printPreviewDialog1.ShowDialog();
            }   

printpreviewdialog

printpreviewdialog

printDocument3.DefaultPageSettings.Margins = new Margins(20, 20, 20, 20);
            printDocument3.OriginAtMargins = true;
            //((ToolStripButton)((ToolStrip)printPreviewDialog1.Controls[1]).Items[0]).Enabled = false;
            printPreviewDialog1.Document = printDocument3;
            printPreviewDialog1.ShowDialog();


推荐答案

我知道已经晚了,但我想有人会仍然需要。
正如Hans Passant所说,打印预览在很大程度上取决于打印机和页面设置。
但是在printpreviewdialog中有一个打印按钮,对于大多数情况仍然是合理的。但是该按钮将直接打印到您的默认打印机,并且从不显示对话框。
如果要从printpreview对话框中进行打印对话框,则只需操纵PrintPreviewDialog的ToolStrip。

I know it is late, but i think someone will still need that. As Hans Passant say, "print preview is heavily depended on printer and page settings." But there is a print-button in printpreviewdialog, which is still reasonable for most cases. But that button directly prints to your default printer, and never shows a dialog. If you want a print dialog from printpreview dialog, you can just manipulate ToolStrip of PrintPreviewDialog.

在这里(假设您初始化了printPreviewDialog1,printDialog1和printDocument1对象) )

Here it goes (assuming you initialized printPreviewDialog1, printDialog1 and printDocument1 objects)

printPreviewDialog1.Document = printDocument1;
ToolStripButton b = new ToolStripButton();
b.Image = Properties.Resources.PrintIcon;
b.DisplayStyle = ToolStripItemDisplayStyle.Image;
b.Click += printPreview_PrintClick;
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.RemoveAt(0);
((ToolStrip)(printPreviewDialog1.Controls[1])).Items.Insert(0, b);
printPreviewDialog1.ShowDialog();

使用上述代码,您可以删除ToolPremium上的PrintPreview上的默认打印按钮,并将其替换为新创建的打印按钮。现在,此按钮具有Click事件处理程序,通过使用它,您可以显示PrintDialog。

Using above code, you can remove default print button on ToolStrip of PrintPreview and replace it with a newly created "print button". This button now has an Click event handler, and by using it, you can show the PrintDialog.

private void printPreview_PrintClick(object sender, EventArgs e)
{
    try
    {
        printDialog1.Document = printDocument1;
        if (printDialog1.ShowDialog() == DialogResult.OK)
        {
            printDocument1.Print();
        }
    }
    catch (Exception ex)
    {
        MessageBox.Show(ex.Message, ToString());
    }
}

这篇关于如何在打印预览对话框中添加打印对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆