Countermeasure to prevent the dialog box program from exiting by pressing enter and ESC

Source: Internet
Author: User
In normal cases, when you run a dialog box program, if you do not pay attention to pressing the enter or ESC key, the program will immediately exit. The reason is that when you press enter, windows will automatically find the button with which the input focus falls. When the button with the focus is obtained, it will be surrounded by a dot-line rectangle. If none of the buttons get the input focus, Windows will automatically find the default button specified by the program or resource (the border of the default button is coarse ). If there is no default button in the dialog box, the onok function is automatically called even if there is no OK button in the dialog box. For a normal Dialog Box program, the onok function is called, I thought the program would quit immediately. To make the Enter key invalid, the simplest way is to write the onok function of cexdlg as an empty function, and then write a new function for the OK button to respond. The same is true for the ESC key. It is mapped to the oncancel function by default. For the ESC key, you need to reload the cdialog class pretranslatemessage function. When it is found to be the ESC key, filter out the message or replace the message.

The following is a simple code example:

Method 1]
You can reload the onok function first.
Voidctestdlg: onok ()
{// Nothing in it}

Then reload the pretranslatemessage function.
Replace the ESC key message with the return key message. In this way, when you press ESC, the onok function is also executed, so that the problem can be solved.

Bool cxxxdlg: pretranslatemessage (MSG * PMSG)
{
If (PMSG-> message = wm_keydown & PMSG-> wparam = vk_escape)
{
PMSG-> wparam = vk_return; // Replace the ESC key message with the return key message. In this way, when you press ESC
// The onok function will also be called, and the onok function will not do anything, so that the ESC will be blocked.
}
Return cdialog: pretranslatemessage (PMSG );

}

Method 2]

The carriage return and ESC messages are directly blocked in the overloaded pretranslatemessage function, which is similar to the preceding method:

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 );
}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.