How to prevent MFC dialog closing on Enter and Escape keys?
How to prevent MFC dialog closing on Enter and Escape keys?
There is an alternative to the previous answer, which is useful if you wish to still have an OK / Close button. If you override the PreTranslateMessage function, you can catch the use of VK_ESCAPE / VK_RETURN like so:
BOOL CMainDialog::PreTranslateMessage(MSG* pMsg)
{
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
{
return TRUE; // Do not process further
}
}
return CDialogEx::PreTranslateMessage(pMsg);
}
이렇게 수정하여야 한다.
PreTranslateMessage 함수를 생성하는 방법은
1. Enter, ESC 키를 차단할 DialogClass 를 우클릭
2. [클래스 마법사] 를 켠다.
3. [가상 함수]
4. 가상 함수 탭에서 PreTranslateMessage 를 선택
5. [함수 추가]
6. 추가된 함수에
if (pMsg->message == WM_KEYDOWN)
{
if (pMsg->wParam == VK_RETURN || pMsg->wParam == VK_ESCAPE)
{
return TRUE; // Do not process further
}
}
추가 작성