Tuesday, December 15, 2009

To close a dialog by pressing esc key

If the dialog has a cancel button, when the esc key is pressed, the dialog will be closed. But if there is no ‘Cancel’ button, pressing esc key will not close the dialog. To make the dialog close when esc is pressed, add the following code in the handler for WM_COMMAND
switch (HIWORD(wParam))
{
case BN_CLICKED:
{
switch (LOWORD(wParam))
{
case IDCANCEL:
EndDialog( hDlg, nResult);
break;
}
}
}

Eventhough cancel button is not present, when esc key is pressed, WM_COMMAND message is sent to the dialog window with BN_CLICKED and ID IDCANCEL

For a standard dialog box,
Clicking on the close button sends a WM_SYSMESSAGE message with code SC_CLOSE to the dialog box
Hitting the ALT+F4 key combination performs the same effect, i.e. a WM_SYSMESSAGE message with SC_CLOSE
Hitting the ESC key simulates a click on the "Cancel", i.e. a WM_COMMAND message with BN_CLICKED and ID IDCANCEL

No comments:

Post a Comment