Prevent dialog box procedures by ENTER and ESC exit

xiaoxiao2021-03-06  66

In the general case, the user is running, if you don't pay attention to the Enter or ESC key, the program will immediately exit, because it is because the Enter key is pressed, Windows will automatically Go to the input focus to fall on which button is found, when the four weight of the focus will be surrounded by the point line rectangle. If all buttons do not get input focus, Windows automatically goes to find the default button specified by the program or resource (the default button border is complicated). If the dialog does not have the default button, even if there is no OK button in the dialog, the ONOK function will be called, for a normal dialog program, the call of the Onok function, thinking that the program will immediately exit. In order to make the Enter key, the easiest way is to write the CEXDLG's onok function into an empty function, and then write a new function for the OK button. The same is true for the ESC key, it is the default and the oncancel function mapped together. For the ESC key, you need to overload the PretranslateMessage function of the CDIALOG class. When it is found to be an ESC key, filter out this message or replace this message.

A simple code example:

【method 1】

You can overload the ONOK function first

voidctestdlg :: onok ()

{// nothing inside}

Then overload the PretranslateMessage function

Replace the message of the ESC key, replace the message with the return key so that when you press ESC, you will also perform the only onok function, so that you can solve it.

Bool CXXXDLG :: PretranslateMessage (MSG * PMSG)

{

IF (pmsg-> message == wm_keydown && pmsg-> wparam == vk_escape)

{

PMSG-> wparam = vk_return; // Replace the message of the ESC button with the message of the Enter key, so that when the ESC is pressed

// will also call the ONOK function, and ONOK does not do, so that ESC is also blocked.

}

Return CDIALOG :: PretranslateMessage (PMSG);

}

[Method 2]

The message is shielded and ESC directly in the overloaded PretranslateMessage function, and the above method is similar:

Bool CXXXDLG :: PretranslateMessage (MSG * PMSG)

{

IF (pmsg-> message == wm_keydown && pmsg-> wparam == vk_escape) Return True;

IF (PMSG-> Message == WM_Keydown && Pmsg-> wparam == vk_retuen) Return True;

Else

Return CDIALOG :: PretranslateMessage (PMSG);

}

转载请注明原文地址:https://www.9cbs.com/read-110436.html

New Post(0)