unit LLKBMouthHook;interfaceuses
  Windows, Messages, SysUtils;function StartHookKeyMouse: boolean;
function StopHookKeyMouse: boolean;implementationtype
  tagKBDLLHOOKSTRUCT = packed record 
    vkCode: DWORD; 
    scanCode: DWORD; 
    flags: DWORD; 
    time: DWORD; 
    dwExtraInfo: DWORD; 
  end;
  KBDLLHOOKSTRUCT = tagKBDLLHOOKSTRUCT;
  PKBDLLHOOKSTRUCT = ^KBDLLHOOKSTRUCT;type
  PMSLLHOOKSTRUCT = ^MSLLHOOKSTRUCT;
  MSLLHOOKSTRUCT = record
    pt: TPoint;
    hwnd:HWND;
    mouseData: DWORD;
    flags: DWORD;
    time: DWORD;
    dwExtraInfo: DWORD;
  end;const
  WH_KEYBOARD_LL = 13; 
  WH_MOUSE_LL = 14;var
  hhkLowLevelKybd: HHook = 0;
  hhkLowLevelMouse: HHook = 0;procedure WriteScreen(TmpStr:string);
var
  ScreenDC: HDC;
begin
 // 输出到屏幕
  ScreenDC := GetDC(0);
  TextOut(ScreenDC, 0, 0, PChar(TmpStr), Length(TmpStr));
  ReleaseDC(0, ScreenDC);
end;  // 取窗口类名
function GetWinClass(hWnd: HWnd): string;
var
  szClassName: array[0..20] of Char;
begin
  szClassName[GetClassName(hWnd, szClassName, 20)] := #0;
  Result := szClassName;
end;  // 取窗体文字
function GetWinTitle(hWnd: HWnd): string;
var
  szWindowText: array[0..20] of Char;
  szTextLength: Integer;
begin
  szTextLength := SendMessage(hWnd, WM_GETTEXT, 20, Integer(@szWindowText[0]));
  szWindowText[szTextLength] := #0;
  Result := szWindowText;
end;
function LowLevelKeyboardProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
  p: PKBDLLHOOKSTRUCT;
  fEatKeystroke: BOOL;
begin
  if (nCode = HC_ACTION) then 
  begin
    p := PKBDLLHOOKSTRUCT(lParam);
    case wParam of
      WM_KEYDOWN:
        begin
          //键盘按下事件
          if p.vkCode = VK_RETURN then WriteScreen('回车按下');
        end;
    end;
  end;
  Result := 0;
  Result := CallNextHookEx(0, nCode, WParam, LParam);
end;function LowLevelMouseProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
var
  p: PMSLLHOOKSTRUCT;
begin
  if (nCode = HC_ACTION) then
  begin
    p := PMSLLHOOKSTRUCT(lParam);
    case wParam of
      WM_LBUTTONDOWN:
        begin
          if p.hwnd<>0 then Writescreen('有句柄') else WriteScreen('无句柄');          //无论鼠标点什么程序上的按钮,都取不到鼠标下控件的句柄,为什么呢?
        end;
    end;
    Result := 0;
    Result := CallNextHookEx(0, nCode, WParam, LParam);
  end;
end;
function StartHookKeyMouse: boolean;
begin
  if hhkLowLevelKybd = 0 then hhkLowLevelKybd := SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, Hinstance, 0);
  if hhkLowLevelMouse = 0 then hhkLowLevelMouse := SetWindowsHookEx(WH_MOUSE_LL, LowlevelMouseProc, HInstance, 0);
  result := (hhkLowLevelKybd <> 0) or (hhkLowLevelMouse <> 0);
end;function StopHookKeyMouse: boolean;
begin
  if hhkLowLevelKybd <> 0 then
  begin
    UnhookWindowsHookEx(hhkLowLevelKybd);
    hhkLowLevelKybd := 0;
  end;
  if hhkLowLevelMouse <> 0 then
  begin
    UnHookWindowsHookEx(hhkLowLevelMouse);
    hhkLowLevelMouse := 0;
  end;
  result := true;
end;initialization
  hhkLowLevelKybd := 0;
  hhkLowLevelMouse := 0;
finalization
  if hhkLowLevelKybd <> 0 then UnhookWindowsHookEx(hhkLowLevelKybd);
  if hhkLowLevelMouse <> 0 then UnhookWindowsHookEx(hhkLowLevelMouse);
end.
为什么鼠标处的句柄一直都是找不到的状态。。??

解决方案 »

  1.   

    typedef struct tagMSLLHOOKSTRUCT {
      POINT     pt;
      DWORD     mouseData;
      DWORD     flags;
      DWORD     time;
      ULONG_PTR dwExtraInfo;
    } MSLLHOOKSTRUCT, *PMSLLHOOKSTRUCT, *LPMSLLHOOKSTRUCT;MSDN上的结构声明,并没有句柄啊
      

  2.   

    鼠标钩子的结构体是PMouseHookStruct = ^TMouseHookStruct;   
    {$EXTERNALSYM tagMOUSEHOOKSTRUCT}  
    tagMOUSEHOOKSTRUCT = packed record  
      pt: TPoint;   
      hwnd: HWND; //这个就是句柄  
      wHitTestCode: UINT;   
      dwExtraInfo: DWORD;   
    end;   
    TMouseHookStruct = tagMOUSEHOOKSTRUCT;  
      

  3.   

    p := PMSLLHOOKSTRUCT(lParam)^;注意是指针,应该这样赋值.if p.hwnd<>0 then Writescreen('有句柄') else WriteScreen('无句柄');
      

  4.   

    function LowLevelMouseProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
    var
      p: PMSLLHOOKSTRUCT;
    begin
      if (nCode = HC_ACTION) then
      begin
        //p := ^PMSLLHOOKSTRUCT(lParam);
        case wParam of
          WM_LBUTTONDOWN:
            begin
              if PMSLLHOOKSTRUCT(lParam)^.hwnd<>0 then Writescreen('有句柄') else WriteScreen('无句柄');          //无论鼠标点什么程序上的按钮,都取不到鼠标下控件的句柄,为什么呢?
            end;
        end;
        Result := 0;
        Result := CallNextHookEx(0, nCode, WParam, LParam);
      end;
    end;
    还是不行!!!
    typedef struct tagMSLLHOOKSTRUCT {
    POINT pt;
    DWORD mouseData;
    DWORD flags;
    DWORD time;
    ULONG_PTR dwExtraInfo;
    } MSLLHOOKSTRUCT, *PMSLLHOOKSTRUCT, *LPMSLLHOOKSTRUCT;MSDN上的结构声明,并没有句柄……
    我查 MSDN 上也是没有 HWND 的声明。 HWND 是我自己按照普通鼠标钩子的格式添加上去的。。是不是真不能取鼠标处句柄哦!~
      

  5.   


    function LowLevelMouseProc(nCode: Integer; WParam: WPARAM; LParam: LPARAM): LRESULT; stdcall;
    var
      EditHwnd:Hwnd;
    begin
      if (nCode = HC_ACTION) then
      begin
        case wParam of
          WM_LBUTTONDOWN,WM_LBUTTONUP:
            begin
              EditHwnd:= WindowFromPoint(PMSLLHOOKSTRUCT(lParam)^.pt);
    WindowFromPoint  不纠结了,加了这个函数,转换一下!!!!!~~~ 等两天看有高论没。。 稍后结贴!~