Eme*_*gul 3 print-preview winforms
我正在开发一个System.Windows.Forms.PrintPreviewDialog用于显示打印预览对话框的 WinForms 应用程序。当用户在该对话框中按 ESC 时,我想关闭该对话框。不幸的是,我无法弄清楚如何做到这一点。我尝试安装 KeyDown/PreviewKeyDown 事件处理程序,但它从未被调用。我还尝试将焦点设置到对话框(及其 PrintPreviewControl),认为这是问题所在,但这也无济于事。有谁知道如何使这项工作?
我最终自定义PrintPreviewDialog并覆盖了它的ProcessCmdKey方法以在用户按下 ESC 时关闭表单。这似乎是最干净的解决方案。
这是我写的代码:
using System.Windows.Forms;
namespace MyProject.UI.Dialogs
{
class CustomPrintPreviewDialog : PrintPreviewDialog
{
protected override bool ProcessCmdKey(ref Message msg, Keys keyData)
{
// Close the dialog when the user presses ESC
if (keyData == Keys.Escape)
{
this.Close();
return true;
}
return base.ProcessCmdKey(ref msg, keyData);
}
}
}
Run Code Online (Sandbox Code Playgroud)