MFC Controls Add hotkeys

Source: Internet
Author: User

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.

  1. In the header file of the dialog box:

  2. afx_msg LRESULT Onhotkey (WPARAM wparam,lparam LPARAM); afx_msg void OnDestroy ();
  3. In the dialog box CPP file:

  4. Begin_message_map (Cyourdlg, CDialog) on_message (Wm_hotkey,onhotkey) On_wm_destroy () End_message_map ()
  5. Hotkey Corresponding mappings:

  6. LRESULT Cyourdlg::onhotkey (WPARAM wparam,lparam LPARAM) {if (wparam==idc_xxx) onyourfunction ();//todo:add functi On return 0; }
  7. Register Hotkey:

  8. BOOL Cyourdlg::oninitdialog () {:: RegisterHotKey (GetSafeHwnd (), idc_xxx, Mod_alt, ' D ');//Register Hotkey Alt+d (D must be capitalized)}
  9. Remember to destroy:

  10. 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

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.