Author: Zswang Wangjihu
Auto Hide Form
Answer:
1 unit Unit1;
2
3 interface
4
5 uses
6 Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
7 Dialogs, StdCtrls, ExtCtrls;
8
9 type
10 TForm1 = class(TForm)
11 Timer1: TTimer;
12 Memo1: TMemo;
13 procedure FormCreate(Sender: TObject);
14 procedure Timer1Timer(Sender: TObject);
15 private
16 { Private declarations }
17 FAnchors: TAnchors;
18 procedure WMMOVING(var Msg: TMessage); message WM_MOVING;
19 public
20 { Public declarations }
21 end;
22
23 var
24 Form1: TForm1;
25
26 implementation
27
28 {$R *.dfm}
29
30 uses Math;
31
32 procedure TForm1.WMMOVING(var Msg: TMessage);
33 begin
34 inherited;
35 with PRect(Msg.LParam)^ do
36 begin
37 Left := Min(Max(0, Left), Screen.Width - Width);
38 Top := Min(Max(0, Top), Screen.Height - Height);
39 Right := Min(Max(Width, Right), Screen.Width);
40 Bottom := Min(Max(Height, Bottom), Screen.Height);
41 FAnchors := [];
42 if Left = 0 then
43 Include(FAnchors, akLeft);
44 if Right = Screen.Width then
45 Include(FAnchors, akRight);
46 if Top = 0 then
47 Include(FAnchors, akTop);
48 if Bottom = Screen.Height then
49 Include(FAnchors, akBottom);
50 Timer1.Enabled := FAnchors <> [];
51 end;
52 end;
53
54 procedure TForm1.FormCreate(Sender: TObject);
55 begin
56 Timer1.Enabled := False;
57 Timer1.Interval := 200;
58 FormStyle := fsStayOnTop;
59 end;
60
61 procedure TForm1.Timer1Timer(Sender: TObject);
62 const
63 cOffset = 2;
64 var
65 vHandle: THandle;
66 begin
67 vHandle := WindowFromPoint(Mouse.CursorPos);
68 while (vHandle <> 0) and (vHandle <> Handle) do
69 vHandle := GetParent(vHandle);
70 if vHandle = Handle then
71 begin
72 if akLeft in FAnchors then
73 Left := 0;
74 if akTop in FAnchors then
75 Top := 0;
76 if akRight in FAnchors then
77 Left := Screen.Width - Width;
78 if akBottom in FAnchors then
79 Top := Screen.Height - Height;
80 end
81 else
82 begin
83 if akLeft in FAnchors then
84 Left := -Width + cOffset;
85 if akTop in FAnchors then
86 Top := -Height + cOffset;
87 if akRight in FAnchors then
88 Left := Screen.Width - cOffset;
89 if akBottom in FAnchors then
90 Top := Screen.Height - cOffset;
91 end;
92 end;
93
94 end.
|