mfc-Registration Hotkey

Source: Internet
Author: User

0. Test environment

VS2015 Professional Edition, dialog-based MFC program

1, MFC project Settings

Class Wizard, message->wm_hotkey-> Add handler, OK or edit code

The following code is generated by the MFC project after execution

 // -------------------Mfc_testdlg.h  afx_msg  void  Onhotkey (UINT Nhotkeyid, uint nKey1, uint nKey2); //  Declares the message handler function  // -------------------Mfc_testdlg.cpp  On_wm_hotkey () //  message map macros, binding messages and corresponding handler functions . Span style= "color: #0000ff;" >void  Cmfc_testdlg::onhotkey (UINT Nhotkeyid, uint nKey1, uint nKey2) // Span style= "color: #008000;" > implement message handler  { //  TODO: Add the message Handler code here and/or call the default value   Cdialogex::onhotkey (Nhotkeyid, NKey1, NKey2);} 
2. Add Hotkey Registration/Cancellation Code2.1. Hotkey Registration/Cancellation API
//defines a system-wide hot key.BOOL WINAPI RegisterHotKey (//https://msdn.microsoft.com/en-us/library/ms646309(v=vs.85). aspx_in_opt_ HWND hwnd,//window handle to receive Hotkey trigger event_in_intId//the ID of the hotkey, the application ID range [0X0000,0XBFFF]. DLL range [0XC000~0XFFFF]_in_ UINT Fsmodifiers,//declares Alt (Mod_alt), Ctrl (Mod_control), Shift (Mod_shift), WIN key (Mod_win), mod_norepeat, or its combination in a key combination_in_ UINT VK//the code of the normal key (for example ' A '), other see https://msdn.microsoft.com/en-us/library/dd375731 (v=vs.85). aspx);//frees a hot key previously registered by the calling thread.BOOL WINAPI Unregisterhotkey (//https://msdn.microsoft.com/en-us/library/ms646327(v=vs.85). aspx_in_opt_ HWND hwnd,//window handle to receive Hotkey trigger event_in_intId//the ID of the hotkey 
2.2. Examples of API usage

The registration of the hotkey can be placed in the OnInitDialog () function or the Wm_create event handler, and the unregister can be placed in the Wm_destroy event handler function.

//wm_create Event Proc Handlerintcmfc_testdlg::oncreate (lpcreatestruct lpcreatestruct) {if(Cdialogex::oncreate (lpcreatestruct) = =-1)        return-1; //TODO: Add your own dedicated creation code here//Register Hotkey Ctrl + s (s)RegisterHotKey ( This->getsafehwnd (),1001, Mod_control,'S'); RegisterHotKey ( This->getsafehwnd (),1002, Mod_control,'s'); //Register Hotkey Ctrl + F1 (do not repeat, do not receive multiple times)RegisterHotKey ( This->getsafehwnd (),1003, Mod_control |mod_norepeat, vk_f1); return 0;}//wm_destroy Event Proc HandlervoidCmfc_testdlg::ondestroy () {Cdialogex::ondestroy (); //TODO: Add Message Handler code here//Unlock Hotkey RegistrationUnregisterhotkey ( This->getsafehwnd (),1001); Unregisterhotkey ( This->getsafehwnd (),1002); Unregisterhotkey ( This->getsafehwnd (),1003);}
3, modify the hotkey processing function
voidCmfc_testdlg::onhotkey (UINT Nhotkeyid, uint nKey1, uint nKey2) {//TODO: Add Message Handler code and/or call default values here    Switch(Nhotkeyid) { Case 1001:     Case 1002:        //proc Ctrl + S here         Break;  Case 1003:        //proc Ctrl + F1 here         Break; default:         Break; } cdialogex::onhotkey (Nhotkeyid, NKey1, NKey2);}

----------------------------------------------

Resources:

http://blog.csdn.net/wangjieest/article/details/6910166

Msdn

mfc-Registration Hotkey

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.