VC下简易实现全局热键--无DLL无钩子(Register HotKey)

本文介绍如何使用 RegisterHotKey 函数来定义全局热键,并通过示例展示了如何在对话框初始化时注册热键(如 Ctrl+W),以及如何在 WM_HOTKEY 消息中处理相应的热键操作。
使用RegisterHotKey()函数即可.
MSDN:The RegisterHotKey function defines a system-wide hot key.
  1. //函数原型:
  2. BOOL RegisterHotKey(
  3.   HWND hWnd,         // window to receive hot-key notification
  4.   int id,            // identifier of hot key
  5.   UINT fsModifiers,  // key-modifier flags
  6.   UINT vk            // virtual-key code
  7. );


href="http://www.j2megame.org/wupei/plugins/plogeshi/styles/plogeshi.css" type="text/css" rel="stylesheet" />具体实现: 1.首先加入函数

  1. BOOL CMyDlg::OnInitDialog()
  2. {
  3.         CDialog::OnInitDialog();
  4.  
  5.         // Set the icon for this dialog.  The framework does this automatically
  6.         //  when the application's main window is not a dialog
  7.         SetIcon(m_hIcon, TRUE);                 // Set big icon
  8.         SetIcon(m_hIcon, FALSE);                // Set small icon
  9.        
  10.         // TODO: Add extra initialization here
  11.  
  12.         //注册热键(Ctrl+W,标识9999)
  13.         RegisterHotKey(this->m_hWnd,9999,MOD_CONTROL,'W');
  14.  
  15.         return TRUE;  // return TRUE  unless you set the focus to a control
  16. }

2.加入相应全局热键函数

  1. //相应WindowProc消息,加入函数
  2. LRESULT CMyCatchScreenDlg::WindowProc(UINT message, WPARAM wParam, LPARAM lParam)
  3. {
  4.         switch(message)
  5.         {
  6.                 ///
  7.                 //热键操作
  8.                 case WM_HOTKEY:
  9.                         if(wParam==9999)  
  10.                         {  
  11.                                 if(!IsWindowVisible())
  12.                                 {
  13.                                         //ShowMyWindow();       // 实现代码
  14.                                 }
  15.                                 else
  16.                                 {
  17.                                         //HideMyWindow();       //实现代码
  18.                                 }
  19.                         }
  20.                 break;
  21.         }
  22.         return CDialog::WindowProc(message, wParam, lParam);
  23. }

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值