Click to See Complete Forum and Search --> : Trapping ESC in CDialog


Jason Coene
May 16th, 1999, 12:29 AM
Hi. I need a simple way to make it so that when you hit ESC, the dialog does not exit. I am assuming its something with keydown message, but exactly how to do it i am not sure.

thanks in advance

May 16th, 1999, 09:30 AM
I think, that pressing ESC in CDialog associated with OnCancel function. So the siplest way, IMO, is to overload OnCancel function that way :

void CAboutDlg::OnCancel()
{
return;
}




And, if you need to use cancel button, but without ESC association, you can give it ID different from IDCANCEL, and in the end of your function call CDialog::OnCancel().

Jack.

nagi
June 10th, 1999, 05:45 AM
use OnCommand, When u press Esc Key wParam on OnCommand will have value 2.

For Example,

if ur Dialog class is CMydialog, then
void CMydialog::OnCommand(WPARAM wParam,LPARAM lParam)
{
if (wParam==2)
return FALSE;

return CDialog::OnCommand(wParam, lParam);
}

I am certain this works.
Chow,

nagi
June 10th, 1999, 05:50 AM
Hay, there is another way,
If u Use OnCommand or OnCancel u will not able to close dialog, with Alt+f4
Instead of those use this,

BOOL CProcessApplSysDlg::PreTranslateMessage(MSG* pMsg)
{
// TODO: Add your specialized code here and/or call the base class
BOOL Result=FALSE;
if(pMsg->message==WM_CHAR)
{

OnChar((UINT)pMsg->wParam,(UINT)LOWORD(pMsg->lParam),
(UINT)HIWORD(pMsg->lParam));
Result=KeyPressResult((UINT)pMsg->wParam);

}
}

BOOL CProcessApplSysDlg::KeyPressResult(UINT KeyID)
{
if(KeyID==2)
return FALSE;
else
return TRUE;
}

in this way ESc Key is trapped & Alt+f4 works fine

I am certain that this will do,

Chow,
Nagi,