Add the control hotkeys we want to the controls in MFC, before you start, it must be clear that the hotkey is divided into local and global, where local functions are active when the current program has focus (activated), and global, whenever it is active, Test local to take precedence over global, that is, if the shortcut key of the active window overlaps with the shortcut key of the inactive window, the current activation window will first respond. It also includes Menu,button.
Naturally, there are many ways to create hotkeys, and different creation methods create hotkeys that do not necessarily have the same range. It is appropriate to choose your own method according to your needs.
Method One:
Open the dialog resource, select the property of the specified control, and add (&y) after the name you defined in the caption key. This allows the hotkey to be implemented. Where y means the shortcut key you want to set, I choose Y, and you can press Alt+y to execute the control.
This mode hotkey only works when focus is on the control, which is considered a local hotkey.
Method Two:
1. Add the resources in the Resource view and select the "Accelerator" option to create new ones;
2. Open the new folder under "Idr_accelerator1" in edit, which includes ID, modifier, key, type. In the ID select the menu you want to add the shortcut to, the ID number of the button, the modifier to choose whether you want to use the key combination, such as: Alt, Alt+shift, and so on, choose what kind of virtual key you want to use, type choose VK form or ASCII form.
3. Add a shortcut key variable in the header file of your dialog box, such as Haccel M_HACC;
4. Load the shortcut key in the OnInitDialog function of the source file of your dialog box, such as:
Haccel m_hacc=loadaccelerators (AfxGetInstanceHandle (), Makeintresource (Idr_accelerator1));
5. Overloaded function PreTranslateMessage, using the Class wizard, overloading virtual function pretranslatemessage, such as:
BOOL Cvideomonitordlg::P retranslatemessage (msg* pMsg) {if (wm_keyfirst<=pmsg->message&&pmsg-> message<= wm_keylast) {haccel HACCEL=M_HACC; if (haccel &&:: TranslateAccelerator (M_hwnd, Haccel, PMSG)) return TRUE; } return CDialogEx::P retranslatemessage (PMSG);}
6. The above 5 steps can basically add a hotkey to the menu or button in the project.
Method Three:
Use Wm_hotkey.
In the header file of the dialog box:
afx_msg LRESULT Onhotkey (WPARAM wparam,lparam LPARAM); afx_msg void OnDestroy ();
In the dialog box CPP file:
Begin_message_map (Cyourdlg, CDialog) on_message (Wm_hotkey,onhotkey) On_wm_destroy () End_message_map ()
Hotkey Corresponding mappings:
LRESULT Cyourdlg::onhotkey (WPARAM wparam,lparam LPARAM) {if (wparam==idc_xxx) onyourfunction ();//todo:add functi On return 0; }
Register Hotkey:
BOOL Cyourdlg::oninitdialog () {:: RegisterHotKey (GetSafeHwnd (), idc_xxx, Mod_alt, ' D ');//Register Hotkey Alt+d (D must be capitalized)}
Remember to destroy:
void Cyourdlg::ondestroy () {:: Unregisterhotkey (GetSafeHwnd (), idc_xxx);//Destroy Hotkey}
Another piece of reference: http://lty2154216.blog.163.com/blog/static/17982629320117129491666/
MFC Controls Add hotkeys