forked from HemulGM/Components
-
Notifications
You must be signed in to change notification settings - Fork 0
/
HGM.Popup.pas
185 lines (162 loc) · 4.6 KB
/
HGM.Popup.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
unit HGM.Popup;
interface
uses
Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants,
System.Classes, Vcl.Graphics, Vcl.Controls, Vcl.Forms, Vcl.Dialogs,
Vcl.ExtCtrls, Vcl.Imaging.pngimage;
type
TFormPopup = class;
TPopupStyles = (psShowArrow, psAnimate, psShadow, psFrame);
TPopupStyle = set of TPopupStyles;
TFreeMethod = TProc<TFormPopup>;
TFormPopup = class(TForm)
imgUpArrow: TImage;
imgDownArrow: TImage;
Shape1: TShape;
tmrAutoHide: TTimer;
procedure FormHide(Sender: TObject);
procedure FormShow(Sender: TObject);
procedure FormActivate(Sender: TObject);
procedure FormClick(Sender: TObject);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
procedure tmrAutoHideTimer(Sender: TObject);
protected
procedure WmActivate(var Msg: TWMActivate); message WM_ACTIVATE;
private
FOwner: TForm;
FControl: TWinControl;
FAnimate: Boolean;
FClosing: Boolean;
FFreeMethod: TFreeMethod;
procedure DoRelease;
public
constructor CreatePopup(AOwner: TForm; AControl: TWinControl; FreeMethod: TFreeMethod; X, Y: Integer; Style: TPopupStyle); overload;
constructor CreateDown(AOwner: TForm; AControl: TWinControl; FreeMethod: TFreeMethod; X, Y: Integer; Style: TPopupStyle; HideAfter: Integer = 0); overload;
end;
implementation
{$R *.dfm}
{ TFormPopup }
constructor TFormPopup.CreateDown(AOwner: TForm; AControl: TWinControl; FreeMethod: TFreeMethod; X, Y: Integer; Style: TPopupStyle; HideAfter: Integer);
begin
CreatePopup(AOwner, AControl, FreeMethod, X, Y, Style);
if HideAfter > 0 then
begin
tmrAutoHide.Interval := HideAfter;
tmrAutoHide.Enabled := True;
end;
imgUpArrow.Hide;
imgDownArrow.Show;
Top := Top - Height;
FControl.Top := 1;
end;
procedure TFormPopup.DoRelease;
begin
FControl.Hide;
Winapi.Windows.SetParent(FControl.Handle, FOwner.Handle);
Release;
end;
constructor TFormPopup.CreatePopup(AOwner: TForm; AControl: TWinControl; FreeMethod: TFreeMethod; X, Y: Integer; Style: TPopupStyle);
const
CS_DROPSHADOW = $00020000;
begin
inherited Create(AOwner);
FClosing := False;
FOwner := AOwner;
FControl := AControl;
FAnimate := psAnimate in Style;
FFreeMethod := FreeMethod;
Winapi.Windows.SetParent(FControl.Handle, Handle);
Left := X;
Top := Y;
if psShowArrow in Style then
begin
if psShadow in Style then
SetClassLong(Handle, GCL_STYLE, GetWindowLong(Handle, GCL_STYLE) and CS_DROPSHADOW);
ClientWidth := FControl.Width;
ClientHeight := FControl.Height + imgUpArrow.Height;
FControl.Left := 0;
FControl.Top := imgUpArrow.Height;
Color := clFuchsia;
TransparentColor := True;
imgUpArrow.Show;
end
else
begin
if psShadow in Style then
begin
SetClassLong(Handle, GCL_STYLE, GetWindowLong(Handle, GCL_STYLE) or CS_DROPSHADOW);
Color := $00404040;
end
else
begin
Shape1.Hide;
Color := clBtnFace;
end;
ClientWidth := FControl.Width;
ClientHeight := FControl.Height;
FControl.Left := 0;
FControl.Top := 0;
TransparentColor := False;
end;
if psFrame in Style then
begin
FControl.Left := FControl.Left + 1;
FControl.Top := FControl.Top + 1;
ClientWidth := ClientWidth + 2;
ClientHeight := ClientHeight + 2;
end;
FControl.Show;
FControl.BringToFront;
FControl.SetFocus;
end;
procedure TFormPopup.FormActivate(Sender: TObject);
begin
SendMessage(FOwner.Handle, WM_NCACTIVATE, Integer(True), 0);
end;
procedure TFormPopup.FormClick(Sender: TObject);
begin
Close;
end;
procedure TFormPopup.FormClose(Sender: TObject; var Action: TCloseAction);
begin
if FClosing then
begin
Action := caNone;
Exit;
end
else if Assigned(FFreeMethod) then
FFreeMethod(Self);
FClosing := True;
end;
procedure TFormPopup.FormHide(Sender: TObject);
begin
if FAnimate then
AnimateWindow(Handle, 100, AW_BLEND or AW_HIDE);
DoRelease;
end;
procedure TFormPopup.FormShow(Sender: TObject);
begin
if Left + Width > Screen.DesktopRect.Right then
Left := Screen.DesktopRect.Right - Width;
if Top + Height > Screen.DesktopRect.Bottom then
Top := Screen.DesktopRect.Bottom - Height;
if Left < 0 then
Left := 0;
if Top < 0 then
Top := 0;
if FAnimate then
AnimateWindow(Handle, 100, AW_BLEND);
end;
procedure TFormPopup.tmrAutoHideTimer(Sender: TObject);
begin
if not Self.BoundsRect.Contains(Mouse.CursorPos) then
Close;
end;
procedure TFormPopup.WmActivate(var Msg: TWMActivate);
begin
SendMessage(FOwner.Handle, WM_NCACTIVATE, Ord(Msg.Active <> WA_INACTIVE), 0);
inherited;
if Msg.Active = WA_INACTIVE then
DoRelease;
end;
end.