Original MFC dialog (dialog box) program Response Accelerator key (accelerator)
2014-8-6 Read 266 comments 0
Create a new dialog box program with the project name test, delete the default OK, Cancel, and static text box controls. Add a button, caption change to "open", id idc_open, double-click the button to add the response function, the response function is as follows:
void Ctestdlg::onbnclickedopen () {MessageBox ("Open is pressed");}
Click "Open" after the program is run to pop up a dialog box.
Here's the button to add the shortcut key Ctrl+o. Lower left corner switch to Resource view, then right-click on Project name > Add > Resource
Where Idr_accelerator1 is the accelerator key resource ID, right-click on it > Properties, open the "Shortcut node" window, you can modify the ID in this window, this ID in the following code will be used in the
In the middle of the window is the shortcut edit window, click on one row and the right side of the editor to display the line of response, notice the title of the right window
Switch to Class View, right-click on Ctestdlg > Add > Add Variable
Add the accelerator key initialization code to the Ctestdlg constructor, and note that the Idr_accelerator1 in the loadaccelerators () parameter is the same as the accelerator key resource ID mentioned earlier
Ctestdlg::ctestdlg (cwnd* pparent/*=null*/): CDialogEx (Ctestdlg::idd, pparent) {m_hicon = AFXGETAPP ()->LoadIcon ( IDR_MAINFRAME);//accelerator key initialization Hacckey=loadaccelerators (AfxGetInstanceHandle (), Makeintresource (Idr_accelerator1));}
Add PreTranslateMessage virtual function for Ctestdlg class, Project > Class Wizard
BOOL Ctestdlg::P retranslatemessage (msg* pMsg)
{if (TranslateAccelerator (m_hwnd,hacckey,pmsg)) {return true;} return CDialogEx::P retranslatemessage (PMSG);}
CTRL+F5, after the program runs, click Ctrl+o will pop up the dialog box, and click on the "open" effect.
This program responds to the "open" event by pressing the letter A in response to the keyboard message.
Project > Class Wizard
void Ctestdlg::onchar (UINT NChar, uint nrepcnt, uint nflags) {if (' a ' = = NChar) {Onbnclickedopen ();} Cdialogex::onchar (NChar, nrepcnt, nflags);}
However, it is not possible to respond to keystroke messages because keyboard messages are intercepted in the dialog box program. You also need to resend the keyboard message with SendMessage in the PreTranslateMessage (msg*pmsg) function just now. The modified PreTranslateMessage (msg*pmsg) function is as follows
BOOL Ctestdlg::P retranslatemessage (msg* pMsg) {if (TranslateAccelerator (m_hwnd,hacckey,pmsg)) {return true;} SendMessage (Pmsg->message,pmsg->wparam,pmsg->lparam); return 0;//return CDialogEx::P retranslatemessage ( PMSG);}
This allows the program to respond to keyboard messages. Run the program, press A or ctrl+o will pop up the dialog box.
Summarize
To use the accelerator key in the dialog box:
1. Add the accelerator resource, bind the control ID and the shortcut key, where the control ID can also make the menu ID, if the accelerator key is added to the menu.
2. Add a variable of type haccel, assuming the variable name is Hacckey, initialize the variable in the constructor or initialization function, Hacckey=loadaccelerators (AfxGetInstanceHandle (), Makeintresource (Idr_accelerator1));//This idr_accelerator1 is the same as the accelerator resource ID you created in step 1
3. Add the PreTranslateMessage function, which captures the accelerator key through the following statement
if (TranslateAccelerator (m_hwnd,hacckey,pmsg)) {
return true;
}
The key point of the dialog box program Response OnChar is to resend the keyboard message using SendMessage in the PreTranslateMessage function. SendMessage (Pmsg->message,pmsg->wparam,pmsg->lparam);
Original MFC dialog (dialog box) program Response Accelerator key (accelerator)