1

如果我运行此代码,并在 PrintDialog 上按取消,它仍然会打印。如何判断使用是否按下取消?

PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();

dialog.ShowDialog();
document.PrinterSettings = p.PrinterSettings;
document.Print();

附录

WebBrowser w = new WebBrowser();
w.ShowPrintDialog(); //.ShowPrintDialog returns a void, how can I deal with this?
4

3 回答 3

7

您可以检查 ShowDialog 方法的结果:

if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
{
   //Print
}
于 2011-02-15T18:35:07.707 回答
3

ShowDialog 返回一个对话框结果枚举。它将是 OK,或 Cancel。

PrintDocument document = new PrintDocument();
PrintDialog dialog = new PrintDialog();

if(dialog.ShowDialog() == DialogResult.Ok)
{
    document.PrinterSettings = p.PrinterSettings;
    document.Print();
}
于 2011-02-15T18:35:50.223 回答
0

上面的答案是正确的System.Windows.Forms.PrintDialog。但是,如果您不构建Forms应用程序,PrintDialog您将使用 is System.Windows.Controls.PrintDialog. 在这里,ShowDialog返回一个bool?

var dialog = new System.Windows.Controls.PrintDialog();

if (dialog.ShowDialog() == true)
{
    // Print...
}
于 2015-07-17T12:32:55.267 回答