In MFC, the program based on SDI and MDI has a shortcut key resource by default. By default, you can directly create a ing between the shortcut key and the message to achieve the shortcut key of the program, by default, a dialog box-based program has no shortcut key resources. As a result, I will use two methods to solve this problem.
First, in the pretranslatemessage function, judge the keyboard's key message. For example, if the shortcut key is Ctrl + Q, we can check whether the ctrl key and Q of the key message are pressed at the same time.
// Use the pretranslatemessage method to determine whether Ctrl + q is pressed. Press the shortcut key to perform the corresponding operation.
If (PMSG-> message = wm_keydown & PMSG-> wparam = 'q' & isctrlpressed ()) { Afxmessagebox ("Ctrl + q pressed "); Return true; } |
Second, in the pretranslatemessage function, you can load the shortcut key resources and create a ing between the shortcut key and the message. First, insert the shortcut key resource and use the following code to load the resource.
Haccel HACC; HACC = loadaccelerators (afxgetapp ()-> m_hinstance, makeintresource (idr_accelerator1 ));
Translate the shortcut keys in the pretranslatemessage function. If the messages wm_keydown and wm_syskeydown are translated, true is returned.
BOOL CAccelerator2Dlg::PreTranslateMessage(MSG* pMsg) {// TODO: Add your specialized code here and/or call the base class int iResult; // For wm_keydown messages and wm_syskeydown messages, the translation shortcut key switch(pMsg->message){case WM_KEYDOWN: case WM_SYSKEYDOWN: iResult = TranslateAccelerator(m_hWnd,hAcc,pMsg); // If the translation shortcut is successful, return trueif (iresult) return true; } return CDialog::PreTranslateMessage(pMsg); } |