windows 如何处理 CEdit 控件中的 Return 键?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/541579/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 11:57:39  来源:igfitidea点击:

How can I handle the Return key in a CEdit control?

c++windowsmfccedit

提问by knaser

How can I handle the Returnkey (VK_RETURN) in a CEditcontrol? The CEditcontrol is parented to a CDialog.

如何处理控件中的Return键 ( VK_RETURN) CEdit?该CEdit控件是CDialog.

回答by Aidan Ryan

You could also filter for the key in your dialog's PreTranslateMessage. If you get WM_KEYDOWNfor VK_RETURN, call GetFocus. If focus is on your edit control, call your handling for return pressed in the edit control.

您还可以过滤对话框的 PreTranslateMessage 中的键。如果你得到WM_KEYDOWNVK_RETURN,打电话GetFocus。如果焦点在您的编辑控件上,请调用您的处理以在编辑控件中按下返回键。

Note the order of clauses in the if relies on short-circuiting to be efficient.

请注意 if 中子句的顺序依赖于短路来提高效率。

BOOL CMyDialog::PreTranslateMessage(MSG* pMsg)
{
    if (pMsg->message == WM_KEYDOWN &&
        pMsg->wParam == VK_RETURN &&
        GetFocus() == m_EditControl)
    {
        // handle return pressed in edit control
        return TRUE; // this doesn't need processing anymore
    }
    return FALSE; // all other cases still need default processing
}

回答by Mark Ingram

The correct answer is to handle the WM_GETDLGCODE/ OnGetDlgCodemessage. In there you can specify that you want all keys to be handled by your class.

正确答案是处理WM_GETDLGCODE/ OnGetDlgCode消息。在那里你可以指定你希望所有的键都由你的类处理。

UINT CMyEdit::OnGetDlgCode()
{
    return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}

回答by honk

By default, the Returnkey closes an MFC dialog. This is, because the Returnkey causes the CDialog's OnOK()function to be called. You can override that function in order to intercept the Returnkey. I got the basic idea from this article(see Method 3at the end).

默认情况下,该Return键关闭 MFC 对话框。这是因为Return键会导致调用CDialog'sOnOK()函数。您可以覆盖该功能以拦截Return密钥。我从这篇文章中得到了基本思想(见文末方法三)。

First, make sure that you have added a member for the edit control to your dialog using the Class Wizard, for example:

首先,确保您已使用类向导将编辑控件的成员添加到对话框中,例如:

CEdit m_editFind;

Next, you can add the following function prototype to the header file of your dialog:

接下来,您可以将以下函数原型添加到对话框的头文件中:

protected:
    virtual void OnOK();

Then you can add the following implementation to the cppfile of your dialog:

然后,您可以将以下实现添加到对话框的cpp文件中:

void CMyDialog::OnOK()
{
    if(GetFocus() == &m_editFind)
    {
        // TODO: Add your handling of the Return key here.
        TRACE0("Return key in edit control pressed\n");

        // Call `return` to leave the dialog open.
        return;
    }

    // Default behavior: Close the dialog.
    CDialog::OnOK();
}

Please note: If you have an OKbutton in your dialog which has the ID IDOK, then it will also call OnOK(). If this causes any problems for you, then you have to redirect the button to another handler function. How to do this is also described in Method 3of the articlethat I have mentioned above.

请注意:如果您的对话框中有一个带有 ID的OK按钮IDOK,那么它也会调用OnOK(). 如果这对您造成任何问题,那么您必须将按钮重定向到另一个处理程序函数。如何做到这一点在我上面提到的文章的方法3中也有描述。

回答by Ben Zhu

I encounter this problem myself. After a little experiment, a simple way existed if you just want to get the do something (after some editing etc) on the return (not specific for which editor you have focus) - I would just create a invisible default button, and let that button handle the 'return' key instead of default Ok button (of course, Ok button should be set default key to false)

我自己也遇到过这个问题。经过一些小实验,如果您只想在返回时执行某些操作(经过一些编辑等)(不特定于您关注的编辑器),则存在一种简单的方法 - 我只会创建一个不可见的默认按钮,然后让它按钮处理 'return' 键而不是默认的 Ok 按钮(当然,Ok 按钮应该设置为默认键为 false)

回答by SAMills

Make certain the Edit Control style ES_WANTRETURN is set in the dialog resource for the control

确保在控件的对话框资源中设置了编辑控件样式 ES_WANTRETURN