加个定时器,然后在定时器中监测,也就是ontimer事件
procedure TFrmMain.Timer1Timer(Sender: TObject);
var
ptMousePos : TPOINT;
begin
ptMousePos.x := 0;
ptMousePos.y := 0;
if (GetCursorPos(ptMousePos)) then
begin
if (left>ptmousepos.X) or(top>ptmousepos.y) or (left+self.Width<ptmousepos.X) or (top+self.Height<ptmousepos.Y ) then
begin
...........做你想做的事情
end;
end;
type
TForm1 = class(TForm)
procedure FormCreate(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
function LowLevelKeyboardFunc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
const WH_MOUSE_LL=14;
var
Form1: TForm1;
hkNTKeyboard: HHOOK;
implementation
{$R *.dfm}
{ TForm1 }
function LowLevelKeyboardFunc(nCode: Integer; wParam: WPARAM; lParam: LPARAM):
LRESULT; stdcall;
var
p:Tpoint;
i,j:integer;
begin
if ncode<>0 then
begin
Result:=CallNextHookEx(0, nCode, wParam, lParam);
exit;
end;
if wParam =WM_MOUSEMOVE then
begin
Getcursorpos(p);
if (p.x< form1.left) or (p.y<form1.top) or (p.x>form1.left+form1.width) or(p.y>form1.top +form1.height) then
form1.Color:=clred
else
form1.Color:=clwindow;
end;
Result:=CallNextHookEx(0, nCode, wParam, lParam);
end;
procedure TForm1.FormCreate(Sender: TObject);
begin
hkNTKeyboard:=SetWindowsHookEx(WH_MOUSE_LL, LowLevelKeyboardFunc, HInstance, 0);
end;
procedure TForm1.FormClose(Sender: TObject; var Action: TCloseAction);
begin
UnHookWindowsHookEx(hkNTKeyboard);
end;