unit Unit1;
{$mode objfpc}{$H+}
interface
uses
windows, Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, StdCtrls;
const
WH_MOUSE_LL =14;
WM_MOUSEHOOK = WM_USER + 1; // definimos nuestro mensaje de usuario
type
{ TForm1 }
TForm1 = class(TForm)
GroupBox1: TGroupBox;
GroupBox2: TGroupBox;
Label1: TLabel;
Label2: TLabel;
Label3: TLabel;
Label4: TLabel;
Label5: TLabel;
Label6: TLabel;
procedure FormClose(Sender: TObject; var CloseAction: TCloseAction);
procedure FormCreate(Sender: TObject);
private
Message: boolean;
procedure OnMouseHook(var Msg: TMessage); message WM_MOUSEHOOK;
public
{ public declarations }
end;
var
Form1: TForm1;
hMouseHook: HHOOK;
function MessageBoxTimeOut(hWnd: HWND; lpText: PChar; lpCaption: PChar; uType: UINT; wLanguageId: WORD; dwMilliseconds: DWORD): Integer; stdcall; external user32 name 'MessageBoxTimeoutA';
implementation
{$R *.lfm}
{ TForm1 }
function MouseEvent(nCode, wParam, lParam: Integer): Integer; stdcall;
var
Point: TPoint;
begin
if nCode>=0 then
begin
GetCursorPos(Point);
Form1.Label1.Caption:= '(Horizontal X: ' + IntToStr(Point.X) + ', Vertical Y: ' + IntToStr(Point.Y) + ')';
PostMessage(Form1.Handle, WM_MOUSEHOOK, Point.X, Point.Y); // Enviamos el mensaje sin esperar respuesta y pasando coordenadas del mouse
if Point.Y=0 then form1.Label3.Caption:='pegado arriba' else form1.Label3.Caption:='normal';
if Point.X=0 then form1.Label4.Caption:='pegado izquierda' else form1.Label4.Caption:='normal';
if Point.X=screen.Width-1 then form1.Label5.Caption:='pegado derecha' else form1.Label5.Caption:='normal';
if Point.Y=screen.height-1 then form1.Label6.Caption:='pegado abajo' else form1.Label6.Caption:='normal';
end;
Result:= CallNextHookEx(hMouseHook, nCode, wParam, lParam);
end;
procedure TForm1.OnMouseHook(var Msg: TMessage);
begin
if not Message then
begin
// Un semáforo para evitar la sobrecarga de mensajes, enviará uno por cada movimiento del Mouse...
Message:= true;
// Uso MessageBoxTimeOut para evitar problemas en el cierre de la aplicación
// Maneja esto como te venga bien.
MessageBoxTimeOut(0, PCHAR('Mouse (' + IntToStr(Msg.lParam) + ', ' + IntToStr(Msg.wParam)+')'),'MouseHook', MB_SYSTEMMODAL or MB_TOPMOST or MB_ICONINFORMATION, 0, 2000);
Message:= false;
end;
end;
procedure TForm1.FormClose(Sender: TObject; var CloseAction: TCloseAction);
begin
// Paramos el Hook
UnhookWindowsHookEx(hMouseHook);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
Message:= false;
form1.Label2.Caption:='El Ancho es: '+inttostr(Screen.Width)+' El alto es: '+inttostr(Screen.Height)+' Y el Pixel es: '+inttostr(Screen.PixelsPerInch); // Obtener las dimensiones de la pantalla
// Ponemos en marcha el Hook
hMouseHook:= SetWindowsHookEx(WH_MOUSE_LL, @MouseEvent, HInstance, 0);
end;
end.