How to Create a hotkey in MFC

Source: Internet
Author: User

First, use the MFC Application Wizard to generate a dialog box-based application.

 

Then, register the hotkey in the OnInitDialog member function of the dialog box class.

: RegisterHotKey (m_hWnd, 001, MOD_ALT, VK_ESCAPE );

: Global identifier;

M_hwnd dialog box handle;

001 hotkey ID, which is an integer value and can be defined at will or by a macro;

MOD_ALT indicates that it is triggered by the combination key (ALT + VK). The optional values include MOD_SHIFT, MOD_WIN, and MOD_NOREPEAT. If it is set to NULL, the combination key is not used;

VK_ESCAPE is the virtual key code of the hotkey, which can be defined as needed. In this example, use the ESC key in the upper left corner of the keyboard.

 

Next, reload the PreTranslateMessage function (Note: This function cannot intercept messages sent by the SendMessage function ).

In VC6.0, right-click the dialog box class in the Class View and select Add virtual Function. Select PreTranslateMessage and reload it.

 

Finally, enter the following code in the function body to enable the pop-up message box function after pressing the hot key.

Switch (pMsg-> message)
{
Case WM_HOTKEY:
If (001 = pMsg-> wParam) MessageBox ("Welcome tohttp: // hi.baidu.com/darks00n /");
Break;
Default:
Break;
}

Message is a member of the MSG message structure, representing the message type. The message type of the hotkey is WM_HOTKEY.

WParam is an additional parameter. When this parameter value is equal to 001, our hotkeys are triggered. Multiple hotkeys are distinguished by this parameter.

 

OK! Press ALT + ESC at the same time after F5 executes the program. The message box is displayed successfully. Is it so easy to create a hotkey under MFC?




Author darks00n's secret base

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.