用APIHOOK屏幕取词如何取到IE中鼠标指定地方的字?
比如取到了“问题的价值分”,现在鼠标指向"价"字。
程序如何知道鼠标指向的哪个字呢?

解决方案 »

  1.   

    smth的delphi版有源代码.发信人: raphaelzl (小飞熊), 信区: Delphi 
    标  题: 数据定义文件 u_def.pas 
    发信站: BBS 水木清华站 (Sat Jan  1 11:25:21 2000) 
      
      
    unit U_Def; 
    interface 
    uses 
      Messages, Windows; 
    const 
      WM_MOUSEPT = WM_USER + 1000 + Ord('M') + Ord('P') + Ord('T'); 
      MappingFileName = 'Mapping File By Raphael'; 
      MaxStringLen = 50; 
      CodeJump = $E9909090; 
    type 
      PInt = ^integer; 
      _ExtTextOutA = function (theDC :HDC; nXStart, nYStart :integer; toOptions 
    : Longint; rect : PRect; 
                               lpStr :PAnsiChar; nCount :integer; Dx : PInteger) 
    :BOOL; stdcall; 
      _PExtTextOutA = ^_ExtTextOutA; 
      TLongJump = packed record 
        JmpOp : Cardinal; 
        Addr : Pointer; 
      end; 
      TShareMem = packed record 
        hProcWnd : HWND;           //The main window of the program 
        hHookWnd : HWND;           //The window currently being hooked 
        hWndPseudo : HWND;         //The pseudo window used to repaint the other 
     window 
        hProc : THandle;           //The process ID of the main program 
        pMouse : TPoint;           //the mouse position 
        fStrMouseQueue : array [0..MaxStringLen] of Char; //mouse info 
        nTimePassed : integer;     //the time passed since last time's mousemove 
      
        bCanSpyNow : Boolean; 
        bHookExtTextOutA : Boolean; 
        pOldExtTextOutA : TLongJump; 
        fStrExtTextOutA : array [0..MaxStringLen] of Char; 
      end; 
      PShareMem = ^TShareMem; 
    implementation 
    end. 
    -- 
      

  2.   

    发信人: raphaelzl (小飞熊), 信区: Delphi 
    标  题: hook的dll源文件 
    发信站: BBS 水木清华站 (Sat Jan  1 11:26:17 2000) 
      
    library dll_HookMouse; 
    uses 
      SysUtils, 
      Windows, 
      Classes, 
      Messages, 
      Math, 
      Dialogs, 
      U_Def in 'U_Def.pas'; 
    {$R *.RES} 
    var 
      hMouseHook : HHOOK; 
      SpyInstalled : Boolean; 
      fTimerID : Cardinal; 
      pShMem : PShareMem; 
      hMappingFile : THandle; 
    function InstallSpy:Boolean; forward; 
    function UnWiseSpy:Boolean; forward; 
    function fExtTextOutA(theDC :HDC; nXStart, nYStart :integer; toOptions : Lon 
    gint; rect : PRect; 
                          lpStr :PAnsiChar; nCount :Longint; Dx: PInteger):BOOL; 
     stdcall; 
    var 
      dwBytes, dwCallingProc : DWORD; 
      pOldExtTextOut : _ExtTextOutA; 
      hModuleGDI : THandle; 
      poOri, poDC, poText, poMouse : TPoint; 
      Size : TSize; 
    begin 
      UnWiseSpy; 
      GetWindowThreadProcessID(pShMem^.hHookWnd, @dwCallingProc); 
      try 
        if pShMem^.bCanSpyNow and (dwCallingProc <> pShMem^.hProc) then 
        begin 
          dwBytes := Min(nCount, MaxStringLen); 
          CopyMemory(@(pShMem^.fStrExtTextOutA), lpStr, dwBytes); 
          //Get lpStr Content 
          //The following codes for get the right text 
          GetDCOrgEx(theDC, poOri); 
          // 取得本窗口设备相关坐标原点的全局逻辑坐标 
          poDC.x := nXStart; 
          poDC.y := nYStart; 
          // 
          LPToDP(theDC, poDC, 1); 
          //全局逻辑坐标转化为设备相关坐标 
          GetCursorPos(poMouse); 
          poText.x := poDC.x + poOri.x; 
          poText.y := poDC.y + poOri.y; 
          if (GetTextAlign(theDC) and TA_UPDATECP) <> 0 then 
          begin 
            GetCurrentPositionEx(theDC, @poOri); 
            poText.x := poText.x + poOri.x; 
            poText.y := poText.y + poOri.y; 
          end; 
          GetTextExtentPoint(theDC, lpStr, nCount, Size); 
          // 取得要输出的字符串的实际显示大小 
          if (poMouse.x >= poText.x) and (poMouse.x <= poText.x + Size.cx) and 
             (poMouse.y >= poText.y) and (poMouse.y <= poText.y + Size.cy) then 
          begin 
            pShMem^.bCanSpyNow := False; 
            pShMem^.nTimePassed := -1; 
          end; 
          pShMem^.fStrExtTextOutA[dwBytes] := Chr(0); 
          FlushViewOfFile(pShMem, 0); 
          if dwCallingProc <> pShMem^.hProc then 
            PostMessage(pShMem^.hProcWnd, WM_MOUSEPT, 2, 2); 
        end; 
        if (dwCallingProc = pShMem^.hProc) or pShMem^.bHookExtTextOutA then 
        begin 
          hModuleGDI := GetModuleHandle(PChar('GDI32')); 
          @pOldExtTextOut := GetProcAddress(hModuleGDI, PChar('ExtTextOutA')); 
          Result := pOldExtTextOut(theDC, nXStart, nYStart, toOptions, rect, lpS 
    tr, nCount, Dx); 
        end else 
          Result := True; 
      except 
        Result := False; 
      end; 
      SpyInstalled := True; 
      InstallSpy; 
    end; 
    function UnWiseSpy:Boolean; 
    var 
      dwBytesWritten, dwOldProtect : DWORD; 
      pOldExtTextOut : _ExtTextOutA; 
      hModuleGDI : THandle; 
    begin 
      hModuleGDI := GetModuleHandle(PChar('GDI32')); 
      @pOldExtTextOut := GetProcAddress(hModuleGDI, PChar('ExtTextOutA')); 
      if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), PAGE_EXECUTE_REA 
    DWRITE, @dwOldProtect) then 
      begin 
        Result := False; 
        Exit; 
      end; 
      if not WriteProcessMemory(GetCurrentProcess, @pOldExtTextOut, @pShMem^.pOl 
    dExtTextOutA, SizeOf(TLongJump), dwBytesWritten) then 
      begin 
        Result := False; 
        Exit; 
      end; 
      if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), dwOldProtect, @d 
    wBytesWritten) then 
      begin 
        Result := False; 
        Exit; 
      end; 
      Result := True; 
    end; 
    function InstallSpy:Boolean; 
    var 
      dwBytesWritten, dwOldProtect : DWORD; 
      ljHere : TLongJump; 
      pOldExtTextOut : _ExtTextOutA; 
      hModuleGDI : THandle; 
    begin 
      hModuleGDI := GetModuleHandle(PChar('GDI32')); 
      @pOldExtTextOut := GetProcAddress(hModuleGDI, PChar('ExtTextOutA')); 
      if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), PAGE_EXECUTE_REA 
    DWRITE, @dwOldProtect) then 
      begin 
        Result := False; 
        Exit; 
      end; 
      ljHere.JmpOp := CodeJump; 
      ljHere.Addr := Pointer( Cardinal(@fExtTextOutA) - Cardinal(@pOldExtTextOut 
    ) - SizeOf(TLongJump) ); 
      if not WriteProcessMemory(GetCurrentProcess, @pOldExtTextOut, @ljHere, Siz 
    eOf(TLongJump), dwBytesWritten) then 
      begin 
        Result := False; 
        Exit; 
      end; 
      if not VirtualProtect(@pOldExtTextOut, SizeOf(TLongJump), dwOldProtect, @d 
    wBytesWritten) then 
      begin 
        Result := False; 
        Exit; 
      end; 
      Result := True; 
    end; 
    -- 
      

  3.   

    function MouseHookProc(nCode : integer; wPar : WParam; lPar : LParam) : lRes 
    ult; stdcall; 
    var 
      pMouseInf : TMouseHookStruct; 
    begin 
      if (not SpyInstalled) and pShMem^.bHookExtTextOutA then 
        InstallSpy; 
      if SpyInstalled and (not pShMem^.bHookExtTextOutA) then 
      begin 
        UnwiseSpy; 
        SpyInstalled := False; 
      end; 
      pShMem^.nTimePassed := 0 ; 
      if (nCode >= 0) and (wPar = WM_MOUSEMOVE) then 
      begin 
        pMouseInf := (PMouseHookStruct(lPar))^; 
        if (pShMem^.pMouse.x <> pMouseInf.pt.x) or 
           (pShMem^.pMouse.y <> pMouseInf.pt.y) then 
        begin 
          if nCode = HC_NOREMOVE then 
            pShMem^.fStrMouseQueue := 'Not removed from the queue' 
          else                                //then HC_ACTION 
            pShMem^.fStrMouseQueue := 'Removed from the queue'; 
          pShMem^.pMouse := pMouseInf.pt; 
          pShMem^.hHookWnd := pMouseInf.hwnd; 
          PostMessage(pShMem^.hProcWnd, WM_MOUSEPT, 1, 1); //1 indicates mouse m 
    essage 
        end; 
      end; 
      FlushViewOfFile(pShMem, 0); 
      Result := CallNextHookEx(hMouseHook, nCode, wPar, lPar); 
    end; 
    procedure fOnTimer(theWnd : HWND; msg, idTimer : Cardinal; dwTime : DWORD);f 
    ar pascal; //CallBack Type 
    begin 
      if pShMem^.nTimePassed = -1 then 
        Exit; 
      pShMem^.nTimePassed := pShMem^.nTimePassed + 1; 
      if pShMem^.nTimePassed > 21 then 
      begin 
        pShMem^.nTimePassed := 21; 
        FlushViewOfFile(pShMem, 0); 
        Exit; 
      end; 
      if pShMem^.nTimePassed > 20 then 
      begin 
        pShMem^.bCanSpyNow := True; 
        FlushViewOfFile(pShMem, 0); 
        SetWindowPos(pShMem^.hWndPseudo, HWND_TOPMOST, pShMem^.pMouse.x, pShMem^ 
    .pMouse.y, 1, 8, SWP_NOACTIVATE or SWP_SHOWWINDOW); 
        ShowWindow(pShMem^.hWndPseudo , SW_HIDE); 
      end; 
    end; 
    function MouseWndProc(theWnd : HWND; theMess : Cardinal; wPar : wParam; lPar 
     : lParam): LResult;stdcall; 
    begin 
      case theMess of 
        WM_CLOSE : 
                begin 
                   DestroyWindow(theWnd); 
                   PostQuitMessage(0); 
                end; 
        else 
             begin 
               Result := DefWindowProc(theWnd, theMess, wPar, lPar); 
               Exit; 
             end; 
      end; 
      Result := 0; 
    end; 
    function InstallMouseHook(hInst : LongWord):Boolean; 
    begin 
      hMouseHook := SetWindowsHookEx(WH_MOUSE, 
                                     MouseHookProc, 
                                     GetModuleHandle(PChar('dll_HookMouse')), 
                                     0); 
      if hMouseHook = 0 then 
      begin 
        Result := False; 
        Exit; 
      end; 
      pShMem^.hWndPseudo := CreateWindowEx(WS_EX_TOPMOST or WS_EX_TOOLWINDOW, 
                                           'ZL_MOUSE_WND_PSEUDO', 
                                           'ZL_MOUSE_WND_PSEUDO', 
                                           WS_CLIPSIBLINGS or WS_POPUP , 
                                           0, 0, 1, 8, 
                                           0, 0, 
                                           hInst, 
                                           nil); 
      ShowWindow(pShMem^.hWndPseudo, SW_HIDE); 
      UpdateWindow(pShMem^.hWndPseudo); 
      fTimerID := SetTimer(0, 0, 10, @fOnTimer); 
      FlushViewOfFile(pShMem, 0); 
      Result := True; 
    end; 
    function UnWiseMouseHook:Boolean; 
    begin 
      KillTimer(0, fTimerID); 
      DestroyWindow(pShMem^.hWndPseudo); 
      if SpyInstalled then 
        UnWiseSpy; 
      pShMem^.bHookExtTextOutA := False; 
      FlushViewOfFile(pShMem, 0); 
      Result := UnHookWindowsHookEx(hMouseHook); 
    end; 
    procedure DllEntry(nReason : integer); 
    begin 
      case nReason Of 
        DLL_PROCESS_ATTACH: 
          begin 
            hMappingFile := CreateFileMapping($FFFFFFFF, 
                                              nil, 
                                              PAGE_READWRITE, 
                                              0, 
                                              SizeOf(TShareMem), 
                                              PChar(MappingFileName)); 
            if hMappingFile<>0 then //if h..=0 , the work is done by OS 
            begin 
              pShMem := PShareMem( MapViewOfFile(hMappingFile, 
                                                 FILE_MAP_WRITE, 
                                                 0, //hi_order offset where mapp 
    ing begins 
                                                 0, //lo_order offset where mapp 
    ing begins 
                                                 0) ); //Size of the mapping 
              if pShMem = nil then 
              begin 
                CloseHandle(hMappingFile); 
                ShowMessage('Cannot create the Share Memory Block!'); 
              end; 
            end else 
              ShowMessage('Cannot create the Share Memory Block!'); 
          end; 
        DLL_PROCESS_DETACH: 
          begin 
            UnwiseSpy; 
            UnMapViewOfFile(pShMem); 
            CloseHandle(hMappingFile); 
          end; 
        else; 
      end; end; 
    exports 
      MouseWndProc, 
      InstallMouseHook, 
      UnWiseMouseHook; 
    begin 
      DllProc := @DllEntry; 
      DllEntry(DLL_PROCESS_ATTACH); 
    end. 
      

  4.   

    嘻嘻,这个程序我有,不过取不到IE中的词,只能取到句子。
    我现在自己搞定IE取词了,用的COM的方法。效果非常不错哦,我找了一天的MSDN :)
    贴一下吧:
    function GetIEFromHWND(AHandle: THandle; var IE: IWebbrowser2): HRESULT;
    var
      hInst: THandle;
      lRes: Cardinal;
      MSG: Integer;
      pDoc: IHTMLDocument2;
      ObjectFromLresult: TObjectFromLresult;
    begin
      Result := S_FALSE;
      hInst := LoadLibrary('Oleacc.dll');
      @ObjectFromLresult := GetProcAddress(hInst, 'ObjectFromLresult');
      if @ObjectFromLresult <> nil then begin
        try
          MSG := RegisterWindowMessage('WM_HTML_GETOBJECT');
          SendMessageTimeOut(AHandle, MSG, 0, 0, SMTO_ABORTIFHUNG, 1000, lRes);
          Result := ObjectFromLresult(lRes, IHTMLDocument2, 0, pDoc);
          if Result = S_OK then
            (pDoc.parentWindow as IServiceprovider).QueryService(IWebbrowserApp, IWebbrowser2, IE);
        finally
          FreeLibrary(hInst);
        end;
      end;
    end;function IEGetSelectElementText(SelectElement: IHTMLSelectElement): string;
    var
      ppvdispOption: IDispatch;
      ppvOption: IHTMLOptionElement;
      Index: OleVariant;
    begin
      Result := '';
      Index := SelectElement.selectedIndex;
      ppvdispOption := SelectElement.item(Index, Index);
      if Succeeded(ppvdispOption.QueryInterface(IID_IHTMLOptionElement, ppvOption)) then
        Result := ppvOption.text;
    end;function IEGetCaption(AHandle: THandle; PT: TPoint; MaxLen: Integer; var CharPos: Integer): string;
      function GetSelText(pTxtRange: IHTMLTxtRange): string;
      var
        I: Integer;
      begin
        try
          if pTxtRange = nil then
          begin
            Result := '';
            Exit;
          end;
          CharPos := 1;
          pTxtRange.moveToPoint(PT.X, PT.Y);
          pTxtRange.expand('character');
          Result := pTxtRange.text;
          for I := 1 to MaxLen - 1 do
          begin
            pTxtRange.moveStart('character', -1);
            if Result = pTxtRange.text then
              Break;
            Result := pTxtRange.text;
            if Copy(Result, 1, 2) <> #13#10 then
              Inc(CharPos, 1);
          end;
          for I := 1 to MaxLen - 1 do
          begin
            pTxtRange.moveEnd('character', 1);
            if Result = pTxtRange.text then
              Break;
            Result := pTxtRange.text;
          end;
          Result := StringReplace(Result, #13, '', [rfReplaceAll]);
          Result := StringReplace(Result, #10, '', [rfReplaceAll]);
          //pTxtRange.select;
        except
        end;
      end;
    var
      IE: IWebBrowser2;
      Document: IHTMLDocument2;
      EL: IHTMLElement;
      //pPwdElement: IHTMLInputTextElement;
      pInputElement: IHTMLInputElement;
      pBtnElement: IHTMLButtonElement;
      pTextAreaElement: IHTMLTextAreaElement;
      pSelectElement: IHTMLSelectElement;
      pHTMLBodyElement: IHTMLBodyElement;
    begin
      if GetIEFromHWnd(AHandle, IE) = S_OK then
      begin
        try
          Document := IE.Document as IHtmlDocument2;
          if Assigned(Document) then
          begin
            EL := Document.elementFromPoint(pt.X, pt.Y);
            if Assigned(EL) then
            begin
              {if Succeeded(EL.QueryInterface(IID_IHTMLInputTextElement, pPwdElement)) then
              begin
                if pPwdElement.type_ = 'password' then //only the password
                  Result := pPwdElement.value;
              end
              else   }
              if Succeeded(EL.QueryInterface(IID_IHTMLInputElement, pInputElement)) then
                Result := GetSelText(pInputElement.createTextRange)
              else if Succeeded(EL.QueryInterface(IID_IHTMLButtonElement, pBtnElement)) then
                Result := GetSelText(pBtnElement.createTextRange)
              else if Succeeded(EL.QueryInterface(IID_IHTMLTextAreaElement, pTextAreaElement)) then
                Result := GetSelText(pTextAreaElement.createTextRange)
              else if Succeeded(EL.QueryInterface(IID_IHTMLSelectElement, pSelectElement)) then
                Result := IEGetSelectElementText(pSelectElement)
              else if Succeeded(Document.body.QueryInterface(IID_IHTMLBodyElement, pHTMLBodyElement)) then
                Result := GetSelText(pHTMLBodyElement.createTextRange);
            end;
          end;
        except
        end;
      end;
    end;