zoukankan      html  css  js  c++  java
  • MFC-注册热键

    0、测试环境

    VS2015专业版,基于对话框的MFC程序,

    例子地址:http://pan.baidu.com/s/1qX9IRec

    1、MFC工程设置

    类向导->消息->WM_HOTKEY->添加处理程序->确定或编辑代码

    执行后MFC工程会生成下面的代码

    //-------------------MFC_TESTDlg.h
    
    afx_msg void OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2); //声明消息处理函数
    
    
    //-------------------MFC_TESTDlg.cpp
    ON_WM_HOTKEY()        //消息映射宏,绑定消息与相应处理函数
    
    void CMFC_TESTDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2) //实现消息处理函数
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
    
        CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
    }

    2、添加热键注册/取消代码

    2.1、热键注册/取消API

    //Defines a system-wide hot key.
    BOOL WINAPI RegisterHotKey(//https://msdn.microsoft.com/en-us/library/ms646309(v=vs.85).aspx
      _In_opt_ HWND hWnd,            //接收热键触发事件的窗口句柄
      _In_     int  id,                //热键的ID,应用程序ID范围 [0x0000,0xBFFF].DLL范围[0xC000~0xFFFF]
      _In_     UINT fsModifiers,    //声明组合键中的 Alt(MOD_ALT)、Ctrl(MOD_CONTROL)、Shift(MOD_SHIFT)、Win键(MOD_WIN)、MOD_NOREPEAT 或者其组合
      _In_     UINT vk                //普通按键的代码(例如'A'),其他见https://msdn.microsoft.com/en-us/library/dd375731(v=vs.85).aspx
    );
    
    //Frees a hot key previously registered by the calling thread.
    BOOL WINAPI UnregisterHotKey(//https://msdn.microsoft.com/en-us/library/ms646327(v=vs.85).aspx
      _In_opt_ HWND hWnd,            //接收热键触发事件的窗口句柄
      _In_     int  id                //热键的ID 

    2.2、API使用举例

    热键的注册可以放在OnInitDialog()函数或者WM_CREATE事件处理函数中,取消注册可以放在WM_DESTROY事件处理函数中。

    //WM_CREATE Event Proc Handler
    int CMFC_TESTDlg::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
        if (CDialogEx::OnCreate(lpCreateStruct) == -1)
            return -1;
    
        // TODO:  在此添加您专用的创建代码
        //注册热键 Ctrl + S(s)
        RegisterHotKey(this->GetSafeHwnd(), 1001, MOD_CONTROL, 'S');
        RegisterHotKey(this->GetSafeHwnd(), 1002, MOD_CONTROL, 's');
        //注册热键 Ctrl + F1(不重复,不会连续收到多次)
        RegisterHotKey(this->GetSafeHwnd(), 1003, MOD_CONTROL | MOD_NOREPEAT, VK_F1);
        return 0;
    }
    
    //WM_DESTROY Event Proc Handler
    void CMFC_TESTDlg::OnDestroy()
    {
        CDialogEx::OnDestroy();
    
        // TODO: 在此处添加消息处理程序代码
        //解除热键注册
        UnregisterHotKey(this->GetSafeHwnd(), 1001);
        UnregisterHotKey(this->GetSafeHwnd(), 1002);
        UnregisterHotKey(this->GetSafeHwnd(), 1003);
    }

    3、修改热键处理函数

    void CMFC_TESTDlg::OnHotKey(UINT nHotKeyId, UINT nKey1, UINT nKey2)
    {
        // TODO: 在此添加消息处理程序代码和/或调用默认值
        switch (nHotKeyId) {
        case 1001:
        case 1002:
            //proc Ctrl + S here
            break;
        case 1003:
            //proc Ctrl + F1 here
            break;
    
        default:
            break;
        }
    
        CDialogEx::OnHotKey(nHotKeyId, nKey1, nKey2);
    }

    ----------------------------------------------

    参考资料:

    http://blog.csdn.net/wangjieest/article/details/6910166

    MSDN

  • 相关阅读:
    180. Consecutive Numbers
    181. Employees Earning More Than Their Managers
    15. 3Sum
    11. Container With Most Water
    178. Rank Scores
    在多台服务器上简单实现Redis的数据主从复制
    Head First
    23种设计模式(6):模版方法模式
    《Head.First设计模式》的学习笔记(9)--外观模式
    Head First--设计模式(装饰者模式)
  • 原文地址:https://www.cnblogs.com/LubinLew/p/MFC-HotKey.html
Copyright © 2011-2022 走看看