如何防止MFC对话框处理回车键和转义键而不将其传递给 [英] How to prevent an MFC dialog from handling the enter and escape keys and not passing it on

查看:111
本文介绍了如何防止MFC对话框处理回车键和转义键而不将其传递给的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何防止MFC对话框处理回车键和转义键而不将其传递给其他人.当我们在对话框中输入时,apllication已关闭.

How to prevent an MFC dialog from handling the enter and escape keys and not passing it on.when we enter in dialog ,apllication has closed.

推荐答案

您可能会覆盖PreTranslateMessage():
You may override PreTranslateMessage():
BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
    BOOL bRet = CDialog::PreTranslateMessage(pMsg);
    if (pMsg->message == WM_KEYDOWN &&
        (pMsg->wParam == VC_RETURN || pMsg->wParam == VK_ESCAPE))
        bRet = TRUE; // do not dispatch message
    return bRet;
}


但是您应该考虑一下.是的,用户无法使用键盘关闭对话框.


But you should think about it. When using this, the user can not close the dialog using the keyboard.


是的,我讨厌有人在编辑框中键入内容并自然按下返回"键,关闭对话框.

返回"作为"OnOK"回调进入对话框.如果您不处理代码中的"OnOK"(例如您没有显示"OK"按钮),则只需提供一个不执行任何操作的OnOK()处理函数(不要调用CDialog :: OnOK) ())

Yeah, I hate it when somebody''s typing in an edit box and naturally hits the "return" key and closes the dialog.

"Return" comes through to your dialog as the "OnOK" callback. If you''re not handling the "OnOK" in your code (like you don''t have an "OK" button displayed), you can simply provide an OnOK() handler that does nothing (do not call CDialog::OnOK())

void MyDialog::OnOK()
{
// I have no OK button so don''t let default <cr> close the app
}</cr>


转义符映射到IDCANCEL按钮.因此,给该按钮一个不同的ID(如果您有一个取消按钮).然后在IDCANCEL处理程序中,仅返回而不是调用CDialog :: OnCancel().将您重命名为取消"按钮的映射到一个调用父对话框类OnCancel()成员的处理程序.

您会看到IDOK按钮是属性中的默认按钮.将其关闭,除非它具有焦点,否则"Enter"键将不会激活该按钮.
Escape gets mapped to the IDCANCEL button. So give that button a different ID (if you have a cancel button). Then in the IDCANCEL handler, just return, rather than call CDialog::OnCancel(). Map you renamed Cancel button to a handler that calls the parent dialog class OnCancel() member.

You''ll see that the IDOK button is the default button in properties. Turn that off, and the ''Enter'' key won''t activate that button unless it has focus.


这篇关于如何防止MFC对话框处理回车键和转义键而不将其传递给的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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