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

来源:互联网 发布:青年网络文明志愿宣言 编辑:程序博客网 时间:2024/05/05 15:52
使用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. );


具体实现: 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. }

 

原创粉丝点击