zoukankan      html  css  js  c++  java
  • RegisterHotKey注册热键,然后响应WM_HOTKEY消息

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

    @1:这个是系统热键

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    1. #include "stdafx.h"  
    2.   
    3. int _cdecl _tmain (  
    4.     int argc,   
    5.     TCHAR *argv[])  
    6. {             
    7.     if (RegisterHotKey(  
    8.         NULL,  
    9.         1,  
    10.         MOD_ALT | MOD_NOREPEAT,  
    11.         0x42))  //0x42 is 'b'  
    12.     {  
    13.         _tprintf(_T("Hotkey 'ALT+b' registered, using MOD_NOREPEAT flag "));  
    14.     }  
    15.    
    16.     MSG msg = {0};  
    17.     while (GetMessage(&msg, NULL, 0, 0) != 0)  
    18.     {  
    19.         if (msg.message == WM_HOTKEY)  
    20.         {  
    21.             _tprintf(_T("WM_HOTKEY received "));              
    22.         }  
    23.     }   
    24.    
    25.     return 0;  
    26. }  

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

    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
    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. }  
    [cpp] view plain copy
     
     在CODE上查看代码片派生到我的代码片
      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.    
      9.     // Perform the initialization procedure.   
      10.    
      11.     // Create a main window for this application instance.   
      12.    
      13.     hwndMain = CreateWindowEx(0L, "MainWindowClass",   
      14.         "Sample Application", WS_OVERLAPPEDWINDOW, CW_USEDEFAULT,   
      15.         CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL,   
      16.         hinst, NULL );   
      17.    
      18.     // If a window cannot be created, return "failure."   
      19.    
      20.     if (!hwndMain)   
      21.         return FALSE;   
      22.    
      23.     // Make the window visible and update its client area.   
      24.    
      25.     ShowWindow(hwndMain, nCmdShow);   
      26.     UpdateWindow(hwndMain);   
      27.    
      28.     // Load the accelerator table.   
      29.     // haccel = LoadAccelerators(hinstAcc,  MAKEINTRESOURCE(IDC_*)) ;IDC_*为.RC资源中的加速器ID   
      30.     haccel = LoadAccelerators(hinstAcc, "FontAccel");   
      31.     if (haccel == NULL)   
      32.         HandleAccelErr(ERR_LOADING);     // application defined   
      33.    
      34.     // Get and dispatch messages until a WM_QUIT message is   
      35.     // received.   
      36.    
      37.     while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)  
      38.     {  
      39.         if (bRet == -1)  
      40.         {  
      41.             // handle the error and possibly exit  
      42.         }  
      43.         else  
      44.         {   
      45.             // Check for accelerator keystrokes.   
      46.        
      47.             if (!TranslateAccelerator(   
      48.                     hwndMain,  // handle to receiving window   
      49.                     haccel,    // handle to active accelerator table   
      50.                     &msg))         // message data   
      51.             {  
      52.                 TranslateMessage(&msg);   
      53.                 DispatchMessage(&msg);   
      54.             }   
      55.         }   
      56.     }  
      57.     return msg.wParam;   
      58. }   
      59.    
      60. LRESULT APIENTRY MainWndProc(HWND hwndMain, UINT uMsg, WPARAM wParam, LPARAM lParam)   
      61. {   
      62.     BYTE fbFontAttrib;        // array of font-attribute flags   
      63.     static HMENU hmenu;       // handle to main menu   
      64.    
      65.     switch (uMsg)   
      66.     {   
      67.         case WM_CREATE:   
      68.    
      69.             // Add a check mark to the Regular menu item to   
      70.             // indicate that it is the default.   
      71.    
      72.             hmenu = GetMenu(hwndMain);   
      73.             CheckMenuItem(hmenu, IDM_REGULAR, MF_BYCOMMAND |   
      74.                 MF_CHECKED);   
      75.             return 0;   
      76.    
      77.         case WM_COMMAND:   
      78.             switch (LOWORD(wParam))   
      79.             {   
      80.                 // Process the accelerator and menu commands.   
      81.    
      82.                 case IDM_REGULAR:   
      83.                 case IDM_BOLD:   
      84.                 case IDM_ITALIC:   
      85.                 case IDM_ULINE:   
      86.    
      87.                     // GetFontAttributes is an application-defined   
      88.                     // function that sets the menu-item check marks   
      89.                     // and returns the user-selected font attributes.   
      90.    
      91.                     fbFontAttrib = GetFontAttributes(   
      92.                         (BYTE) LOWORD(wParam), hmenu);   
      93.    
      94.                     // SetFontAttributes is an application-defined   
      95.                     // function that creates a font with the   
      96.                     // user-specified attributes the font with   
      97.                     // the main window's device context.   
      98.    
      99.                     SetFontAttributes(fbFontAttrib);   
      100.                     break;   
      101.    
      102.                 default:   
      103.                     break;   
      104.             }   
      105.             break;   
      106.    
      107.             // Process other messages.   
      108.    
      109.         default:   
      110.             return DefWindowProc(hwndMain, uMsg, wParam, lParam);   
      111.     }   
      112.     return NULL;   
      113. }  

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

  • 相关阅读:
    [转]Maven 初学+http://mvnrepository.com/
    比较IDEA与Eclipse
    [web] 使用Promise封装fetch实现网络超时,终止请求的功能
    [web] 理解和使用Promise.all和Promise.race
    [Web] How to Test React and MobX with Jest
    [Web 测试] Jest单元测试的几个指标
    [Web] 取消Promise
    [安全分析] 安全分析中的威胁情报(一)
    [Web] 深入理解现代浏览器
    [Web] HTML5新特性history pushState/replaceState解决浏览器刷新缓存
  • 原文地址:https://www.cnblogs.com/findumars/p/5342982.html
Copyright © 2011-2022 走看看