在OnMouseMove中 if pixels(X,Y)==-1 then begin showmessage('不在窗口内') end;
用这个判断:WindowFromPoint(Mouse.CursorPos) = Handle
cobi(我是小新) 好像只能检测到鼠标在窗体内啊,应该怎样写啊
应该在什么事件写才行,当鼠标离开窗体范围后Form的OnMouseMove就无效了阿
+一个TIMERGetCursorPosClientToScreenScreenToClient
unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls, ExtCtrls;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;end.
加个定时器,然后在定时器中监测,也就是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;end;
if pixels(X,Y)==-1 then
begin
showmessage('不在窗口内')
end;
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;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;end.
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;end;