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

发布于 2024-07-14 07:19:42 字数 130 浏览 9 评论 0 原文

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

How can I handle the Return key (VK_RETURN) in a CEdit control? The CEdit control is parented to a CDialog.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

玩物 2024-07-21 07:19:42

您还可以在对话框的 PreTranslateMessage 中过滤键。 如果您收到 VK_RETURNWM_KEYDOWN,请调用 GetFocus。 如果焦点位于编辑控件上,请调用编辑控件中按下的返回按钮的处理。

请注意,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
}

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

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

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
}
完美的未来在梦里 2024-07-21 07:19:42

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

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

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

UINT CMyEdit::OnGetDlgCode()
{
    return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}
┊风居住的梦幻卍 2024-07-21 07:19:42

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

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

清风夜微凉 2024-07-21 07:19:42

默认情况下,Return 键会关闭 MFC 对话框。 这是因为 Return 键会导致调用 CDialogOnOK() 函数。 您可以重写该函数以拦截 Return 键。 我从 本文(请参阅最后的方法3)。

首先,确保您已使用 类向导,例如:

CEdit m_editFind;

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

protected:
    virtual void OnOK();

然后您可以将以下实现添加到对话框的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();
}

请注意:如果对话框中有一个 ID 为 IDOKOK 按钮,那么它也会调用 OnOK()
如果这给您带来任何问题,那么您必须将按钮重定向到另一个处理程序函数。
方法 3 中也描述了如何执行此操作-key-from-edit-controls-in-a-dialog-box" rel="nofollow noreferrer">我上面提到的文章

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

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 cpp file of your dialog:

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 OK button 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 3 of the article that I have mentioned above.

伏妖词 2024-07-21 07:19:42

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

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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文