⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 cpmousehook.pas

📁 键盘钩子程序及控件, C++ Builder 和DELPHI可用
💻 PAS
📖 第 1 页 / 共 2 页
字号:
begin
if AValue = FHookFilters.WrapPointer then exit;
FHookFilters.WrapPointer := AValue;
end;

procedure TCPMouseHook.SetBlockLeftButton(AValue: boolean);
begin
if AValue = FHookFilters.BlockLeftButton then exit;
FHookFilters.BlockLeftButton := AValue;
end;

procedure TCPMouseHook.SetBlockRightButton(AValue: boolean);
begin
if AValue = FHookFilters.BlockRightButton then exit;
FHookFilters.BlockRightButton := AValue;
end;

procedure TCPMouseHook.SetBlockMiddleButton(AValue: boolean);
begin
if AValue = FHookFilters.BlockMiddleButton then exit;
FHookFilters.BlockMiddleButton := AValue;
end;

procedure TCPMouseHook.SetBlockWheel(AValue: boolean);
begin
if AValue = FHookFilters.BlockWheel then exit;
FHookFilters.BlockWheel := AValue;
end;

procedure TCPMouseHook.SetSwapButtons(AValue: boolean);
begin
if AValue = FHookFilters.SwapButtons then exit;
FHookFilters.SwapButtons := AValue;
SwapMouseButton(AValue);
end;

function TCPMouseHook.GetScreenMickeyX: Extended;
begin
Result := (SCREEN_MICKEYS / Screen.Width);
end;

function TCPMouseHook.GetScreenMickeyY: Extended;
begin
Result := (SCREEN_MICKEYS / Screen.Height);
end;

constructor TCPMouseHook.Create(AOwner: TComponent);
begin
 inherited Create(AOwner);
 {if not (csDesigning in ComponentState) then}
FHookLibLoaded := False;
FWindowHandle := Classes.AllocateHWnd(WndProc);
FEnabled := False;
FUserHookMsg := WM_MOUSEHOOKMSG;
FHookFilters.DisableMouse := False;
FHookFilters.HidePointer := False;
FHookFilters.InvertPointer := False;
FHookFilters.WrapPointer := False;
FHookFilters.BlockLeftButton := False;
FHookFilters.BlockRightButton := False;
FHookFilters.BlockMiddleButton := False;
FHookFilters.BlockWheel := False;
FHookFilters.SwapButtons := False;
keyreps := 0;
end;

destructor TCPMouseHook.Destroy;
begin
TRY
if (FHookLibLoaded and FEnabled) then PFncHookStop;
FINALLY
UnloadHookLib;
DeallocateHWnd(FWindowHandle);
inherited Destroy;
END;
end;

procedure TCPMouseHook.WndProc(var Msg: TMessage);
begin
if Msg.Msg = FUserHookMsg then
     try
     HookMsg(Msg);
     except
     Application.HandleException(Self);
     end
  else Msg.Result := DefWindowProc(FWindowHandle, Msg.Msg, Msg.WParam, Msg.LParam);
end;

procedure TCPMouseHook.HookMsg(var msg : TMessage);
var
FMouseState: TMouseStates;
MouseHookStruct: PMouseHookStruct;
MickeysX,MickeysY: Extended;
dx,dy: word;
InvertX,InvertY: boolean;
WrapX,WrapY: boolean;
begin
  //Move(PMouseHookStruct(msg.lParam)^,MouseHookStruct,SizeOf(TMouseHookStruct));
  MouseHookStruct := PFncHookGetData;
  FMouseState.x := MouseHookStruct.pt.X;
  FMouseState.y := MouseHookStruct.pt.Y;
  MickeysX := (SCREEN_MICKEYS / Screen.Width);
  MickeysY := (SCREEN_MICKEYS / Screen.Height);
  dx := Round(FMouseState.x * MickeysX);
  dy := Round(FMouseState.y * MickeysY);
  FMouseState.dx := dx;
  FMouseState.dy := dy;
  FMouseState.Window := MouseHookStruct.hwnd;
  FMouseState.HitTestCode := MouseHookStruct.wHitTestCode;
  FMouseState.ExtraInfo := MouseHookStruct.dwExtraInfo;
  FMouseState.ButtonInfo := msg.WParam;

  { If Event is InvertPointer or WrapPointer then Save New Pointer Position and Exit}
  if FMouseState.ExtraInfo = LLMHF_INJECTED then begin
     LastDX := dx;
     LastDY := dy;
     LastX := FMouseState.x;
     LastY := FMouseState.y;
     if LastDX < Round(1 * MickeysX) then LastDX := Round(2 * MickeysX);
     if LastDY < Round(1 * MickeysY) then LastDY := Round(2 * MickeysY);
     if LastDX > Round(Screen.Width * MickeysX) then LastDX := Round((Screen.Width-1) * MickeysX);
     if LastDY > Round(Screen.Height * MickeysY) then LastDY := Round((Screen.Height-1) * MickeysY);
     exit;
     end;

  { Pointer Disable and Hide }
  if (FHookFilters.HidePointer or FHookFilters.DisableMouse) and (FMouseState.ExtraInfo <> LLMHF_INJECTED) then begin
     if (dx <> LastDX) or (dy <> LastDY) then begin
        dx := SCREEN_MICKEYS; dy := SCREEN_MICKEYS; LastDX := SCREEN_MICKEYS; LastDY := SCREEN_MICKEYS;
        Mouse_Event(MOUSEEVENTF_MOVE or MOUSEEVENTF_ABSOLUTE,dx,dy,0,LLMHF_INJECTED);
        end;
     end
  { Pointer Invert and Wrap using DLL function calls }
  else if FHookFilters.InvertPointer and (FMouseState.ExtraInfo <> LLMHF_INJECTED) then begin
     InvertX := False;
     InvertY := False;
     if dx <> LastDX then InvertX := True;
     if dy <> LastDY then InvertY := True;
     { DLL Function InvertPointer(InvertX,InvertY: boolean; var CurrentDX,PreviousDX,CurrentDY,PreviousDY: word; WrapPointer: boolean); }
     if (InvertX or InvertY) then PFncHookInvertPointer(InvertX,InvertY,dx,LastDX,dy,LastDY,FHookFilters.WrapPointer);
     end
  else if FHookFilters.WrapPointer and (FMouseState.ExtraInfo <> LLMHF_INJECTED) then begin
     WrapX := False;
     WrapY := False;
     if (dx < 1) or (dx >= Round((Screen.Width-1) * MickeysX)) then WrapX := True;
     if (dy < 1) or (dy >= Round((Screen.Height-1) * MickeysY)) then WrapY := True;
     { DLL function WrapPointer(WrapX,WrapY: boolean; var CurrentDX,CurrentDY: word); }
     if (WrapX or WrapY) then PFncHookWrapPointer(WrapX,WrapY,dx,dy);
     end;

  { Save Current Pointer Positions for Invert functions later }
  LastDX := dx;
  LastDY := dy;
  LastX := FMouseState.x;
  LastY := FMouseState.y;

  msg.Result := 1;
  if Assigned(FOnMouse) then FOnMouse(self,FMouseState);
end;

function TCPMouseHook.Start_MouseHook: Boolean;
var
curpos: TPoint;
begin
Result := False;
if FEnabled then exit;
if Not LoadHookLib then exit;
if PFncHookStart(FLicenceCode,FWindowHandle,FUserHookMsg,FHookFilters) then begin
   FEnabled := True;
   Result := True;
   GetCursorPos(curpos);
   LastX := curpos.X;
   LastY := curpos.Y;
   LastDX := Round(LastX * (SCREEN_MICKEYS / Screen.Width));
   LastDY := Round(LastY * (SCREEN_MICKEYS / Screen.Height));
   end;
end;

function TCPMouseHook.Stop_MouseHook: Boolean;
begin
Result := False;
Try
if FEnabled then Result := PFncHookStop;
FEnabled := False;
Finally
UnloadHookLib;
End;
end;

function TCPMouseHook.UpdateHook: boolean;
begin
Result := False;
if FEnabled then Result := PFncHookUpdateHook(FHookFilters);
end;

function TCPMouseHook.LoadHookLib: boolean;
begin
result := false;
if FHookLibLoaded then exit;
DllHandle := LoadLibrary(PChar(HOOKLIBNAME));
if DllHandle <> 0 then
   begin
   { Get pointers to DLL Hook Functions }
   PFncHookStart := GetProcAddress(DllHandle, 'MouseHook_Start');
   PFncHookStop := GetProcAddress(DllHandle, 'MouseHook_Stop');
   PFncHookGetData := GetProcAddress(DllHandle, 'MouseHook_GetData');
   PFncHookUpdateHook := GetProcAddress(DllHandle, 'MouseHook_UpdateHook');
   PFncHookInvertPointer := GetProcAddress(DllHandle, 'MouseHook_InvertPointer');
   PFncHookWrapPointer := GetProcAddress(DllHandle, 'MouseHook_WrapPointer');
   if Assigned(PFncHookStart) and Assigned(PFncHookStop) and Assigned(PFncHookGetData) and
      Assigned(PFncHookUpdateHook) and Assigned(PFncHookInvertPointer) and Assigned(PFncHookWrapPointer) then
      begin
      FHookLibLoaded := True;
      result := true;
      end else FreeLibrary(DllHandle);
   end;
end;

function TCPMouseHook.UnloadHookLib: boolean;
begin
result := false;
if DllHandle <> 0 then
   begin
   FreeLibrary(DllHandle);
   FHookLibLoaded := false;
   result := true;
   end;
End;

initialization

finalization
 if DllHandle <> 0 then FreeLibrary(DllHandle);

end.

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -