How to prevent dialog box from getting dismissed On pressing Esc key ?


If a dialog box is displayed and Esc key is pressed, OnCancel( ) handler gets called and the dialog gets dismissed. At times we want to take some other step when Esc key is pressed. For example, if user has selected an item from a combo box we may want that his selection should get cancelled if he presses Esc key. In this situation we cannot handle WM_KEYDOWN message to check whether Esc is pressed since this message is not generated if Esc key is hit when the dialog is displayed. This situation can be handled in the following way:

Add a handler for the ‘Cancel’ button and add code to it as given below:

void CTrickDlg::OnCancel( )

{

short i = ::GetKeyState ( 27 ) ;

if ( i < 0 )

;

else

CDialog::OnCancel( ) ;

}

Here we have checked whether Esc is hit by calling ::GetKeyState( ) function and passing to it virtual code of Esc key. If OnCancel( ) is called because of pressing Esc key do nothing (note the ‘;’ after if statement) otherwise call base class’s OnCancel( ) to dismiss the dialog.

Subscribe / Share

It's very calm over here, why not leave a comment?

Leave a Reply




Categories

Powered by Yahoo! Answers