按下 ESC 时关闭 PrintPreviewDialog

发布于 2024-08-20 07:14:59 字数 240 浏览 9 评论 0原文

我正在开发一个 WinForms 应用程序,它使用 System.Windows.Forms.PrintPreviewDialog 来显示打印预览对话框。当用户在该对话框中按 ESC 时,我想关闭该对话框。不幸的是,我不知道如何做到这一点。我尝试安装 KeyDown/PreviewKeyDown 事件处理程序,但它从未被调用。我还尝试将焦点设置到对话框(及其 PrintPreviewControl),认为这就是问题所在,但这也没有帮助。有谁知道如何进行这项工作?

I'm working on a WinForms application that uses System.Windows.Forms.PrintPreviewDialog to display a Print Preview dialog. When the user presses ESC in that dialog, I'd like to close the dialog. Unfortunately, I can't figure out how to do this. I've tried to install a KeyDown/PreviewKeyDown event handler, but it never gets called. I also tried setting focus to the dialog (and to its PrintPreviewControl), thinking that was the issue, but that didn't help either. Does anyone have any idea how to make this work?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

冷月断魂刀 2024-08-27 07:14:59

我最终自定义了 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);
    }
  }
}

I ended up customizing PrintPreviewDialog and overriding its ProcessCmdKey method to close the form when the user presses ESC. This seems like the cleanest solution.

Here's the code that I wrote:

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);
    }
  }
}
弱骨蛰伏 2024-08-27 07:14:59

我还没有尝试过这个,但是当您按 Esc 时,System.Windows.Forms 不会调用 CancelButton 吗?尝试创建一个虚拟取消按钮,该按钮调用表单上的 .Close

I haven't tried this, but don't System.Windows.Formss call CancelButton when you press Esc? Try creating a dummy Cancel button which calls .Close on the form.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文