现在的位置: 首页 > 综合 > 正文

仿QQ悬挂窗口的实现(全局鼠标钩子)

2013年06月28日 ⁄ 综合 ⁄ 共 6642字 ⁄ 字号 评论关闭

工程下载地址:http://download.csdn.net/user/kissyfish

上过QQ的朋友们都知道,当QQ窗口位于桌面的左边界、右边界或顶部的时候,QQ会自动隐藏起来;而一旦鼠标再次接触到上述边界的时候,QQ窗口又会自动展开。QQ的这种特效在一定程度上大大的节约了桌面资源,给使用者带来的方便。

    QQ悬挂窗口主要特点就是结合窗口以及鼠标的位置,并通过鼠标事件来调整窗口的显示方式。其中,窗口以及鼠标的位置可以通过GetWindowRectGetCursorPos这两个函数来获取,故如何获取鼠标事件成为QQ悬挂窗口实现的关键。

    对于一个窗口来说,按鼠标事件的触发位置,鼠标事件可以分为三类:

1.      客户区鼠标消息:鼠标在窗口的客户区移动时产生的消息,此消息是标准的鼠标消息,MFC中通过WM_MOUSEMOVE这个事件解决了这个问题。

2.      非客户区鼠标消息:鼠标在非客户区以外(标题栏、框架等)移动时产生的消息,此消息是标准的鼠标消息,MFC中通过WM_NCMOUSEMOVE这个事件解决了这个问题。

3.      窗口以外的鼠标消息:鼠标不在本窗口移动时产生的消息,此消息不是标准的鼠标消息,在MFC中也找不到这样的事件。那该如何捕获这样的鼠标消息呢?

窗口以外的鼠标消息必然是发生在其他窗口上的,此鼠标消息是发往其他窗口的消息队列中,由其他窗口的消息队列所维护。

不过,我们可以通过设置全局鼠标钩子来监视鼠标的位置,并触发鼠标消息。如果将鼠标钩子设置在窗口内部设置的话,那此鼠标钩子仅能够监视到上述鼠标事件的前两类事件,而不能够监视到本窗口以外的鼠标消息,并不是真正的全局鼠标钩子。如果将鼠标钩子设置在DLL中,那么鼠标在整个屏幕上所发生的事件都会被这个鼠标过程所监察到,即可以捕获其他窗口的鼠标消息并将此鼠标消息发往本窗口的所属线程的消息队列中。在本窗口中,必须将本窗口的线程ID传到DLL中,使DLL能够将其他鼠标事件发到指定线程的消息队列中。具体实现如下:

  1. //------------------------------------------------------------------------------------
  2. // Function:  SetHook - Creates mouse hook (Exported), called by CAppBarMngr
  3. // Arguments: _id    - Calling thread ID, used to send message to it
  4. //            _width - Width of window
  5. //            _left  - True if window is left side docked, false if not
  6. // Returns:   False if it is already hooked
  7. //            True if hook has been created 
  8. //------------------------------------------------------------------------------------
  9. BOOL SetHook(DWORD _id, int _width, BOOL _left)
  10. {
  11.     if (s_ThreadID)
  12.         return FALSE;   // Already hooked!
  13.     s_Width    = _width;
  14.     s_Left     = _left;
  15.     g_Hook     = ::SetWindowsHookEx(WH_MOUSE, (HOOKPROC)MouseProc, g_Instance, 0);
  16.     s_ThreadID = _id;
  17.     return TRUE;   // Hook has been created correctly
  18. }
  19. //-------------------------------------------------------------------------------------
  20. // Function:  UnSetHook - Removes hook from chain
  21. // Arguments: none
  22. // Returns:   False if not hook pending to delete (no thread ID defined)
  23. //            True if hook has been removed. Also returns true if there is not hook
  24. //                handler, this can occur if Init failed when called in second instance
  25. //-------------------------------------------------------------------------------------
  26. BOOL UnSetHook()
  27. {
  28.     if (!s_ThreadID) {
  29.         return FALSE;    // There is no hook pending to close
  30.     }
  31.     if (g_Hook) {        // Check if hook handler is valid
  32.         ::UnhookWindowsHookEx(g_Hook); // Unhook is done here
  33.         s_ThreadID = 0;  // Remove thread id to avoid continue sending
  34.         g_Hook = NULL;   // Remove hook handler to avoid to use it again
  35.     }
  36.     return TRUE;         // Hook has been removed
  37. }
  38. //-------------------------------------------------------------------------------------
  39. // Function:  MouseProc - Callback function for mouse hook
  40. // Arguments: nCode  - action code, according to MS documentation, must return 
  41. //                     inmediatly if less than 0
  42. //            wParam - not used
  43. //            lParam - not used
  44. // Returns:   result from next hook in chain
  45. //-------------------------------------------------------------------------------------
  46. static LRESULT CALLBACK MouseProc(int nCode, WPARAM wParam, LPARAM lParam)
  47. {
  48.     static LRESULT lResult; // Made static to accelerate processing
  49.     static POINT pt;        // idem
  50.     if (nCode<0 && g_Hook) 
  51.     {
  52.         ::CallNextHookEx(g_Hook, nCode, wParam, lParam);  // Call next hook in chain
  53.         return 0;
  54.     }
  55.     if (s_ThreadID) 
  56.     {
  57.         // Obtain absolute screen coordinates
  58.         ::GetCursorPos(&pt);
  59.         static POINT ptOld;
  60.         //只有当鼠标发生移动时候发生鼠标事件
  61.         if(ptOld.x!=pt.x && ptOld.y!=pt.y)
  62.         {
  63.             ::PostThreadMessage(s_ThreadID, WM_USER+1000, 0, 0); 
  64.             ptOld.x = pt.x;
  65.             ptOld.y = pt.y;
  66.         }
  67.     }
  68.     return ::CallNextHookEx(g_Hook, nCode, wParam, lParam);  // Call next hook in chain
  69. }

     

鼠标消息一旦发到本窗口线程的消息队列后,本窗口过程在鼠标消息未被翻译之前从消息队列中取出消息,并进行处理。故得重载PreTranslateMessage这个虚函数。逻辑判断过程如下:

  1. BOOL CHookTestDlg::PreTranslateMessage(MSG* pMsg) 
  2. {
  3.     // TODO: Add your specialized code here and/or call the base class
  4.     static int i=0;
  5.     
  6.     int nSrcWidth = ::GetSystemMetrics(SM_CXSCREEN);
  7.     int nSrcHeight = ::GetSystemMetrics(SM_CYSCREEN);
  8.     switch(pMsg->message)
  9.     {
  10.     case WM_USER+1000:
  11.         {
  12.             POINT pt;
  13.             CRect rcWindow;
  14.             ::GetCursorPos(&pt);
  15.             GetWindowRect(&rcWindow);
  16.             if(pt.x<1 && (pt.y>rcWindow.top && pt.y<rcWindow.bottom))
  17.             {
  18.                 if(rcWindow.left<1 && rcWindow.Width()<1)
  19.                 {
  20.                     SliderWindow(LEFT, true);
  21.                 }
  22.                 else if(rcWindow.left<1 && rcWindow.Width()>99)
  23.                 {
  24.                 }
  25.                 else if(rcWindow.left>1 && rcWindow.Width()>99)
  26.                 {
  27.                 }
  28.                 else
  29.                 {
  30.                 }
  31.             }
  32.             else if(pt.y<rcWindow.top || pt.y>rcWindow.bottom)
  33.             {
  34.                 if(rcWindow.left<1 && rcWindow.Width()<1)
  35.                 {
  36.                     
  37.                 }
  38.                 else if(rcWindow.left<1 && rcWindow.Width()>99)
  39.                 {
  40.                     SliderWindow(LEFT, false);
  41.                 }
  42.                 else if(rcWindow.left>1 && rcWindow.Width()>99)
  43.                 {
  44.                 }
  45.                 else
  46.                 {
  47.                 }
  48.             }
  49.             else if(pt.x>0 && pt.x<100)
  50.             {
  51.                 if(rcWindow.left<1 && rcWindow.Width()<1 && (pt.y>rcWindow.top && pt.y<rcWindow.bottom))
  52.                 {
  53.                     
  54.                 }
  55.                 else if(rcWindow.left<1 && rcWindow.Width()>99)
  56.                 {
  57.                     //SliderWindow(LEFT, true);
  58.                 }
  59.                 else if(rcWindow.left>1 && rcWindow.Width()>99)
  60.                 {
  61.                 }
  62.                 else
  63.                 {
  64.                 }
  65.             }
  66.             else
  67.             {
  68.                 if(rcWindow.left<1 && rcWindow.Width()<1)
  69.                 {
  70.                 }
  71.                 else if(rcWindow.left<1 && rcWindow.Width()>99 && (pt.y>rcWindow.top && pt.y<rcWindow.bottom))
  72.                 {
  73.                     SliderWindow(LEFT, false);
  74.                 }
  75.                 else if(rcWindow.left>1 && rcWindow.Width()>99)
  76.                 {
  77.                 }
  78.                 else
  79.                 {
  80.                 }
  81.             }
  82.         }
  83.         break;
  84.     default:
  85.         break;
  86.     }
  87.     return CDialog::PreTranslateMessage(pMsg);
  88. }

最后运行结果如下,

朋友们,以后若想捕获其他窗口的鼠标事件的时候可以采用这个方法,大家也可以明白MFC中的标准鼠标消息的底层是怎么实现的,大家是否有眼前一亮的感觉呢?最后提出几个问题:

1、在安装完上述鼠标钩子后,MFC的标准鼠标消息WM_MOUSEMOVEWM_LBUTTONDOWN等还有作用吗?

2、通过MFCON_MESSAGEWM_USER+1000这个与指定的处理过程相关联来处理鼠标消息(当然此时不需要重载PreTranslateMessage),这样做可以吗?如:ON_MESSAGE(WM_USER+1000, MouseProc)

【上篇】
【下篇】

抱歉!评论已关闭.