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

kna*_*ser 10 c++ windows mfc cedit

如何处理控件中的Returnkey(VK_RETURN)CEdit?该CEdit控制父到CDialog.

Aid*_*yan 15

您还可以在对话框的PreTranslateMessage中过滤键.如果你得到WM_KEYDOWNVK_RETURN,打电话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
}
Run Code Online (Sandbox Code Playgroud)


Mar*_*ram 5

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

UINT CMyEdit::OnGetDlgCode()
{
    return CEdit::OnGetDlgCode() | DLGC_WANTALLKEYS;
}
Run Code Online (Sandbox Code Playgroud)


小智 3

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

  • ES_WANTRETURN *仅*适用于多行文本框。 (3认同)