Added the hotkey function in VC programming.

Source: Internet
Author: User

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:

  1. Bool caboutdlg: pretranslatemessage (MSG * PMSG ){
  2. If (m_hacceltable ){
  3. If (: translateaccelerator (m_hwnd, m_hacceltable, PMSG )){
  4. Return (true );
  5. }
  6. }
  7. Return cdialog: pretranslatemessage (PMSG );
  8. }

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:

  1. Lresult cmydlg: onhotkey (wparam, lparam)
  2. {
  3. // Todo: add your code here
  4. Return 0;
  5. }

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:

  1. 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:

  1. > Unregisterhotkey (m_hwnd, 1001 );

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.