C# 如何使用“打印”对话框
如果在Visual Studio 2005中执行以下操作(或只执行ctrl+p): 文件==>打印C# 如何使用“打印”对话框,c#,printing,C#,Printing,如果在Visual Studio 2005中执行以下操作(或只执行ctrl+p): 文件==>打印 您将看到一个打印对话框屏幕。我想在我的程序中使用相同的对话框,但如何使用?此对话框是一个所谓的通用对话框,是一个内置的Windows对话框,可供多个应用程序使用 要在C#应用程序中使用此对话框,可以使用PrintDialog类。以下MSDN页面包含说明以及一些示例代码: WinForms: WPF:(感谢) 嗯,您可以使用名称巧妙的类……您可以使用以下标准打印对话框: var printDial
您将看到一个打印对话框屏幕。我想在我的程序中使用相同的对话框,但如何使用?此对话框是一个所谓的通用对话框,是一个内置的Windows对话框,可供多个应用程序使用 要在C#应用程序中使用此对话框,可以使用
PrintDialog
类。以下MSDN页面包含说明以及一些示例代码:
- WinForms:
- WPF:(感谢)
var printDialog = new PrintDialog();
printDialog.ShowDialog();
。。。但是打印必须由您自己完成…;-)
编辑:对于所有仍在使用VisualStudio2005的用户:
PrintDialog printDialog = new PrintDialog();
printDialog.ShowDialog();
如果您使用WinForms构建UI,则可以使用本机PrintDialog控件(请参阅)。据我所知,它应该以WinForms控件的设计器模式出现在工具箱中。如果使用WPF,可以使用
PrintDialog
:
如果您喜欢WinForms,您可以使用…PrintDialog
:作为CTRL+p快捷键:
在表单中添加一个工具栏(我想它被称为ToolStrip),在其中添加一个条目,在属性面板中指定快捷键CTRL+P。
对于打印对话框:
向窗体中添加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();
}
}
示例来源:Oh come-on-进行计算并将其更改为PrintDialog PrintDialog=new PrintDialog()
yourself。。。rolleyes@ThorstenDittmar:不,托尔斯滕,我不会那样做。在每种情况下,如果清楚变量的类型,我将编写一个var
。在这种情况下,我是一个“坚定的人”:-)@费舍马恩:我在和雷姆科说话;-)@ThorstenDittmar:哎呀,错过了。对不起,谢谢你提供了一个充实的例子。