Welcome

Wheresoever you go, go with all your heart. (Confucius)

11/17/2010

[MFC] Ignore Enter and ESC key on the Dialog

If you push ENTER and ESC key on the Dialog, this Dialog is closed.
There are two solutions to solve this problem.

1. Catch the message for inputting these key.
BOOL CTestDlg::PreTranslateMessage(MSG* pMsg)
{
    if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_RETURN) // in case of ENTER key
        pMsg->wParam = 1;
    if(pMsg->message == WM_KEYDOWN && pMsg->wParam == VK_ESCAPE) // in case of ESC key
        pMsg->wParam = 1;

    return CDialog::PreTranslateMessage(pMsg);
}

2. Override OnOK() and OnCancel().
In the head file,
private:
    virtual void OnOK(){}
    virtual void OnCancel(){}

No comments: