如何使用打印对话框?

5
如果您在Visual Studio 2005中进入以下位置(或只需按ctrl+p): 文件 ==> 打印.. 你会得到一个打印对话框。我想在我的程序中也有同样的功能,但是怎么实现呢?
6个回答

5

2
对于CTRL+P快捷键: 在您的表单中添加一个工具栏(我认为它被称为ToolStrip),在其中放置一个条目,并从属性面板中分配CTRL+P快捷键。 对于PrintDialog: 将PrintDialog控件添加到您的表单中,将Document属性设置为应打印的文档。进入工具栏中打印条目的单击事件代码。将PrintDialog.ShowDialog();代码添加到其中,检查是否单击了打印按钮,如果是,则使用DocumentToPrint.Print();进行打印。 以下是示例:
private void Button1_Click(System.Object sender, 
        System.EventArgs e)
    {

        // Allow the user to choose the page range he or she would
        // like to print.
        PrintDialog1.AllowSomePages = true;

        // Show the help button.
        PrintDialog1.ShowHelp = true;

        // Set the Document property to the PrintDocument for 
        // which the PrintPage Event has been handled. To display the
        // dialog, either this property or the PrinterSettings property 
        // must be set 
        PrintDialog1.Document = docToPrint;

        DialogResult result = PrintDialog1.ShowDialog();

        // If the result is OK then print the document.
        if (result==DialogResult.OK)
        {
            docToPrint.Print();
        }

    }

示例源代码: http://msdn.microsoft.com/zh-cn/library/system.windows.forms.printdialog.document.aspx


感谢您提供详细的示例。 - octopusgrabbus

2

1
你可以使用以下代码创建一个标准的打印对话框:
var printDialog = new PrintDialog();
printDialog.ShowDialog();

......但是打印必须由您自己完成...;-)

编辑:对于仍在使用VisualStudio2005的所有人:

PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();

4
算一下吧,自己把它改成PrintDialog printDialog = new PrintDialog()... 翻白眼 - Thorsten Dittmar
1
@ThorstenDittmar:不,Thorsten,我不会这样做。在每种情况下,只要变量的类型是清晰明确的,我就会写一个var。在这种情况下,我是一个“有信念的人”。 :-) - Fischermaen
@Fischermaen:我正在和Remco交谈;-) - Thorsten Dittmar
@ThorstenDittmar:哎呀,我错过了。抱歉。 - Fischermaen

0

嗯,你可以使用巧妙命名的PrintDialog类……


0
如果您使用WinForms来构建用户界面,则可以使用本地的PrintDialog控件(请参见此处)。据我所知,这应该出现在WinForms控件的设计模式中的工具箱中。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接