Prevent Enter and Esc close dialog program

来源:互联网 发布:机智的监狱生活 知乎 编辑:程序博客网 时间:2024/06/05 17:01

Well there are also many way, such as Override the OnOK() and OnCancel().

The easier and more powerful way is to Overrides the function PreTranslateMessage() in the dialog class.

BOOL CServerSocketDlg::PreTranslateMessage(MSG* pMsg)

{

    if (pMsg->message == WM_KEYDOWN || nVirtKey == VK_RETURN) {

 

       int nVirtKey = (int) pMsg->wParam;

       if (nVirtKey == VK_ESCAPE)

           return TRUE;

    }

   

    return CDialog::PreTranslateMessage(pMsg);

}

 

But in this way all Enter key pressed event will be disabled, if wanna press Enter to click a button, we can add extra code:

       // CButton m_ctlMessage

       if (nVirtKey == VK_RETURN && (GetFocus()->m_hWnd  == m_ctlMessage.m_hWnd))

       {

           // this is the CButton function

OnBnClickedButton1();

           return TRUE;

       }