如何防止Enter和Escape键关闭MFC对话框? [英] How to prevent MFC dialog closing on Enter and Escape keys?

查看:105
本文介绍了如何防止Enter和Escape键关闭MFC对话框?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道一种在按下 Enter Esc 键时防止MFC对话框关闭的方法,但是我想了解该过程的更多详细信息,所有常用的替代方法.

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:

  1. 该对话框具有默认控件(请参见CDialog::SetDefID()).然后,将具有此控件ID的WM_COMMAND发送到对话框.
  2. 该对话框没有默认控件.然后将ID = IDOK的WM_COMMAND发送到对话框.
  1. The dialog has a default control (see CDialog::SetDefID()). Then a WM_COMMAND with the ID of this control is sent to the dialog.
  2. 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.

  1. 将默认控件设置为除IDOK之外的其他控件.
  2. 为不调用EndDialog()WM_COMMAND(IDOK)设置处理程序.
  3. 覆盖CDialog::OnOk(),并且不调用基本实现.
  1. Set the default control to other than IDOK.
  2. Set a handler to the WM_COMMAND(IDOK) that does not call EndDialog().
  3. 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:

  1. 为不调用EndDialog()WM_COMMAND(IDCANCEL)设置处理程序.
  2. 覆盖CDialog::OnCancel(),并且不调用基本实现.
  1. Set a handler to the WM_COMMAND(IDCANCEL) that does not call EndDialog().
  2. Override CDialog::OnCancel() and do not call the base implementation.

这篇关于如何防止Enter和Escape键关闭MFC对话框?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆