MSDN中的一个示例代码,步骤就是RegisterHotKey注册热键,然后响应WM_HOTKEY消息

@1:这个是系统热键

  1. #include "stdafx.h"
  2. int _cdecl _tmain (
  3. int argc,
  4. TCHAR *argv[])
  5. {
  6. if (RegisterHotKey(
  7. NULL,
  8. 1,
  9. MOD_ALT | MOD_NOREPEAT,
  10. 0x42))  //0x42 is 'b'
  11. {
  12. _tprintf(_T("Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag\n"));
  13. }
  14. MSG msg = {0};
  15. while (GetMessage(&msg, NULL, 0, 0) != 0)
  16. {
  17. if (msg.message == WM_HOTKEY)
  18. {
  19. _tprintf(_T("WM_HOTKEY received\n"));
  20. }
  21. }
  22. return 0;
  23. }

@2:软件非全局的快捷键,Keyboard Accelerators(键盘加速器)

  1. FontAccel ACCELERATORS
  2. {
  3. VK_F5,  IDM_REGULAR,    VIRTKEY
  4. "B",    IDM_BOLD,       CONTROL, VIRTKEY
  5. "I",    IDM_ITALIC,     CONTROL, VIRTKEY
  6. "U",    IDM_ULINE,      CONTROL, VIRTKEY
  7. }
    1. HWND hwndMain;      // handle to main window
    2. HANDLE hinstAcc;    // handle to application instance
    3. int WINAPI WinMain(HINSTANCE hinst, HINSTANCE hinstPrev, LPSTR lpCmdLine, int nCmdShow)
    4. {
    5. MSG msg;            // application messages
    6. BOOL bRet;          // for return value of GetMessage
    7. HACCEL haccel;      // handle to accelerator table
    8. // Perform the initialization procedure.
    9. // Create a main window for this application instance.
    10. hwndMain = CreateWindowEx(0L, "MainWindowClass",
    11. "Sample Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,
    12. CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,
    13. hinst, NULL );
    14. // If a window cannot be created, return "failure."
    15. if (!hwndMain)
    16. return FALSE;
    17. // Make the window visible and update its client area.
    18. ShowWindow(hwndMain, nCmdShow);
    19. UpdateWindow(hwndMain);
    20. // Load the accelerator table.
    21. // haccel = LoadAccelerators(hinstAcc,  MAKEINTRESOURCE(IDC_*)) ;IDC_*为.RC资源中的加速器ID
    22. haccel = LoadAccelerators(hinstAcc, "FontAccel");
    23. if (haccel == NULL)
    24. HandleAccelErr(ERR_LOADING);     // application defined
    25. // Get and dispatch messages until a WM_QUIT message is
    26. // received.
    27. while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
    28. {
    29. if (bRet == -1)
    30. {
    31. // handle the error and possibly exit
    32. }
    33. else
    34. {
    35. // Check for accelerator keystrokes.
    36. if (!TranslateAccelerator(
    37. hwndMain,  // handle to receiving window
    38. haccel,    // handle to active accelerator table
    39. &msg))         // message data
    40. {
    41. TranslateMessage(&msg);
    42. DispatchMessage(&msg);
    43. }
    44. }
    45. }
    46. return msg.wParam;
    47. }
    48. LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam)
    49. {
    50. BYTE fbFontAttrib;        // array of font-attribute flags
    51. static HMENU hmenu;       // handle to main menu
    52. switch (uMsg)
    53. {
    54. case WM_CREATE:
    55. // Add a check mark to the Regular menu item to
    56. // indicate that it is the default.
    57. hmenu = GetMenu(hwndMain);
    58. CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND |
    59. MF_CHECKED);
    60. return 0;
    61. case WM_COMMAND:
    62. switch (LOWORD(wParam))
    63. {
    64. // Process the accelerator and menu commands.
    65. case IDM_REGULAR:
    66. case IDM_BOLD:
    67. case IDM_ITALIC:
    68. case IDM_ULINE:
    69. // GetFontAttributes is an application-defined
    70. // function that sets the menu-item check marks
    71. // and returns the user-selected font attributes.
    72. fbFontAttrib = GetFontAttributes(
    73. (BYTE) LOWORD(wParam), hmenu);
    74. // SetFontAttributes is an application-defined
    75. // function that creates a font with the
    76. // user-specified attributes the font with
    77. // the main window's device context.
    78. SetFontAttributes(fbFontAttrib);
    79. break;
    80. default:
    81. break;
    82. }
    83. break;
    84. // Process other messages.
    85. default:
    86. return DefWindowProc(hwndMain, uMsg, wParam, lParam);
    87. }
    88. return NULL;
    89. }

http://blog.csdn.net/x356982611/article/details/16341797

RegisterHotKey注册热键,然后响应WM_HOTKEY消息的更多相关文章

  1. 用RegisterHotKey注册系统热键

    函数功能:该函数定义一个系统范围的热键. 函数原型:BOOL RegisterHotKey(HWND hWnd,int id,UINT fsModifiers,UINT vk): 参数: hWnd:接 ...

  2. WPF注册热键后处理热键消息(非winform方式)

    由于最近在做wpf版的截图软件,在处理全局热键的时候,发现国内博客使用的都是winform窗体的键盘处理方式,此方式需要使用winform的动态库,如此不协调的代码让我开始在github中寻找相关代码 ...

  3. RegisterHotKey注册快捷键

    RegisterHotKey的具体使用方使用方法如下: BOOL   RegisterHotKey( HWND   hWnd,         //响应该热键的窗口句柄 Int   id,       ...

  4. WinForm和WPF中注册热键

    由于.Net没有提供专门的类库处理热键,所以需要直接调用windows API来解决. HotKey为.NET调用Windows API的封装代码,主要是RegisterHotKey和Unregist ...

  5. MFC注册热键

    注册热键. 当用户点击注册的快捷键时,做出相应的响应. 定义 ALT+M键为测量按钮响应函数: 头文件中定义: #define ID_HOTKEY1 10001 在初始化函数中加入注册热键函数: if ...

  6. 离奇失踪的WM_HOTKEY消息--浅析WIN32消息队列

    故事的开端有些平淡,眼红于XXX小程序,认为写完该程序就有了和心仪的妹子多相处的机会,必须搞,必须酷,按钮不能有,界面得隐藏,这就想到了全局快捷键. 注册调用RegisterHotKey(m_hWnd ...

  7. MFC 给对话框注册热键

    在头文件中添加: //}}AFX_MSGafx_msg LRESULT OnHotKey(WPARAM wParam,LPARAM lParam);//(此行为加入的)BEGIN_MESSAGE_MA ...

  8. delphi注册热键方法(一)

    uses windows,menus; ..... //声明 HotKey_Key: Word; HotKey_Shift: Word; procedure WMHotKey(var msg : Tm ...

  9. 【WinForm程序】注册热键快捷键切换

    重写DefWndProc事件 #region Window 消息捕获 const int WM_COPYDATA = 0x004A; public struct COPYDATASTRUCT { pu ...

随机推荐

  1. 四大流行的jdbc连接池之C3P0篇

    C3P0是一个开放源代码的JDBC连接池,它在lib目录中与Hibernate一起发布,包括了实现jdbc3和jdbc2扩展规范说明的Connection 和Statement 池的DataSourc ...

  2. 双绞线的制作,T568A线序,T568B线序

    双绞线的制作 1.1 实验目的 双绞线是组建局域网时常常使用的通信传输介质,通过本实验,让学生学会制作双绞线. 1.2 实验任务 (1)了解双绞线的特性及屏蔽与非屏蔽双绞线的区别. (2)了解EIA/ ...

  3. UVA 10911 Forming Quiz Teams(dp + 集合最优配对问题)

    4th IIUC Inter-University Programming Contest, 2005 G Forming Quiz Teams Input: standard input Outpu ...

  4. 根据li标签 查找class="alcw4 alcw41"对应的值

    jrhmpt01:/root/lwp/0526# cat a2.pl use LWP::UserAgent; use DBI; use POSIX; use Data::Dumper; use HTM ...

  5. 基于visual Studio2013解决面试题之0503取最大数字字符串

     题目

  6. 基于visual Studio2013解决C语言竞赛题之1065二维排序

        题目 解决代码及点评 /* 功能:二维数组排序.设有4×5的数组M,通过排序使 M[1][1]≤M[1][2]≤...≤M[1][5]≤M[2][1]≤M[2][2]≤...≤ ...

  7. 两个栈实现队列+两个队列实现栈----java

                                               两个栈实现队列+两个队列实现栈----java 一.两个栈实现一个队列 思路:所有元素进stack1,然后所有出s ...

  8. Logistic Regression(逻辑回归)(一)基本原理

    (整理自AndrewNG的课件,转载请注明.整理者:华科小涛@http://www.cnblogs.com/hust-ghtao/) 虽然叫做“回归”,但是这个算法是用来解决分类问题的.回归与分类的区 ...

  9. .Net 4.0特性 Tuple元组

    Tuple 字面意思:元组.是.net4.0增加的新特性,是干什么的呢?总结一句,个人觉得这个东西 就是用来在有返回很多种类型的值时可以用到.它提供了8种类型的Tuple,直接看最复杂的那种(其实不是 ...

  10. CButtonEx的实现

    要想修改CButton类按钮背景颜色和文字颜色,必须利用自绘方法对按钮进行重新绘制.这可以通过定义一个以CButton为基类的新按钮类来实现.以下为具体的实现方法: 方法一: 加入一个新类,类名:CB ...