当前位置:主页>销售管理软件> 列表

鼠标移到窗体外时怎么判断? 找仓库管理软件下载

进销存软件版1楼: 想在鼠标移出form窗体时隐藏或关闭

最好有详细代码

2楼: 窗体声明部分加入:
procedure CMMouseLeave(var Message: TMessage);message CM_MouseLeave;
实现部分加入:
procedure TfrmTest.CMMouseLeave(var Message: TMessage);
begin
Caption := ''鼠标已经移出了窗体!'';
end; 如仓库管理软件下载

3楼: 上面的代码有问题,在鼠标移动到窗体上的其他控件时也会出现该消息!
不过此时鼠标还在窗体内!

4楼: procedure wmnchittest(var msg:Twmnchittest);message wm_nchittest;

procedure TForm1.wmnchittest(var msg: Twmnchittest);
begin
inherited;
if (msg.Result = htcaption) or (msg.Result = htclient) then
begin
caption := ''鼠标在窗体内!'';
end
else
begin
caption := ''鼠标在窗体外!'';
end;
end;

5楼: 其实上面的两个方法都有几个缺点,而且有共同的缺点,它们捕获的有用的消息都只有鼠标通过窗体边框的时候,当鼠标通过边框的速度比较快,窗体就很可能捕获不到这个消息,假如边框的一部分被其它窗体挡住或者窗体根本就是没有边框的话,窗体也不会捕获到消息。


我还是建议楼主用检查鼠标坐标的方法,虽然这种方法比较苯,但是如果配合窗体的Active属性使用,可以达到很理想的效果。

6楼: 关于坐标检查和转换
liyinwei可以给出详细的代码吗

进销存软件版7楼: 以下测试代码全部给你了,我使用的是定时器,最好的方法是跟踪鼠标消息:
楼主也可以直接到这里下载范例:
地址: ftp.mlfjnp.com
帐户和密码均为: dfw


unit frmMouse;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;

type
TForm1 = class(TForm)
Label1: TLabel;
Timer1: TTimer;
procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
procedure Timer1Timer(Sender: TObject);
private
{ Private declarations }
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormMouseMove(Sender: TObject; Shift: TShiftState; X,
Y: Integer);
begin
// Label1.Caption:=''x=''+IntToStr(x)+'', ''+''y=''+IntToStr(y);


end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
Label1.Caption:=''x=''+IntToStr(mouse.CursorPos.X)+'', ''+''y=''+IntToStr(mouse.CursorPos.Y);
end;

end.

8楼: 关于坐标检查和转换
procedure TForm1.Timer1Timer(Sender: TObject);
var vPoint:TPoint;
begin
vPoint:=ScreenToClient(Mouse.CursorPos);
Label1.Caption:=''x=''+IntToStr(vPoint.X)+'', ''+''y=''+IntToStr(vPoint.Y);
// Label1.Caption:=''x=''+IntToStr(mouse.CursorPos.X)+'', ''+''y=''+IntToStr(mouse.CursorPos.Y);
end;

9楼: timer1.Enabled:=false;
GetCursorPos(p);
if (p.X>=CursorOper.Left) and (p.X<=CursorOper.Left+CursorOper.Width)
and (p.Y>=CursorOper.Top) and (p.Y<=CursorOper.Top+CursorOper.Height) then
begin
CursorStatus.Panels[0].Text:=''在本窗体内'';
end else
begin
CursorStatus.Panels[0].Text:=''在本窗体外'';

10楼: {*******************************************************
//以下为我修改、整理的实现类似QQ窗体的完整代码,仅仅缺少磁性窗


//体停靠功能(因你没有相应控件所以我把该段代码删除了...)。
//请试用,希望能帮你解决问题:) Good luck!********************************************************}
unit fMain;

interface

uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, Math, ExtCtrls, ShellAPI, StdCtrls, AppEvnts,
Buttons;

type
TfmMain = class(TForm)
Timer1: TTimer;
Button1: TButton;
BitBtn1: TBitBtn;
StaticText1: TStaticText;
procedure Timer1Timer(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormShow(Sender: TObject);
private
Lst_Height : Integer; //记录窗体隐藏前的高度
Lst_Width : Integer; //记录窗体隐藏前的宽度
Rec_Position : Boolean; //是否启动窗体宽高记录标志
cur_Top: Integer;
cur_Bottom: Integer;
FAnchors: TAnchors;
procedure WMMOVING(var Msg: TMessage); message WM_MOVING;
procedure SetBarHeight;
procedure WMSizing(var Msg: TMessage); message WM_SIZING;


{ Private declarations }
public
{ Public declarations }
end;

var
fmMain: TfmMain;

implementation

{$R *.dfm}

procedure TfmMain.WMMOVING(var Msg: TMessage);
begin
inherited;
with PRect(Msg.LParam)^ do
begin
if (akLeft in FAnchors) or (akRight in FAnchors) then
begin
if (Left > 0) and (Right < Screen.Width) then
begin
if rec_Position then
begin
Bottom := top + Lst_Height;
Right := Left + Lst_Width;
Height := Lst_Height;
Width := Lst_Width;
end;
end else
begin
SetBarHeight;
Top := Cur_Top;
Bottom := Cur_Bottom;
exit;
end;
end;
Left := Min(Max(0, Left), Screen.Width - Width);
Top := Min(Max(0, Top), Screen.Height - Height);
Right := Min(Max(Width, Right), Screen.Width);
Bottom := Min(Max(Height, Bottom), Screen.Height);
if not Rec_Position then

begin
Lst_Height := fmMain.Height;
Lst_Width := fmMain.width;
end;
FAnchors := [];
if Left = 0 then Include(FAnchors, akLeft);
if Right = Screen.Width then Include(FAnchors, akRight);
if Top = 0 then Include(FAnchors, akTop);
if Bottom = Screen.Height then Include(FAnchors, akBottom);
if (akLeft in FAnchors) or (akRight in FAnchors) then
begin
Rec_Position := True;
SetBarHeight;
Top := Cur_Top;
Bottom := Cur_Bottom;
end else
Rec_Position := False;
Timer1.Enabled := FAnchors <> [];
end;
end;

function ControlFromPoint(mParent: TWinControl; mPoint: TPoint): TControl;
var
I: Integer;
begin
Result := nil;
if not Assigned(mParent) then Exit;
for I := 0 to mParent.ControlCount - 1 do begin
if not mParent.Controls[I].Visible then Continue;
if PtInRect(mParent.Controls[I].BoundsRect, mPoint) then begin
if mParent.Controls[I] is TWinControl then
Result := ControlFromPoint(TWinControl(mParent.Controls[I]),
Point(mPoint.X - mParent.Controls[I].Left,
mPoint.Y - mParent.Controls[I].Top));
if not Assigned(Result) then Result := mParent.Controls[I];
end;
end;
end; { ControlFromPoint }

procedure TfmMain.Timer1Timer(Sender: TObject);
const
cOffset = 2;
var
vControl: TControl;
begin
vControl := ControlFromPoint(Self, ScreenToClient(Mouse.CursorPos));
if (WindowFromPoint(Mouse.CursorPos) = Handle) or Assigned(vControl) then
begin
if akLeft in FAnchors then Left := 0;
if akTop in FAnchors then Top := 0;
if akRight in FAnchors then Left := Screen.Width - Width;
if akBottom in FAnchors then Top := Screen.Height - Height;
end
else
begin
if akLeft in FAnchors then Left := -Width + cOffset;
if akTop in FAnchors then Top := -Height + cOffset;
if akRight in FAnchors then Left := Screen.Width - cOffset;

if akBottom in FAnchors then Top := Screen.Height - cOffset;
if akLeft in FAnchors then
begin
Left := -Width + cOffset;
SetBarHeight;
Top := Cur_Top;
Height := Cur_Bottom;
end;
if akRight in FAnchors then
begin
Left := Screen.Width - cOffset;
SetBarHeight;
Top := Cur_Top;
Height := Cur_Bottom;
end;
end;
end;

procedure TfmMain.SetBarHeight;
var
AppBarData : TAPPBARDATA;
begin
AppBarData.cbSize := SIZEOF(AppBarData);
If (SHAppBarMessage(ABM_GETSTATE,AppBarData) AND ABS_AUTOHIDE) <> 0 then
begin
Cur_Top := 1;
Cur_Bottom := Screen.Height - 1;
end else
begin
SHAppBarMessage(ABM_GETTASKBARPOS,AppBarData);
case AppBarData.uEdge of
ABE_TOP : begin
Cur_Top := AppBarData.rc.Bottom + 1;
Cur_Bottom := Screen.Height - 1;
end;
ABE_LEFT : begin

Cur_Top := 1;
Cur_Bottom := Screen.Height - 1;
end;
ABE_RIGHT : begin
Cur_Top := 1;
Cur_Bottom := Screen.Height - 1;
end;
ABE_BOTTOM : begin
Cur_Top := 1;
Cur_Bottom:=Screen.Height -(AppBarData.rc.Bottom - AppBarData.rc.Top) - 1;
end;
end;
end;
end;

procedure TfmMain.WMSizing(var Msg: TMessage);
begin
inherited;
if (akRight in FAnchors) then
begin
with PRect(Msg.LParam)^ do
begin
Left := Screen.Width - Width;
Top := Cur_Top;
Right := Screen.Width;
Bottom := Cur_Bottom
end;
end else if (akLeft in FAnchors) then
begin
with PRect(Msg.LParam)^ do
begin
Left := 0;
Top := Cur_Top;
Right := Width;
Bottom := Cur_Bottom;
end;
end;
end;

procedure TfmMain.FormCreate(Sender: TObject);
begin
cur_Top := 1;
cur_Bottom := Screen.Width - 30;
end;

procedure TfmMain.FormShow(Sender: TObject);
var
I: Integer;
strTmp: String;
wnd: Integer;
begin
strTmp := ExtractFileName(Application.ExeName);
I := Pos(''.'',strTmp);
strTmp := Copy(strTmp,1,I - 1);
wnd := FindWindow(nil,PChar(strTmp));
ShowWindow(wnd,SW_HIDE);
end;

end.

11楼: 多人接受答案了。