Some time ago, two friends asked me how to register VC and shield the global keyboard hotkeys. This is a piece of code I have previously written. To tell the truth, I feel that it is "trivial". I don't need to talk about the source code! (Which brother has other ideas and can communicate with each other ......)
1. Define the macro of the key message hotkey. You can add more
/*************************************** *******************************/<Br/> // virtualkey. hby: koma2009.08.22 <br/> // define the hotkey <br/> // http://blog.csdn.net/wangningyu </P> <p> /**************** **************************************** * **************/<br/>/* ctrl key combination (D, F2, F8, F9, F10, F11, F12) 6) <br/> /*********************************** ***********************************/<br /># define idh_hot14001 <br/> # define idh_hot24002 <br/> # define idh_hot34003 <br/> # define idh_hot44004 <br/> # define idh_hot54005 <br/> # define idh_hot64006 <br/> # define idh_hot194019 </P> <p> /************************* **************************************** * ******/<br/>/* Alt key combination (three keys for F1, F4, and F9) <br/> /*********************************** ***********************************/<br /># define idh_hot74007 <br/> # define idh_hot84008 <br/> # define idh_hot94009 </P> <p> /************* **************************************** *******************/
2. Register global hotkeys to shield them.
/*************************************** *******************************/<Br/> /* function Description: the MFC initialization function is used to register the global hotkey <br/>/* parameter number: None <br/>/* return value: true is returned for success, false is returned for failure <br/>/*: koma 2009.07.30 <br/> /******************************** **************************************** /<br/> bool chookkbdlg:: oninitdialog () <br/>{< br/> cdialog: oninitdialog (); <br/> // if you want to add other key combinations, you need to add them in virtualkey. h add record <br/> // Add a line of code to oninitdialog and pretranslatemessage </P> <p> // CTRL <D, F2, F8, F9, F10, F11, and F12) <br/> registerhotkey (this-> getsafehwnd (), idh_hot1, mod_control, vk_f2); <br/> registerhotkey (this-> getsafehwnd (), idh_hot2, mod_control, vk_f8 ); <br/> registerhotkey (this-> keys (), idh_hot3, mod_control, vk_f9); <br/> registerhotkey (this-> getsafehwnd (), idh_hot4, mod_control, vk_f10 ); <br/> registerhotkey (this-> keys (), idh_hot5, mod_control, vk_f11); <br/> registerhotkey (this-> getsafehwnd (), idh_hot6, mod_control, vk_f12 ); <br/> registerhotkey (this-> getsafehwnd (), idh_hot19, mod_control, 'D '); </P> <p> // Alt key combinations (F1, F4, and F9) <br/> registerhotkey (this-> getsafehwnd (), idh_hot7, mod_alt, vk_f1); <br/> registerhotkey (this-> getsafehwnd (), idh_hot8, mod_alt, vk_f4); <br/> registerhotkey (this-> getsafehwnd (), idh_hot9, mod_alt, vk_f9); </P> <p> return true; // return true unless you set the focus to a control <br/>}
3. It is a bit like "occupying the pitfalls without pulling s". Directly filter out this message...
/*************************************** *******************************/<Br/> /* function Description: MFC virtual function, used to shield hotkeys <br/>/* parameter number: None <br/>/* return value: returns true if not blocked. <br/>/*: koma 2009.07.30 <br/> /******************************** **************************************** /<br/> bool chookkbdlg:: pretranslatemessage (MSG * PMSG) <br/>{< br/> // todo: add your specialized code here and/or call the base class <br/> I F (PMSG-> message = wm_hotkey) <br/>{< br/> switch (PMSG-> wparam) <br/>{< br/> case idh_hot1: <br/> case idh_hot2: <br/> case idh_hot3: <br/> case idh_hot4: <br/> case idh_hot5: <br/> case idh_hot6: <br/> case idh_hot7: <br/> case idh_hot8: <br/> case idh_hot9: <br/> // MessageBox (_ T ("this type of key combination is strictly prohibited! "); <Br/> // true indicates that the message is no longer delivered to the Message Queue. <br/> setdlgitemtext (idc_static1," don't press it, it's useless! "); <Br/> return true; <br/>}< br/> return cdialog: pretranslatemessage (PMSG); <br/>}
4. VC source code
The program passes the test under vc6.0 + win SP3. If you have any questions, you can send an email!
Network Disk download: http://www.rayfile.com/files/8dda00ca-8ef9-11de-9b83-0014221b798a/
Http://download.csdn.net/source/1596580 (csdn)
QQ: 27746072 E-mail: koma0769@vip.qq.com