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:
- 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 .
- 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.
//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