如何使用“打印”对话框

如果你进入以下Visual Studio 2005(或只是执行ctrl + p):文件==>打印..

你得到一个打印对话框屏幕。 我希望在我的程序中也一样,但是怎么样?

此对话框是一个所谓的通用对话框 ,一个可供多个应用程序使用的内置Windows对话框。

要在C#应用程序中使用此对话框,可以使用PrintDialog类。 以下MSDN页面包含说明以及一些示例代码:

  • WinForms: System.Windows.Forms.PrintDialog
  • WPF: System.Windows.Controls.PrintDialog (感谢Bartosz )

如果您使用WPF,您可以使用PrintDialog : http : //msdn.microsoft.com/en-us/library/system.windows.controls.printdialog.aspx

如果你进入WinForms,你可以使用… PrintDialog : http : //msdn.microsoft.com/en-us/library/system.windows.forms.printdialog.aspx

对于CTRL + P快捷方式:向表单添加一个工具栏(我认为它被称为ToolStrip),在其中放入一个条目,从属性面板中指定快捷键CTRL + P. 对于PrintDialog:将PrintDialog控件添加到窗体并将Document属性设置为应打印的文档。 进入工具栏中打印条目的单击事件的代码。 添加代码PrintDialog.ShowDialog(); 检查是否单击了“打印”按钮,如果是,则使用DocumentToPrint.Print();打印它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/en-us/library/system.windows.forms.printdialog.document.aspx

您可以使用以下标准打印对话框:

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

……但打印必须由你自己完成…… 😉

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

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

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

如果您使用WinForms构建UI,那么您可以使用本机PrintDialog控件(请参阅此处 )。 据我所知,这应该出现在WinForms控件的设计器模式的工具箱中。