Usage of VC hotkeys and key combinations mfc c ++ hotkey wm_hotkey

Source: Internet
Author: User

The content described below is successfully debugged in the vc6.0 MFC program.

 

[1] use of hotkeys

It mainly uses two API functions: registerhotkey and unregisterhotkey. For details, see msdn. The edit box is used as an example to create cmyedit2, a derived class of cedit.

1) Respond to the wm_hotkey message

Registerhotkey can send wm_hotkey messages to the window. Therefore, wm_hotkey message ing is defined first.

Add afx_msg long onhotkey (wparam, lparam) to the. h file. The specific location is as follows:

 Protected: <br/> // {afx_msg (cmyedit2) <br/> afx_msg void ondestroy (); <br/> afx_msg int oncreate (lpcreatestruct ); <br/> //} afx_msg <br/> afx_msg long onhotkey (wparam, lparam); // manually add <br/> declare_message_map ()

Add on_message (wm_hotkey, onhotkey) to the. cpp file. The specific location is as follows:

Begin_message_map (cmyedit2, cedit) <br/> // {afx_msg_map (cmyedit2) <br/> on_wm_char () <br/> on_wm_destroy () <br/> on_wm_create () <br/> //} afx_msg_map <br/> on_message (wm_hotkey, onhotkey) // manually add <br/> end_message_map ()

Define the response function:

Long cmyedit2: onhotkey (wparam, lparam) <br/>{</P> <p> uint fumodifiers = (uint) loword (lparam ); // key-modifier flags <br/> uint uvirtkey = (uint) hiword (lparam ); // virtual-key code </P> <p> // determines the hotkey responded <br/> If (mod_control = fumodifiers & 'D' = ustmkey) <br/>{< br/> afxmessagebox (_ T ("you pressed the key combination Ctrl + D ")); <br/>}< br/> else if (mod_alt = fumodifiers & 'M' = u1_key) <br/>{< br/> afxmessagebox (_ T ("you pressed the combination key Alt + M ")); <br/>}< br/> else <br/> afxmessagebox (_ T ("you pressed the unknown hotkey"); </P> <p> return 0; <br/>}

 

2) Registration hotkey

Void cmyedit2: Init () <br/>{< br/> // this function can be used in presubclasswindow and oncreate; <br/> // if it is not used in the first two, you can go to cdialog: oninitdialog or <br/> // cview :: use </P> <p> assert (null! = Getsafehwnd ()); </P> <p> // register hotkey <br/> # ifdef _ debug // debug version <br/> int nret = registerhotkey (getsafehwnd (), m_nhotkeyid [0], mod_control, 'D'); // key Ctrl + D <br/> If (! Nret) <br/> afxmessagebox (_ T ("registerhotkey 0 false"); <br/> nret = registerhotkey (getsafehwnd (), m_nhotkeyid [1], mod_alt, 'M'); // The Hot Key Alt + M <br/> If (! Nret) <br/> afxmessagebox (_ T ("registerhotkey 1 false"); <br/> # else // release version <br/> registerhotkey (getsafehwnd (), m_nhotkeyid [0], mod_control, 'D'); // key Ctrl + D <br/> registerhotkey (getsafehwnd (), m_nhotkeyid [1], mod_alt, 'M '); // Hot Key Alt + M <br/> # endif <br/>}

3) unregister hotkey

Void cmyedit2: ondestroy () <br/>{< br/> cedit: ondestroy (); </P> <p> // unregister hotkey <br/> # ifdef _ debug <br/> int nret = unregisterhotkey (getsafehwnd (), m_nhotkeyid [0]); <br/> If (! Nret) <br/> afxmessagebox (_ T ("unregisterhotkey 0 false"); <br/> nret = unregisterhotkey (getsafehwnd (), m_nhotkeyid [1]); <br/> If (! Nret) <br/> afxmessagebox (_ T ("unregisterhotkey 1 false"); <br/> # else <br/> unregisterhotkey (getsafehwnd (), m_nhotkeyid [0]); <br/> unregisterhotkey (getsafehwnd (), m_nhotkeyid [1]); <br/> # endif </P> <p>}

 

Note: The position where Init () is used. Here, the isinit variable (bool value) is used to ensure that the init function is called only once.

Void cmyedit2: presubclasswindow () <br/>{< br/> // initialization <br/> If (! Isinit) <br/>{< br/> Init (); <br/> isinit = true; <br/>}</P> <p> cedit :: presubclasswindow (); <br/>}</P> <p> int cmyedit2: oncreate (maid) <br/>{< br/> If (cedit :: oncreate (lpcreatestruct) =-1) <br/> return-1; </P> <p> // initialization <br/> If (! Isinit) <br/>{< br/> Init (); <br/> isinit = true; <br/>}</P> <p> return 0; <br/>}< br/>

 

[2] (send with code) Send a key combination message

1) Send the wm_hotkey message

Wparam; <br/> lparam; </P> <p> // send the wm_hotkey message and press Ctrl + D. <br/> wparam = 0; <br/> lparam = makelong (mod_control, 'D'); <br/>: sendmessage (heditwnd, wm_hotkey, wparam, lparam );

 

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.