1.利用定时器 如下图所示建立工程: 代码如下: unit mainunit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls; type TForm1 = class(TForm) GroupBox1: TGroupBox; Button1: TButton; Button2: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; Timer1: TTimer; procedure FormCreate(Sender: TObject); procedure FormPaint(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); procedure Timer1Timer(Sender: TObject); private { Private declarations } public procedure GetRectArea; { Public declarations } end; var Form1: TForm1; FormHandle:Hwnd; FrmLeft,FrmTop,FrmRight,FrmBottom:Integer; implementation {$R *.dfm} procedure JudgePointPosition(CurPoint:TPoint); begin if ((CurPoint.X >= FrmLeft) and (CurPoint.X <= Frmright) and (CurPoint.Y >= FrmTop) and (CurPoint.Y <= FrmBottom)) then begin Form1.Show; end else begin Form1.hide; end; end; procedure TForm1.GetRectArea; var rect:TRect; begin GetWindowRect(FormHandle,rect); label1.Caption:='窗体左上角坐标:'+'('+Inttostr(rect.left)+','+Inttostr(rect.top)+')'; label2.Caption:='窗体右上角坐标:'+'('+Inttostr(rect.right)+','+Inttostr(rect.top)+')'; label3.Caption:='窗体左下角坐标:'+'('+Inttostr(rect.left)+','+Inttostr(rect.bottom)+')'; label4.Caption:='窗体右下角坐标:'+'('+Inttostr(rect.right)+','+Inttostr(rect.bottom)+')'; FrmLeft:=rect.left; FrmTop:=rect.top; FrmRight:=rect.right; FrmBottom:=rect.bottom; end; procedure TForm1.FormCreate(Sender: TObject); begin FormHandle:=Form1.Handle; end; procedure TForm1.FormPaint(Sender: TObject); begin GetRectArea; end; procedure TForm1.Button2Click(Sender: TObject); begin Timer1.Enabled:=False; end; procedure TForm1.Button1Click(Sender: TObject); begin Timer1.Enabled:=True; end; procedure TForm1.Timer1Timer(Sender: TObject); var P:TPoint; begin GetRectArea; GetCursorPos(P); Form1.label6.caption:='横坐标:'+InttoStr(P.x); Form1.label7.caption:='纵坐标:'+InttoStr(P.y); JudgePointPosition(P); end; end. 2.利用鼠标钩子 如下图所示建立工程: 代码如下: unit mainunit; interface uses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls; type TForm1 = class(TForm) GroupBox1: TGroupBox; Button1: TButton; Button2: TButton; Label1: TLabel; Label2: TLabel; Label3: TLabel; Label4: TLabel; Label5: TLabel; Label6: TLabel; Label7: TLabel; procedure FormCreate(Sender: TObject); procedure FormPaint(Sender: TObject); procedure Button2Click(Sender: TObject); procedure Button1Click(Sender: TObject); private HookHandle: THandle; { Private declarations } public procedure WMSysCommand(var Msg: TWMSysCommand); message WM_SYSCOMMAND; procedure GetRectArea; { Public declarations } end; var Form1: TForm1; FormHandle:Hwnd; FrmLeft,FrmTop,FrmRight,FrmBottom:Integer; implementation {$R *.dfm} procedure JudgePointPosition(CurPoint:TPoint); begin if ((CurPoint.X >= FrmLeft) and (CurPoint.X <= Frmright) and (CurPoint.Y >= FrmTop) and (CurPoint.Y <= FrmBottom)) then begin Form1.Show; end else begin Form1.hide; end; end; function MouseProc(Code: Integer; W: WPARAM; L: LPARAM): LRESULT; stdcall; var CurPoint:TPoint; begin if Code < 0 then Result := CallNextHookEx(Form1.HookHandle, Code, W, L) else begin if Code=HC_ACTION then begin if W = WM_MOUSEMOVE then begin Form1.label6.caption:='横坐标:'+InttoStr(PMouseHookStruct(L).pt.x); Form1.label7.caption:='纵坐标:'+InttoStr(PMouseHookStruct(L).pt.y); CurPoint:=PMouseHookStruct(L).pt; JudgePointPosition(CurPoint); end; Result := 0; end; end; end; procedure TForm1.WMSysCommand(var Msg: TWMSysCommand); begin GetRectArea; DefaultHandler(Msg); end; procedure TForm1.GetRectArea; var rect:TRect; begin GetWindowRect(FormHandle,rect); label1.Caption:='窗体左上角坐标:'+'('+Inttostr(rect.left)+','+Inttostr(rect.top)+')'; label2.Caption:='窗体右上角坐标:'+'('+Inttostr(rect.right)+','+Inttostr(rect.top)+')'; label3.Caption:='窗体左下角坐标:'+'('+Inttostr(rect.left)+','+Inttostr(rect.bottom)+')'; label4.Caption:='窗体右下角坐标:'+'('+Inttostr(rect.right)+','+Inttostr(rect.bottom)+')'; FrmLeft:=rect.left; FrmTop:=rect.top; FrmRight:=rect.right; FrmBottom:=rect.bottom; end; procedure TForm1.FormCreate(Sender: TObject); begin FormHandle:=Form1.Handle; HookHandle := 0; end; procedure TForm1.FormPaint(Sender: TObject); begin GetRectArea; end; procedure TForm1.Button2Click(Sender: TObject); begin if HookHandle <> 0 then begin UnhookWindowsHookEx(HookHandle); HookHandle := 0; Form1.Caption:='鼠标钩子成功卸载!'; end; end; procedure TForm1.Button1Click(Sender: TObject); begin if HookHandle = 0 then begin HookHandle := SetWindowsHookEx(WH_MOUSE, @MouseProc, hInstance, 0); if HookHandle<>0 then Form1.Caption:='成功加载了鼠标钩子函数!'; end; end; end.