type
TForm1 =
class(TForm)
Image1: TImage;
Button1: TButton;
Button2: TButton;
Label1: TLabel;
procedure Button1Click(Sender: TObject);
procedure Button2Click(Sender: TObject);
procedure FormClose(Sender: TObject;
var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
type
PMouseLLHookStruct = ^TMouseLLHookStruct;
TMouseLLHookStruct =
record
pt: TPoint;
mouseData: DWORD;
Flags: DWORD;
Time: DWORD;
dwExtraInfo: Cardinal;
end;
var
Form1: TForm1;
mHook: Cardinal;
implementation
{$R *.dfm}
procedure DrawMouseCursor(ScreenShotBitmap: TBitmap);
var
R: TRect;
Icon: TIcon;
II: TIconInfo;
CI: TCursorInfo;
begin
R := ScreenShotBitmap.Canvas.ClipRect;
Icon := TIcon.Create;
try
CI.cbSize := SizeOf(CI);
if GetCursorInfo(CI)
then
if CI.Flags = CURSOR_SHOWING
then
begin
Icon.Handle := CopyIcon(CI.hCursor);
if GetIconInfo(Icon.Handle, II)
then
begin
ScreenShotBitmap.Canvas.Draw(CI.ptScreenPos.X -
(CI.ptScreenPos.X - Round(Icon.Width / 2)),
CI.ptScreenPos.Y - (CI.ptScreenPos.Y - Round(Icon.Height /
2)), Icon);
end;
end;
finally
Icon.Free;
end;
end;
function CaptureWindow(
const WindowHandle: HWnd): TBitmap;
var
DC: HDC;
pt: TPoint;
Ico: TIcon;
IcoInfo: TIconInfo;
begin
DC := GetWindowDC(WindowHandle);
Result := TBitmap.Create;
try
Ico := TIcon.Create;
try
Ico.Handle := GetCursor;
try
GetIconInfo(Ico.Handle, IcoInfo);
Result.Width := Ico.Width;
Result.Height := Ico.Height;
GetCursorPos(pt);
BitBlt(Result.Canvas.Handle, 0, 0, Result.Width, Result.Height,
DC,
pt.X - Round(Ico.Width / 2), pt.Y - Round(Ico.Height / 2), SRCCOPY);
DrawMouseCursor(Result);
finally
Ico.ReleaseHandle;
end;
finally
Ico.Free;
end;
finally
ReleaseDC(WindowHandle,
DC);
end;
end;
procedure ScreenShot;
var
Bmp: TBitmap;
begin
Bmp := TBitmap.Create;
try
Bmp := CaptureWindow(GetDesktopWindow);
Form1.Image1.Picture.Assign(Bmp);
finally
Bmp.Free;
end;
end;
function LowLevelMouseHookProc(nCode: LongInt; WPARAM: WPARAM; lParam: lParam)
: LRESULT;
stdcall;
var
info: ^TMouseLLHookStruct
absolute lParam;
begin
Result := CallNextHookEx(mHook, nCode, WPARAM, lParam);
case WPARAM
of
WM_LBUTTONDOWN:
begin
ScreenShot;
Form1.Label1.Caption := Format('
X = %d, Y = %d, LMBtn down',
[TMouseLLHookStruct(info^).pt.X, TMouseLLHookStruct(info^).pt.Y]);
end;
WM_LBUTTONUP:
Form1.Label1.Caption := Format('
X = %d, Y = %d, LMBtn up',
[TMouseLLHookStruct(info^).pt.X, TMouseLLHookStruct(info^).pt.Y]);
WM_RBUTTONDOWN:
Form1.Label1.Caption := Format('
X = %d, Y = %d, RMBtn down',
[TMouseLLHookStruct(info^).pt.X, TMouseLLHookStruct(info^).pt.Y]);
WM_RBUTTONUP:
Form1.Label1.Caption := Format('
X = %d, Y = %d, RMBtn up',
[TMouseLLHookStruct(info^).pt.X, TMouseLLHookStruct(info^).pt.Y]);
WM_MOUSEMOVE:
Form1.Label1.Caption := Format('
X = %d, Y = %d, Move',
[TMouseLLHookStruct(info^).pt.X, TMouseLLHookStruct(info^).pt.Y]);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
begin
mHook := SetWindowsHookEx(WH_MOUSE_LL, @LowLevelMouseHookProc, HInstance, 0);
end;
procedure TForm1.Button2Click(Sender: TObject);
begin
UnhookWindowsHookEx(mHook);
end;
procedure TForm1.FormClose(Sender: TObject;
var Action: TCloseAction);
begin
UnhookWindowsHookEx(mHook);
end;