按下 ESC 时关闭 PrintPreviewDialog
我正在开发一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我最终自定义了
PrintPreviewDialog
并覆盖其ProcessCmdKey
方法用于在用户按 ESC 时关闭表单。这似乎是最干净的解决方案。这是我写的代码:
I ended up customizing
PrintPreviewDialog
and overriding itsProcessCmdKey
method to close the form when the user presses ESC. This seems like the cleanest solution.Here's the code that I wrote:
我还没有尝试过这个,但是当您按 Esc 时,
System.Windows.Forms
不会调用CancelButton
吗?尝试创建一个虚拟取消按钮,该按钮调用表单上的.Close
。I haven't tried this, but don't
System.Windows.Forms
s callCancelButton
when you press Esc? Try creating a dummy Cancel button which calls.Close
on the form.