How to prevent MFC dialog closing on Enter and Escape keys?(如何防止 MFC 对话框在 Enter 和 Escape 键上关闭?)
问题描述
我知道一种防止 MFC 对话框在按下 Enter 或 Esc 键时关闭的方法,但我想了解更多有关该过程的详细信息和这样做的所有常见替代方法.
I know one method of preventing an MFC dialog from closing when the Enter or Esc keys are pressed, but I'd like to know more details of the process and all the common alternative methods for doing so.
提前感谢您的帮助.
推荐答案
当用户在对话框中按下 Enter 键时,会发生两种情况:
When the user presses Enter key in a dialog two things can happen:
- 对话框有一个默认控件(参见
CDialog::SetDefID()
).然后将带有此控件 ID 的 WM_COMMAND 发送到对话框. - 对话框没有默认控件.然后将 ID = IDOK 的 WM_COMMAND 发送到对话框.
- The dialog has a default control (see
CDialog::SetDefID()
). Then a WM_COMMAND with the ID of this control is sent to the dialog. - The dialog does not have a default control. Then WM_COMMAND with ID = IDOK is sent to the dialog.
使用第一个选项,默认控件的 ID 可能等于 IDOK.那么结果将与第二个选项中的结果相同.
With the first option, it may happen that the default control has a ID equal to IDOK. Then the results will be the same that in the second option.
默认情况下,CDialog
类有一个 WM_COMMAND(IDOK)
的处理程序,用于调用 CDialog::OnOk()
,这是一个虚函数,默认情况下它调用 EndDialog(IDOK)
来关闭对话框.
By default, class CDialog
has a handler for the WM_COMMAND(IDOK)
that is to call to CDialog::OnOk()
, that is a virtual function, and by default it calls EndDialog(IDOK)
that closes the dialog.
因此,如果您想避免关闭对话框,请执行以下操作之一.
So, if you want to avoid the dialog being closed, do one of the following.
- 将默认控件设置为
IDOK
以外的其他控件. - 为不调用
EndDialog()
的WM_COMMAND(IDOK)
设置一个处理程序. - 覆盖
CDialog::OnOk()
并且不调用基本实现.
- Set the default control to other than
IDOK
. - Set a handler to the
WM_COMMAND(IDOK)
that does not callEndDialog()
. - Override
CDialog::OnOk()
and do not call the base implementation.
关于 IDCANCEL,类似但没有等效的 SetDefID()
并且 ESC 键是硬编码的.所以为了避免对话框被关闭:
About IDCANCEL, it is similar but there is not equivalent SetDefID()
and the ESC key is hardcoded. So to avoid the dialog being closed:
- 为不调用
EndDialog()
的WM_COMMAND(IDCANCEL)
设置一个处理程序. - 重写
CDialog::OnCancel()
并且不调用基本实现.
- Set a handler to the
WM_COMMAND(IDCANCEL)
that does not callEndDialog()
. - Override
CDialog::OnCancel()
and do not call the base implementation.
这篇关于如何防止 MFC 对话框在 Enter 和 Escape 键上关闭?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何防止 MFC 对话框在 Enter 和 Escape 键上关闭?


基础教程推荐
- 如何检查GTK+3.0中的小部件类型? 2022-11-30
- 常量变量在标题中不起作用 2021-01-01
- 如何通过C程序打开命令提示符Cmd 2022-12-09
- 如何在 C++ 中初始化静态常量成员? 2022-01-01
- C++结构和函数声明。为什么它不能编译? 2022-11-07
- 我有静态或动态 boost 库吗? 2021-01-01
- 这个宏可以转换成函数吗? 2022-01-01
- 如何将 std::pair 的排序 std::list 转换为 std::map 2022-01-01
- 在 C++ 中计算滚动/移动平均值 2021-01-01
- 静态库、静态链接动态库和动态链接动态库的 .lib 文件里面是什么? 2021-01-01