MFC frequently asked questions and workarounds (1) _mfc Following this edit box press ENTER after the window exits

Source: Internet
Author: User

Here is the main introduction of the solution to meet this method, there may be a number of solutions, but here only a valid one, here will not explain the reasons for the problem and why this solution, want to know more Baidu, write this is mainly to prevent later forget, do a simple note.

Issue:MFC dialog box Program-text edit box control, press ENTER after window exit


Workaround: overload virtual function PreTranslateMessage ();

Specific operation:

  1. Right -click on the Resource Editor of the dialog box, open the Class Wizard , tap virtual function , search for virtual function pretranslatemessage, select PreTranslateMessage, click Add function on the right, then click Edit Code .
  2. function, enter the following code.
    //Override virtual function PreTranslateMessage () for Enter key confirmationBOOL Clogindlg::P retranslatemessage (msg*PMSG) {    //TODO: Add private code here and/or call base class    if(Pmsg->message = = Wm_keydown && Pmsg->wparam = =Vk_return) {        if(GetFocus ()->getdlgctrlid () = = Idc_edit_password)//Press ENTER if the current focus is on the control you expect        {            //What you want to do if you press ENTER to focus on the control you wantOnbnclickedbtnok (); }        returnTRUE; }    if(Pmsg->message = = Wm_keydown && Pmsg->wparam = =vk_escape)returnTRUE; returnCDialogEx::P retranslatemessage (PMSG);}

    PS: The above code is my test code, the function is to execute the login button after the Password box enter the operation, if only need to block the return, only to be //TODO: Here to add special code and/or call the base class code below the line of the copy in. if (GetFocus ()->getdlgctrlid () = = Idc_edit_password) This if also can not, through this method, of course, can also kill the ESC key pressed problem, The following code is directly shielded from the Enter and exit keys.

  3. //Override virtual function PreTranslateMessage () for Enter key confirmationBOOL Clogindlg::P retranslatemessage (msg*PMSG) {    //TODO: Add private code here and/or call base class    if(Pmsg->message = = Wm_keydown&&pmsg->wparam = =vk_escape) {        //If the message is a keyboard press event and the ESC key executes the following code (do nothing, you can add the desired code yourself)        returnTRUE; }    if(Pmsg->message = = Wm_keydown && Pmsg->wparam = =Vk_return) {        //If the message is a keyboard press event, and is the Entert key, execute the following code (do nothing, you can add the desired code yourself)        returnTRUE; }    returnCDialogEx::P retranslatemessage (PMSG);}

MFC frequently asked questions and workarounds (1) _mfc Following this edit box press ENTER after the window exits

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.