离开某个窗体后,如何自动隐藏(100)

  • 主题发起人 主题发起人 Tophi
  • 开始时间 开始时间
T

Tophi

Unregistered / Unconfirmed
GUEST, unregistred user!
我在一个form上添加一个button1,写入如下事件。procedure TForm1.Button1Click(Sender: TObject);begin form2 := TForm2.Create(self); form2.parent := form1; form2.Show;end;这样点击button1就会在这个窗体之中出现一个新的窗体form2。我想在form2上去控制(不是在form1的其他地方点击事件)当我点击form2之外的其他地方时,这个窗体自动隐藏。
 
顶下,学习中
 
加TApplication捕捉MouseClick事件
 
To liuls:你这样设计的话Form2中有控件的话 FORM2就看不见了(关闭,隐藏?),我是在Form2 中加了个Timer,算是曲线救国~~~代码如下:procedure TForm2.Timer1Timer(Sender: TObject);var pt: TPoint;begin Edit1.Text := IntToStr(Self.Left); Edit2.Text := IntToStr(Self.Top); GetCursorPos(pt); {&Otilde;&acirc;&Ecirc;&Ccedil;&raquo;&ntilde;&Egrave;&iexcl;&micro;&Auml;&Iuml;à&para;&Ocirc;&Oacute;&Uacute;&AElig;&Aacute;&Auml;&raquo;&micro;&Auml;×&oslash;±ê} //pt := ScreenToClient(pt); Edit3.Text :=IntToStr(pt.X); Edit4.Text :=IntToStr(pt.Y); if (pt.X <Self.Left) or (pt.Y < Self.Top) or (pt.X>(Self.Left+self.Width)) or (pt.Y > (Self.Top+ Self.Height)) then begin //ShowMessage('q12'); Self.Visible := False; end else begin Self.Visible := True; end;end;procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);begin Timer1.Free;end;
 
呵呵 谢谢liuls,指教
 
To: JC不懂汗~, 这只是例子,可以根据实际情况变通嘛~~.哪怕你给Form2周边留出一个象素位, 就可以了.并不影响你的组件排部.比如: Form2 你想要放一个 TPanel (Align := alClient), 可替换为: Align := alNone; Anchors := [akLeft, akTop, akRight, akBottom]; 并为Form2客户区周边留出一个象素位即可!
 
// 当鼠标离开客户区时隐藏unit Unit1;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs, StdCtrls;type TForm1 = class(TForm) btn1: TButton; procedure btn1Click(Sender: TObject); procedure FormDestroy(Sender: TObject); procedure FormCreate(Sender: TObject); private public { Public declarations } end;var Form1: TForm1;implementationuses Unit2;{$R *.dfm}procedure TForm1.btn1Click(Sender: TObject);begin if not Assigned(Form2) then begin Form2 := TForm2.Create(Self); Form2.Parent := Self; end; Form2.Show;end;procedure TForm1.FormDestroy(Sender: TObject);begin if Assigned(Form2) then FreeAndNil(Form2);end;procedure TForm1.FormCreate(Sender: TObject);begin// Self.FormStyle := fsMDIForm;end;end.// =======================================unit Unit2;interfaceuses Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms, Dialogs;type TForm2 = class(TForm) procedure FormClose(Sender: TObject; var Action: TCloseAction); procedure FormCreate(Sender: TObject); procedure FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer); procedure FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer); private { Private declarations } FHasTrackMouse: Boolean; procedure WM_MouseLeave(var message: TMessage); message WM_MouseLeave; public { Public declarations } procedure NoTitleBoardMove; { Public declarations } end;var Form2: TForm2;implementation{$R *.dfm}procedure TForm2.WM_MouseLeave(var message: TMessage);begin inherited; Self.Hide; FHasTrackMouse := False;end;procedure TForm2.FormClose(Sender: TObject; var Action: TCloseAction);begin Form2 := nil; Action := caFree;end;procedure TForm2.FormCreate(Sender: TObject);begin// Self.FormStyle := fsMDIChild; FHasTrackMouse := False;end;procedure TForm2.FormMouseMove(Sender: TObject; Shift: TShiftState; X, Y: Integer);var a: tagTRACKMOUSEEVENT;begin if not FHasTrackMouse then begin a.cbSize := SizeOf(a); a.dwFlags := TME_LEAVE; a.hwndTrack := Self.Handle; // a.dwHoverTime := FHasTrackMouse := TrackMouseEvent(a); end;end;procedure TForm2.NoTitleBoardMove;begin ReleaseCapture; SendMessage(Handle, WM_SYSCOMMAND, SC_MOVE + HTCAPTION, 0);end;procedure TForm2.FormMouseDown(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);begin NoTitleBoardMove;end;end.
 
感谢两位的思路。原先我认为除了sendmessage和timer之外,可能有更简单的方法。我只能放弃这个思路,换个做法:)
 
后退
顶部