MFC Controls Add hotkeys

Source: Internet
Author: User


  
 
  1. MFC 控件添加热键 2014-12-24 14:28:47
  2. 标签:C++ MFC 控件 热键使用
  3. Span class= "pun" to mfc Span class= "PLN" >local global where local function is valid when the current program has focus (activated), and global local global menu button
  4. 自然而然,创建热键的方法也有多种,不同的创建方法创建的热键作用范围不一定相同。应该根据需求合理的选择自己的方法。
  5. 方法一:
  6. 打开对话框资源,选择指定控件的属性-->在caption项中你定义的名字后添加(&Y)。这样就可以实现热键。其中Y表示你要制定的快捷按键,我选的是Y,按下ALT+Y即可执行这个控件。
  7. 该方式热键只有当FOCUS在控件上时才起作用,算是局部热键。
  8. 方法二:
  9. 1. 在资源视图中添加资源,选择"Accelerator"选项新建即可;
  10. 2. Span class= "PLN" > Open new folder under "Idr_accelerator1" id id id number, modifier Select whether you want to use a key combination, such as: alt alt + shift and so on, select the type of virtual key you want to use in the key, select vk ascii form.
  11. 3. 在你的对话框的头文件中添加快捷键变量,如HACCEL m_hAcc;
  12. 4. 在你的对话框的源文件的OnInitDialog函数中加载快捷键,如:
  13. 1 HACCEL m_hAcc=LoadAccelerators(AfxGetInstanceHandle(),MAKEINTRESOURCE(IDR_ACCELERATOR1));
  14. 5. 重载函数PreTranslateMessage,使用类向导,重载虚函数PreTranslateMessage,如:
  15. 12345678910 BOOL CVideoMonitorDlg::PreTranslateMessage(MSG* pMsg)
  16. {
  17. if (WM_KEYFIRST<=pMsg->message&&pMsg->message<= WM_KEYLAST)
  18. {
  19. HACCEL hAccel=m_hAcc;
  20. if (hAccel && ::TranslateAccelerator(m_hWnd, hAccel, pMsg))
  21. return TRUE;
  22. }
  23. return CDialogEx::PreTranslateMessage(pMsg);
  24. }
  25. 6. 通过以上5个步骤基本上即可对工程中的菜单或者按钮添加热键。
  26. 方法三:
  27. 使用WM_HOTKEY。
  28. 在对话框头文件中:
  29. 12 afx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);
  30. afx_msg void OnDestroy();
  31. 在对话框CPP文件中:
  32. 1234 BEGIN_MESSAGE_MAP(CYourDlg, CDialog)
  33. ON_MESSAGE(WM_HOTKEY,OnHotKey)
  34. ON_WM_DESTROY()
  35. END_MESSAGE_MAP()
  36. hotkey对应映射:
  37. 1234567 LRESULT CYourDlg::OnHotKey(WPARAM wParam,LPARAM lParam)
  38. {
  39. if(wParam==IDC_XXX)
  40. OnYourFunction();
  41. //ToDo: add function
  42. return 0;
  43. }
  44. 注册热键:
  45. 1234 BOOL CYourDlg::OnInitDialog()
  46. {
  47. ::RegisterHotKey(GetSafeHwnd(), IDC_XXX, MOD_ALT, ‘D‘);//注册热键alt+D(D必须大写)
  48. }
  49. 记得销毁:
  50. 1234 void CYourDlg::OnDestroy()
  51. {
  52. ::UnregisterHotKey(GetSafeHwnd(),IDC_XXX);//销毁热键
  53. }
  54. 另外一篇参考:http://lty2154216.blog.163.com/blog/static/17982629320117129491666/



From for notes (Wiz)

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.