var _gaq = _gaq || []; _gaq.push(['_setAccount', 'UA-333696-1']); _gaq.push(['_trackPageview']); _gaq.push(['_trackPageLoadTime']); (function() { var ga = document.createElement('script'); ga.type = 'text/javascript'; ga.async = true; ga.src = ('https:' == document.location.protocol ? 'https://ssl' : 'http://www') + '.google-analytics.com/ga.js'; var s = document.getElementsByTagName('script')[0]; s.parentNode.insertBefore(ga, s); })();
  • 2009年03月23日

    Windows界面自动化录制机制

    分类:

    不同于SpyWindows自动化录制工具需要记录有效的操作信息,这就需要对接收到的消息进行筛选。考虑到这个要求,使用Win32 Hooks是最有效地解决方式,利用它截获并筛选鼠标和键盘消息。

     

    需要用到的主要Windows Api

    DWORD WINAPI GetCurrentThreadId(void);

    HMODULE WINAPI GetModuleHandle(__in_opt  LPCTSTR lpModuleName);

    LRESULT CALLBACK MouseProc(int nCode,WPARAM wParam,LPARAM lParam);

    HHOOK SetWindowsHookEx(int idHook,HOOKPROC lpfn,HINSTANCE hMod,DWORD dwThreadId);

    LRESULT CallNextHookEx(HHOOK hhk,int nCode,WPARAM wParam,LPARAM lParam);

    BOOL UnhookWindowsHookEx(HHOOK hhk);

     

    下面用伪码说明:

    1.     鼠标事件

    MouseHook = SetWindowsHookEx(WH_MOUSE, MyMouseProc, GetModuleHandle(null), GetCurrentThreadId());

     

    MyMouseProc

    {

          if(nCode >= 0)

          {

               if (lParam)

               {

                     MOUSEHOOKSTRUCT *pMouseHookStruct = (MOUSEHOOKSTRUCT *)lParam;

                    

                     switch (wParam)

                     {

                          case WM_LBUTTONDOWN: //TODO ; break;

                          case WM_RBUTTONDOWN: //TODO ; break;

                          case WM_MBUTTONDOWN: //TODO ; break;

                          case WM_XBUTTONDOWN: //TODO ; break;

                          //. . . See Mouse Messages

                          case WM_MOUSEWHEEL); //TODO ; break;

                     }

               }

          }

         

          return CallNextHookEx(g_hHook, nCode, wParam, lParam);

    }

     

    UnhookWindowsHookEx(g_hHook)

     

    2.     键盘事件

    KeyboardHook = SetWindowsHookEx(WH_KEYBOARD, MyKeyboardProc,GetModuleHandle(null), GetCurrentThreadId());

     

    MyKeyboardProc

    {

          if(nCode >= 0)

          {

               if (lParam)

               {   

                     switch (wParam)

                     {

                          case VK_F2: //TODO ; break;

                          case VK_LEFT: //TODO ; break;

                          case VK_RIGHT: //TODO ; break;

                          //. . . See Using Keyboard Input and About Keyboard Input

                          case VK_END; //TODO ; break;

                     }

               }

          }

         

          return CallNextHookEx(g_hKeyboard, nCode, wParam, lParam);

    }

     

    UnhookWindowsHookEx(g_hKeyboard)

    分享到: