unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm1 = class(TForm) procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormCreate(Sender: TObject); private FPoint: TPoint; FMouseExit: boolean; procedure WMTimer(var msg: TWMTimer); message WM_TIMER; public { Public declarations } end;var Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); begin if FMouseExit then begin FMouseExit := False; SetTimer(Handle,1,250,nil); Caption := 'Enter'; end; GetCursorPos(FPoint); end;procedure TForm1.WMTimer(var msg: TWMTimer); var APoint: TPoint; begin GetCursorPos(APoint); if (Apoint.X <> Fpoint.X) or (Apoint.Y <> FPoint.Y) then begin KillTimer(Handle,1); FMouseExit := True; Caption := 'Exit'; end; end;procedure TForm1.FormCreate(Sender: TObject); begin FMouseExit := True; end;end.
截获WM_MOUSEMOVE的消息: procedure WndProc(var message: TMessage);override;//声明procedure TForm1.WndProc(var message: TMessage); begin if message.Msg = WM_MOUSEMOVE then begin { LOWORD(message.LParam)就是鼠标对应于窗体的横坐标 HIWORD(message.LParam)就是鼠标对应于窗体的纵坐标 楼主剩下的应该不难了吧?? ^_^ } end; inherited; end;
在窗體上捕捉CM_MOUSEENTER和CM_MOUSEEXIT消息即可,
CM_MOUSEENTER和CM_MOUSELEAVE消息不太可靠.
procedure TForm1.Button1Click(Sender: TObject); var thePos:TPoint; theRect:TRect; theHwnd:HWND; begin GetCursorPos(thePos); //ShowMessage(InttoStr(thePos.X)+' '+InttoStr(thePos.Y)); theHwnd:=FindWindow(nil,'Form1'); GetWindowRect(theHwnd,theRect); //ShowMessage(InttoStr(theRect.Left)+' '+InttoStr(theRect.Top)); if (thePos.X>theRect.Left) and (thePos.X<theRect.Right) and (thePos.Y>theRect.Top) and (thePos.Y<theRect.Bottom) then ShowMessage('IN'); end;
如果是自己的程序的窗体就跟简单了,就不用FindWindow了。直接比较就可以了。
在文档那抄来的 *************** 虽然从D7开始,给许多组件增加了OnEnter与OnExit事件,但对于大多数组件,要感知鼠标移入移出组件仍是一件不容易的事情。下面的方法就是用于解决这个问题。(对一般组件有效,对Form、MainMenu等无效) private { Private declarations } //定义两个过程: procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER; procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;procedure TForm1.CMMouseEnter(var Msg: TMessage); var anObject : TObject; begin anObject := TObject(Msg.lParam); //Button1为要感知的组件,可以换为其它组件 if anobject = Button1 then edit1.Text :='Mouse Enter'; end;procedure TForm1.CMMouseLeave(var Msg: TMessage); var anObject : TObject; begin anObject := TObject(Msg.lParam); if anobject = Button1 then edit1.Text := 'Mouse Leave'; end;
2。处理CM_MOUSEENTER和CM_MOUSEEXIT消息
这两种方法皆可
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs;type
TForm1 = class(TForm)
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure FormCreate(Sender: TObject);
private
FPoint: TPoint;
FMouseExit: boolean;
procedure WMTimer(var msg: TWMTimer); message WM_TIMER;
public
{ Public declarations }
end;var
Form1: TForm1;implementation{$R *.dfm}procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
if FMouseExit then
begin
FMouseExit := False;
SetTimer(Handle,1,250,nil);
Caption := 'Enter';
end;
GetCursorPos(FPoint);
end;procedure TForm1.WMTimer(var msg: TWMTimer);
var
APoint: TPoint;
begin
GetCursorPos(APoint);
if (Apoint.X <> Fpoint.X) or (Apoint.Y <> FPoint.Y) then
begin
KillTimer(Handle,1);
FMouseExit := True;
Caption := 'Exit';
end;
end;procedure TForm1.FormCreate(Sender: TObject);
begin
FMouseExit := True;
end;end.
procedure WndProc(var message: TMessage);override;//声明procedure TForm1.WndProc(var message: TMessage);
begin
if message.Msg = WM_MOUSEMOVE then
begin
{ LOWORD(message.LParam)就是鼠标对应于窗体的横坐标
HIWORD(message.LParam)就是鼠标对应于窗体的纵坐标
楼主剩下的应该不难了吧??
^_^
}
end;
inherited;
end;
var
thePos:TPoint;
theRect:TRect;
theHwnd:HWND;
begin
GetCursorPos(thePos);
//ShowMessage(InttoStr(thePos.X)+' '+InttoStr(thePos.Y));
theHwnd:=FindWindow(nil,'Form1');
GetWindowRect(theHwnd,theRect);
//ShowMessage(InttoStr(theRect.Left)+' '+InttoStr(theRect.Top));
if (thePos.X>theRect.Left) and (thePos.X<theRect.Right) and (thePos.Y>theRect.Top) and (thePos.Y<theRect.Bottom) then
ShowMessage('IN');
end;
***************
虽然从D7开始,给许多组件增加了OnEnter与OnExit事件,但对于大多数组件,要感知鼠标移入移出组件仍是一件不容易的事情。下面的方法就是用于解决这个问题。(对一般组件有效,对Form、MainMenu等无效)
private
{ Private declarations }
//定义两个过程:
procedure CMMouseEnter(var Msg: TMessage); message CM_MOUSEENTER;
procedure CMMouseLeave(var Msg: TMessage); message CM_MOUSELEAVE;procedure TForm1.CMMouseEnter(var Msg: TMessage);
var
anObject : TObject;
begin
anObject := TObject(Msg.lParam);
//Button1为要感知的组件,可以换为其它组件
if anobject = Button1 then
edit1.Text :='Mouse Enter';
end;procedure TForm1.CMMouseLeave(var Msg: TMessage);
var
anObject : TObject;
begin
anObject := TObject(Msg.lParam);
if anobject = Button1 then
edit1.Text := 'Mouse Leave';
end;
目前我试了,只有 miky(miky) 方法 扑捉 是最准的;
其他的方法,当鼠标移动太快没有办法扑捉 ;----------------------------
miky(miky) 的方法,我只要 Form 发出的消息,怎么过来其他的控件发出的消息!