Implementation of shortcut keys based on the dialog box program in MFC

Source: Internet
Author: User

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);
}

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.