Implementation of VC/MFC shortcut keys

Source: Internet
Author: User

VC We often use the shortcut keys, there are a variety of shortcut keys, including Menu,button. There is also a distinction between local and global, where local functions are effective when the current program has the focus (activated), and global, which is effective at all times, and test local priority over global, That is, if the shortcut key for the active window overlaps the shortcut key of the inactive window, the current active window priority response.

Here the shortcut keys are divided into menu and button two kinds.

menu to add a local shortcut key is divided into two, one is directly associated with the menu item accelerator, the other is a custom accelerator key.

Zz:http://www.cnblogs.com/thankgoodness/articles/1136617.html

First: Add the shortcut key resource ID to the resource file accelerator Select the name of the menu item you want to associate and then set your shortcut. What, next? Add a Haccel haccel to the. h file and add an H to the OnInitDialog or initialization  Accel=::loadaccelerators (AfxGetInstanceHandle (), Makeintresource (Idr_menu_main)); The following argument is changed to the accelerator's resource file name.

Finally, in PreTranslateMessage (msg* pMsg), add the following:
if (:: TranslateAccelerator (GetSafeHwnd (), haccel,pmsg))
return true;


That way, just add the shortcut key to the accelerator resource file.
Note: The name of the shortcut key must be the same as the menu name to be able to respond. Now just add the OnCommand message processing to this menu item.


The second: or add the accelerator resource ID to the resource file accelerator define one yourself. And then set your shortcut key. Next ... is to define a shortcut key object in the. h file.
Haccel M_haccel;

And then in the. cpp file, the initial
M_haccel =:: Loadaccelerators (AfxGetInstanceHandle (), Makeintresource (Idr_accelerator1));
Idr_accelerator1 for your accelerated resource name. Note The accelerator ID that is not just defined.

Add the following code to the PreTranslateMessage message processing:
Saving shortcut keys is enabled
if (M_haccel!= NULL)
{
if (TranslateAccelerator (M_hwnd, M_haccel, PMSG))
return TRUE;
}

Add the following code to the OnCommand message processing:

Response accelerator Keys
Switch (LOWORD (wParam))
{
Case SHOW_DIAL0G://accelerator Key ID
//... Add a processing statement
Break
Case SHOW_DIALOG_02://accelerator Key ID
//... Add a processing statement
Break
}


menu to add a global shortcut key to the dialog box program for example:

To add a code to register a hotkey in OnInitDialog:
RegisterHotKey (GetSafeHwnd (), 1001,null,vk_f2);//f2 key
RegisterHotKey (GetSafeHwnd (), 1002,mod_alt, ' 1 ');//alt+1 key
In
Begin_message_map (Cxxxdlg, CDialog)
...
End_message_map ()
To add a Wm_hotkey message map to:
On_message (Wm_hotkey,&cxxxdlg::onhotkey)//shortcut key message map manually join
To add a Onhotkey declaration to a header file:
Protected
afx_msg lresult Onhotkey (WPARAM wparam,lparam LPARAM);//join manually.


Implementation code for Onhotkey in CPP:

Message mappings for corresponding shortcut keys
Lresult Cxxxdlg::onhotkey (WPARAM wparam,lparam LPARAM)
{
if (WParam ==1001)
{
MessageBox ("Hotkey F2 already pressed.") ");
Here you can add the action you want to perform, or call the button-mapped message function directly
}
else if (wparam==1002)
{
MessageBox ("Hotkey alt+1 already pressed.") ");
}
return 0;
}
Use when closing a dialog box
Unregisterhotkey (GetSafeHwnd (), 1001);/Logoff F2 key
Unregisterhotkey (GetSafeHwnd (), 1002);/Logoff alt+1 key
Log off the hotkey.

button to add a Locak shortcut can be directly in the button caption with &+ ' x ', then press Ctrl + ' x ' to press this button, play the role of shortcut keys, you can also learn the menu to add a local shortcut key of the second way, Add a handler function to the PreTranslateMessage, for example:

BOOL Cyourdlg::P retranslatemessage (msg* pMsg)
{
Todo:add your specialized code here and/or call the base class
BOOL bhandledmsg = FALSE;
Switch (pmsg->message)
{
Case WM_KEYDOWN:
{
Switch (Pmsg->wparam)
{
Case Vk_escape://esc Key
Bhandledmsg = TRUE;
Break
Case 13://Carriage return
Bhandledmsg = TRUE;
Break
Default:break;
}//switch (Pmsg->wparam)
}
Break
Default:break;
}//switch (pmsg->message)
Return (bhandledmsg?) True:cdialog::P retranslatemessage (PMSG));
}


button add global shortcut key to add global shortcut key with menu.

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.