为菜单增加热键
来源:互联网 发布:mac 安装软件权限设置 编辑:程序博客网 时间:2024/05/19 22:00
Using MFC in C++ Part 2: Menus - Adding accelerator keys
(Page 8 of 9 )
Accelerator keys allow a user to accessmenu items quicker by pressing a pre-defined combination of keys,instead of having to click on a specific menu item everytime it isrequred. An accelerator table, which contains a list accelerator keys,has the following structure:
[TableName] ACCELERATORS
{
KeyCode, Message Id [, Type, Options]
KeyCode, Message Id [, Type, Options]
KeyCode, Message Id [, Type, Options]
}
KeyCode: The KeyCode value should contain the key combination that will trigger the accelerator. The key code can be either:
Type: Can be either empty, VIRTKEY or ASCII, as described above.
Options: If CONTROL is specified, then the control keymust be pressed to trigger this accelerator key. If SHIFT is specified,then the shift key must be pressed to trigger this accelerator key. IfALT is specified, then the alt key must be pressed to trigger thisaccelerator key.
Adding an accelerator key for a menu item iseasy. To make the CMainWin::OnItem1() function execute when we pressthe Control key and the 1 (one) key, we would add the following code tothe end of our resource file, menu.rc:
MyKeys ACCELERATORS
{
"1", IDM_1, CONTROL, VIRTKEY
}
To trigger the CMainWin::OnItem2() function when we prress the F2 key, we would add this accelerator key:
VK_F2, IDM_2, VIRTKEY
To trigger our the CMainWin::OnItem3() function when we press the “3” key, we would add this accelerator key:
51, IDM_3, ASCII // 51 is the ascii code for the 3 key
Wecan’t actually use these accelerator keys yet. We need to load theaccelerator table. Double click on menu.cpp and add the following linejust before the end of the CMainWin constructor:
if(!LoadAccelTable("MyKeys"))
MessageBox("Couldn't load the accelerator table");
TheLoadAccelTable function is a member of the CFrameWnd class, accepts oneparameter, the name of the menu (in quotes), and returns a BOOL value.It loads our accelerator table into memory and sends the notificationmessages to our application when the right combination of keys ispressed.
To make sure our accelerator keys work, compile and runour app. First, press Cntl+1. Next, press the F2 key. Lastly, pressjust the “3” key. Each different OnItem function should be triggered.
To make our menu items display the accelerator keys they respond to, change the MENU table in menu.rc, like this:
MyMenu MENU
{
POPUP "Popup"
{
MENUITEM "Item One/tCntl+1", IDM_1
MENUITEM "Item Two/tF2", IDM_2
MENUITEM SEPARATOR
MENUITEM "Item Three/t3", IDM_3
}
}
(Page 8 of 9 )
Accelerator keys allow a user to accessmenu items quicker by pressing a pre-defined combination of keys,instead of having to click on a specific menu item everytime it isrequred. An accelerator table, which contains a list accelerator keys,has the following structure:
[TableName] ACCELERATORS
{
KeyCode, Message Id [, Type, Options]
KeyCode, Message Id [, Type, Options]
KeyCode, Message Id [, Type, Options]
}
KeyCode: The KeyCode value should contain the key combination that will trigger the accelerator. The key code can be either:
- Any alphanumeric character enclosed in quotes, such as “a”, “C”, “6”. These are case sensitive.
- The header file afxres.h defines several macrosfor “virtual keys”. Virtual key macros allow keys such as F1 and F2(VK_F1 to VK_F12) to be used as accelerator keys. If a virtual key isused as the key code, then the type should be set to VIRTKEY.
- The ASCII code number of a character. If an ASCII key code is used, then the type should be set to ASCII.
Type: Can be either empty, VIRTKEY or ASCII, as described above.
Options: If CONTROL is specified, then the control keymust be pressed to trigger this accelerator key. If SHIFT is specified,then the shift key must be pressed to trigger this accelerator key. IfALT is specified, then the alt key must be pressed to trigger thisaccelerator key.
Adding an accelerator key for a menu item iseasy. To make the CMainWin::OnItem1() function execute when we pressthe Control key and the 1 (one) key, we would add the following code tothe end of our resource file, menu.rc:
MyKeys ACCELERATORS
{
"1", IDM_1, CONTROL, VIRTKEY
}
To trigger the CMainWin::OnItem2() function when we prress the F2 key, we would add this accelerator key:
VK_F2, IDM_2, VIRTKEY
To trigger our the CMainWin::OnItem3() function when we press the “3” key, we would add this accelerator key:
51, IDM_3, ASCII // 51 is the ascii code for the 3 key
Wecan’t actually use these accelerator keys yet. We need to load theaccelerator table. Double click on menu.cpp and add the following linejust before the end of the CMainWin constructor:
if(!LoadAccelTable("MyKeys"))
MessageBox("Couldn't load the accelerator table");
TheLoadAccelTable function is a member of the CFrameWnd class, accepts oneparameter, the name of the menu (in quotes), and returns a BOOL value.It loads our accelerator table into memory and sends the notificationmessages to our application when the right combination of keys ispressed.
To make sure our accelerator keys work, compile and runour app. First, press Cntl+1. Next, press the F2 key. Lastly, pressjust the “3” key. Each different OnItem function should be triggered.
To make our menu items display the accelerator keys they respond to, change the MENU table in menu.rc, like this:
MyMenu MENU
{
POPUP "Popup"
{
MENUITEM "Item One/tCntl+1", IDM_1
MENUITEM "Item Two/tF2", IDM_2
MENUITEM SEPARATOR
MENUITEM "Item Three/t3", IDM_3
}
}
- 为菜单增加热键
- c++ builder 如何为application 增加热键(快捷键)
- 【ahk】为QQ增加激活主面板的热键
- [AHK]为通达信标记文字窗口的按钮增加热键
- 为右键增加菜单项目
- 为NSView增加右键菜单
- 【ahk】为招商证券通达信版增加热键显隐跳空缺口,热键ctrl+
- Launcher2中热键修改为打开“设置”菜单
- 为右键菜单增加dos命令行功能
- 为JSDoc3文档增加导航菜单
- [AHK]为通达信增加打开关闭交易窗口的功能热键
- VC菜单热键、应用程序热键和系统级热键。
- 注册热键完善,增加容器,可以增加删除热键
- VC编程增加热键功能
- 为FoxPro中的弹出式菜单增加自学习功能
- 为ASP.NET 2.0菜单控件增加target属性
- 为FoxPro中的弹出式菜单增加自学习功能
- 为ASP.NET2.0菜单控件增加target属性
- 一个简单的SharePoint表单库 + InfoPath应用
- CSDN Blog电子报今天统计数据
- 深入剖析JSP和Servlet对中文的处理
- 程序员短命速成 --谈国内目前的人员外派模式(一)
- sql
- 为菜单增加热键
- 常见文件扩展名和它们的说明
- Visual Basic 9.0 前沿播报·动态篇 动态标识符
- C语言基础知识总结(1)-基本数据类型
- 《暗黑》同名小说——再也回不去(连载1)
- SqlServer数据库实现图片存入与读取
- log4j的使用
- Visual C++ 6.0技术内幕笔记(一)
- 又开始了漫长的学习生活