Hotkeys include menu hotkeys, application hotkeys, and system-level hotkeys.
I. Menu hotkeys:
Include & X directly in the menu. X is any character key you want to use.
Example: The caption of the about menu item can be written as follows: "& about" or "about (& A)" indicates that A is used to call this menu item, which is case-insensitive.
2. Application hotkeys:
<1> non-dialog box-based:
Add the corresponding ID and buttons to the accelerator of the resource view.
<2> Based on the modal dialog box:
See the Microsoft Knowledge Base q222829 and q100770. The following is a key part of the translation q222829:
1. generate a new mfc mdi application named ctestapp.
2. Add a button in the aboutbox dialog box with ID idc_button1.
3. Double-click the button to add the corresponding function and insert the following code:
Afxmessagebox ("hello ");
4. Add the accelerator resource idr_accelerator1.
5. Add a hotkey to the accelerator table to associate F5 with "idc_button1.
6. Add the member variable m_hacceltable for caboutbox:
Haccel m_hacceltable;
7. Set the initial value of m_hacceltable in caboutbox:
M_hacceltable = loadaccelerators (AfxGetInstanceHandle (), makeintresource (idr_accelerator1 ));
8. Use the Wizard to add the pretranslatemessage function of caboutdlg.
9. In caboutbox: pretranslatemessage (), add the following code:
- Bool caboutdlg: pretranslatemessage (MSG * PMSG ){
- If (m_hacceltable ){
- If (: translateaccelerator (m_hwnd, m_hacceltable, PMSG )){
- Return (true );
- }
- }
- Return cdialog: pretranslatemessage (PMSG );
- }
10. Compile and run the ctestapp.
Iii. System-level hotkeys:
1. Add afx_msg lresult onhotkey (wparam, lparam) in. h );
2. Add a function in. cpp:
- Lresult cmydlg: onhotkey (wparam, lparam)
- {
- // Todo: add your code here
- Return 0;
- }
Add message ing between begin_message_map and end_message_map:
On_message (wm_hotkey, onhotkey)
3. register the hotkey when creating/entering the program:
- Registerhotkey (m_hwnd, 1001, mod_alt, 0x41); // use Alt + A as the shortcut key. The second parameter must be unique.
4. Cancel the hotkey when destroying/exiting the program:
- > Unregisterhotkey (m_hwnd, 1001 );