-Press ENTER or ESC to exit the program.

Source: Internet
Author: User

Vs 2008, the default focus is on the OK button. Press enter to trigger the onok event of this button. Press enter by default to trigger the onok event to see if there is any invalid breakpoint; comment out onok (); or reset the tab key order; cancel the defaultbutton attribute of onok;

 

I. Reasons

When you press enter, Windows automatically finds the button with which the input focus falls. The circle around the button with the focus is surrounded by a dotted 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 no default button exists in the dialog box, the onok function is automatically called even if no OK button exists in the dialog box. I deleted all the two buttons in the dialog box, and then compiled and run the program. If I press the Enter key, the program is automatically closed. MFC responds to the Enter key by default.

Ii. Solution

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 procedure is as follows:

First, create a dialog box-based program named ex.

1. Use classwizard to map the idok button to the onok virtual function.

In classwizard, select idok from the object IDs list of cexdlg, and click bn_clicked to generate the prototype and framework of the onok function.

2. Use the dialog box editor to change the OK button ID.

Select the OK button, change its ID from idok to idc_ OK, and remove the check mark of the default button attribute. In this way, the onok function is separated.

3. Use classwizard to create an onclickedok member function.

In classwizard, select idc_ OK from the object IDs list of cexdlg, and click bn_clicked. The function name uses onclickedok to generate the prototype and framework of the onclickedok function.

4. Edit the code.

Void cexdlg: onok ()
{
// Todo: add extra validation here
// Cdialog: onok ();
}

Void cexdlg: onclickedok ()
{
// Todo: add your control notification handler code here
Cdialog: onok ();
}

5. Compile and test the program.

Recompile the program and test the Enter key. You will find that after you press enter, there will be no action, and clicking OK will still be the same as before.

3. oncancel Processing

Just as the Enter key can trigger the onok function call, the ESC key can also trigger the oncancel function call. It causes the control to exit from the dialog box. However, the reader can handle this situation like the OK button by setting an invalid oncancel function.

Third Method
Bool cxxxdlg: pretranslatemessage (MSG * PMSG)
{
// Modify the return key operation response
If (PMSG-> message = wm_keydown & PMSG-> wparam = vk_return)
{
Uint nid = getfocus ()-> getdlgctrlid ();
If (ctbutton! = Getctrltype (NID ))
{
Nextdlgctrl ();
Int nfocusid = getfocus ()-> getdlgctrlid ();
If (nfocusid = 1001)
(Ccombobox *) (getfocus ()-> getparent ()-> showdropdown ();
Else if (ctcombobox = getctrltype (nfocusid ))
(Ccombobox *) getfocus ()-> showdropdown ();

Return true;
}
}
Return cdialog: pretranslatemessage (PMSG );
}

 

1. In VC ++, How can I obtain the message "press the Enter key" of the Edit Control in the dialog box function?

Process the wm_keydown message, where wparam is vk_return, that is, the carriage return message.

Case wm_keydwon:
{
If (vk_return = msg. wparam)
{
// Press ENTER
// Obtain the focus handle
Hwnd hfocus = getfocus ();
If (hfocus = hedit)
{
// Process your logic
}
}
}
Break;

 

 

2. my previous practice was to make a pretranslatemessage and sense MSG in it. It's just like if you press Enter, if you use pretranslatemessage to determine whether the carriage return function is not suitable for the entire DLG, how can this problem be solved if the carriage return function is received from the Edit Control?
What is the practice?

Rewrite the Edit Control and add the wm_keydown and wm_char processing functions to intercept common button messages, but neither can intercept the carriage return message.
We recommend that you use pretranslatemessage. For more information, see
Http://tech.163.com/05/0906/09/1SV5EEQF00091589.html
This is easier to write.
Http://hi.baidu.com/albertdiao/blog/item/3183ab47d0c0182f879473bf.html

 

3. Why does my vs2008 Dialog Box program have no pretranslatemessage message?
The dialog box-based MFC program generated by the Class Wizard is read in the message of the attribute. All start with WM. All are found. Not found... Khan...
What's going on?

Haha, not in the message of the class, but in the overrides of the class... In classview, isn't there two classes? Click the dialog box class. In the "properties" window, click the "Override" tab, which lists all functions that can be reloaded.

 

4. After the dialog box generated by Using MFC, use the ESC key to close the dialog box. My program does not want this function. How can I remove it?
I tried to intercept wm_char, wm_keydonwn, and wm_syskeydown, but none of them were triggered.

Do not remove the cancel at the beginning. Double-click it and add the oncancel () command to remove the parent class called in oncancel.
If you have deleted the button, you have to manually add this command, but the effect is the same.

 

5. Have you selected the want return in the text edit box attribute?

// Control the Keyboard Message
Bool csetupdlg: pretranslatemessage (MSG * PMSG)
{
If (PMSG-> message = um_return)
{
// Let the combo box respond and press enter to get the text
Getcomboedittext ();
}
// Process the carriage return message
If (PMSG-> message = wm_keydown & PMSG-> wparam = vk_return)
{
// Return to press enter as the tab key
PMSG-> wparam = vk_tab;
}

Return cdialog: pretranslatemessage (PMSG );
}
Es_wantreturn is only valid for the edit of multiple-line. We still use the wade_vc (my life is not controlled by the day) method to process it in the pretranslatemessage function.

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.