-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathFMX.Trayicon.Win.pas
456 lines (405 loc) · 10.4 KB
/
FMX.Trayicon.Win.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
unit FMX.Trayicon.Win;
interface
{$IFNDEF MSWINDOWS}
{$HINTS OFF}
{$ENDIF}
uses
System.SysUtils, System.Classes, System.Generics.Collections,
{$IFDEF MSWINDOWS}
FMX.Platform.Win, Winapi.ShellAPI, Winapi.Windows, Winapi.Messages,
{$ENDIF}
FMX.Dialogs, FMX.Menus, FMX.Forms, FMX.Objects, System.Messaging;
{$IFDEF MSWINDOWS}
const
WM_TRAYICON = WM_USER + 1;
{$ENDIF}
type
TBalloonIconType = (None, Info, Warning, Error, User, BigWarning, BigError);
{$IFNDEF MSWINDOWS}
HICON = Int64;
LONG_PTR = Integer;
TNotifyIconData = record
end;
HWND = Integer;
{$ENDIF}
type
TIcon = HICON;
TTrayIconNotify = procedure(ID: Integer) of object;
[ComponentPlatformsAttribute(pidWin32 or pidWin64 or pidWinNX32 or pidWinARM32)]
TFMXTrayIcon = class(TComponent)
type
TTrayList = class(TList<TFMXTrayIcon>)
procedure Delete(TrayIcon: TFMXTrayIcon); overload;
function GetByID(ID: Integer; var TrayIcon: TFMXTrayIcon): Boolean;
end;
private
class var
TrayList: TTrayList;
IDs: Integer;
NeedHook: Boolean;
OldWndProc: LONG_PTR;
private
FIcon: TIcon;
FHICON: HICON;
FHint: string;
FBalloonTitle: string;
FBalloonText: string;
FBalloonIconType: TBalloonIconType;
FNotifyIconData: TNotifyIconData;
FPopupMenu: TPopupMenu;
FPopupOffset: Integer;
FOnClick: TNotifyEvent;
FOnDblClick: TNotifyEvent;
FOnPopup: TNotifyEvent;
FShowing: Boolean;
FAutoShow: Boolean;
FID: Integer;
FIconResource: string;
FShowingPopup: Boolean;
procedure DoOnClick;
procedure DoOnDblClick;
procedure DoOnRightClick;
procedure DoOnPopup;
procedure SetAutoShow(const Value: Boolean);
class procedure Hook(Handle: HWND);
class procedure InternalTrayIconClick(ID: Integer);
class procedure InternalTrayIconDblClick(ID: Integer);
class procedure InternalTrayIconRightClick(ID: Integer);
procedure SetID(const Value: Integer);
procedure SetIcon(const Value: TIcon);
procedure UpdateIcon;
procedure SetHint(const Value: string);
procedure UpdateHint;
procedure SetIconResource(const Value: string);
function GetWindowHandle: HWND;
procedure SetPopupMenu(const Value: TPopupMenu);
procedure FOnPopupForm(const Sender: TObject; const M: TMessage);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Show;
procedure Hide;
procedure RecreateIcon(Rehook: Boolean = False);
procedure ShowBalloonHint; overload;
procedure ShowBalloonHint(Title, Text: string; BalloonIcon: TBalloonIconType); overload;
property Icon: TIcon read FIcon write SetIcon;
procedure LoadIconFromResources(ResourceName: string);
property WindowHandle: HWND read GetWindowHandle;
protected
procedure Loaded; override;
published
property Hint: string read FHint write SetHint;
property BalloonText: string read FBalloonText write FBalloonText;
property BalloonTitle: string read FBalloonTitle write FBalloonTitle;
property BalloonIconType: TBalloonIconType read FBalloonIconType write FBalloonIconType default TBalloonIconType.None;
property PopupOffset: Integer read FPopupOffset write FPopupOffset default 0;
property PopupMenu: TPopupMenu read FPopupMenu write SetPopupMenu;
property AutoShow: Boolean read FAutoShow write SetAutoShow default True;
property ID: Integer read FID write SetID;
property OnClick: TNotifyEvent read FOnClick write FOnClick;
property OnDblClick: TNotifyEvent read FOnDblClick write FOnDblClick;
property OnPopup: TNotifyEvent read FOnPopup write FOnPopup;
property IconResource: string read FIconResource write SetIconResource;
end;
procedure Register;
implementation
uses
FMX.Types, FMX.Controls;
procedure Register;
begin
RegisterComponents('Win32', [TFMXTrayIcon]);
end;
{$IFDEF MSWINDOWS}
function HookWndProc(HWND: HWND; Msg: UINT; WParam: WParam; LParam: LParam): LRESULT; stdcall;
begin
try
if Msg = WM_TRAYICON then
begin
case LParam of
WM_LBUTTONDBLCLK:
TFMXTrayIcon.InternalTrayIconDblClick(WParam);
WM_LBUTTONUP:
TFMXTrayIcon.InternalTrayIconClick(WParam);
WM_RBUTTONUP:
TFMXTrayIcon.InternalTrayIconRightClick(WParam);
end;
end;
except
end;
Result := CallWindowProc(Ptr(TFMXTrayIcon.OldWndProc), HWND, Msg, WParam, LParam);
end;
{$ENDIF}
class procedure TFMXTrayIcon.Hook;
begin
{$IFDEF MSWINDOWS}
if NeedHook then
begin
OldWndProc := GetWindowLongPtr(Handle, GWL_WNDPROC);
SetWindowLongPtr(Handle, GWL_WNDPROC, LONG_PTR(@HookWndProc));
NeedHook := False;
end;
{$ENDIF}
end;
class procedure TFMXTrayIcon.InternalTrayIconClick(ID: Integer);
var
Tray: TFMXTrayIcon;
begin
if TrayList.GetByID(ID, Tray) then
Tray.DoOnClick;
end;
class procedure TFMXTrayIcon.InternalTrayIconDblClick(ID: Integer);
var
Tray: TFMXTrayIcon;
begin
if TrayList.GetByID(ID, Tray) then
Tray.DoOnDblClick;
end;
class procedure TFMXTrayIcon.InternalTrayIconRightClick(ID: Integer);
var
Tray: TFMXTrayIcon;
begin
if TrayList.GetByID(ID, Tray) then
Tray.DoOnRightClick;
end;
constructor TFMXTrayIcon.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FShowingPopup := False;
TMessageManager.DefaultManager.SubscribeToMessage(TFormBeforeShownMessage, FOnPopupForm);
Inc(IDs);
FID := IDs;
FShowing := False;
FAutoShow := True;
FPopupOffset := 0;
{$IFDEF MSWINDOWS}
FHICON := GetClassLong(WindowHandle, GCL_HICONSM);
{$ENDIF}
//
TrayList.Add(Self);
end;
procedure TFMXTrayIcon.SetAutoShow(const Value: Boolean);
begin
FAutoShow := Value;
end;
procedure TFMXTrayIcon.SetHint(const Value: string);
begin
FHint := Value;
if FShowing then
UpdateHint;
end;
procedure TFMXTrayIcon.SetIcon(const Value: TIcon);
begin
FIcon := Value;
{$IFDEF MSWINDOWS}
if FIcon <> 0 then
FHICON := FIcon
else
FHICON := GetClassLong(WindowHandle, GCL_HICONSM);
if FShowing then
UpdateIcon;
{$ENDIF}
end;
procedure TFMXTrayIcon.SetIconResource(const Value: string);
begin
FIconResource := Value;
LoadIconFromResources(Value);
end;
procedure TFMXTrayIcon.SetID(const Value: Integer);
begin
FID := Value;
end;
procedure TFMXTrayIcon.SetPopupMenu(const Value: TPopupMenu);
begin
FPopupMenu := Value;
end;
procedure TFMXTrayIcon.RecreateIcon;
begin
{$IFDEF MSWINDOWS}
if Rehook then
TFMXTrayIcon.NeedHook := True;
FShowing := True;
with FNotifyIconData do
begin
cbSize := SizeOf;
Wnd := WindowHandle;
uID := FID;
uFlags := NIF_MESSAGE + NIF_ICON + NIF_TIP;
dwInfoFlags := NIIF_NONE;
uCallbackMessage := WM_TRAYICON;
hIcon := FHICON;
StrLCopy(szTip, PChar(FHint), High(szTip));
end;
Shell_NotifyIcon(NIM_ADD, @FNotifyIconData);
if Owner is TForm then
Hook(WindowHandle);
{$ENDIF}
end;
procedure TFMXTrayIcon.Show;
begin
{$IFDEF MSWINDOWS}
if FShowing then
Exit;
RecreateIcon;
{$ENDIF}
end;
procedure TFMXTrayIcon.ShowBalloonHint;
begin
{$IFDEF MSWINDOWS}
with FNotifyIconData do
begin
StrLCopy(szInfo, PChar(FBalloonText), High(szInfo));
StrLCopy(szInfoTitle, PChar(FBalloonTitle), High(szInfoTitle));
dwInfoFlags := Ord(FBalloonIconType);
uFlags := NIF_INFO;
end;
Shell_NotifyIcon(NIM_MODIFY, @FNotifyIconData);
{$ENDIF}
end;
procedure TFMXTrayIcon.ShowBalloonHint(Title, Text: string; BalloonIcon: TBalloonIconType);
begin
FBalloonText := Text;
FBalloonTitle := Title;
FBalloonIconType := BalloonIcon;
ShowBalloonHint;
end;
procedure TFMXTrayIcon.UpdateIcon;
begin
{$IFDEF MSWINDOWS}
with FNotifyIconData do
begin
hIcon := FHICON;
uFlags := NIF_ICON;
end;
Shell_NotifyIcon(NIM_MODIFY, @FNotifyIconData);
{$ENDIF}
end;
procedure TFMXTrayIcon.UpdateHint;
begin
{$IFDEF MSWINDOWS}
with FNotifyIconData do
begin
StrLCopy(szTip, PChar(FHint), High(szTip));
uFlags := NIF_TIP;
end;
Shell_NotifyIcon(NIM_MODIFY, @FNotifyIconData);
{$ENDIF}
end;
procedure TFMXTrayIcon.Hide;
begin
FShowing := False;
{$IFDEF MSWINDOWS}
Shell_NotifyIcon(NIM_DELETE, @FNotifyIconData);
{$ENDIF}
end;
procedure TFMXTrayIcon.Loaded;
begin
inherited;
if not (csDesigning in ComponentState) then
begin
if FAutoShow then
Show;
end;
end;
procedure TFMXTrayIcon.LoadIconFromResources(ResourceName: string);
begin
{$IFDEF MSWINDOWS}
Icon := LoadIcon(hInstance, PChar(ResourceName));
{$ENDIF}
end;
destructor TFMXTrayIcon.Destroy;
begin
TMessageManager.DefaultManager.Unsubscribe(TFormBeforeShownMessage, FOnPopupForm);
if FShowing then
Hide;
TrayList.Delete(Self);
inherited;
end;
procedure TFMXTrayIcon.DoOnClick;
begin
if Assigned(FOnClick) then
FOnClick(Self);
end;
procedure TFMXTrayIcon.DoOnDblClick;
begin
if Assigned(FOnDblClick) then
FOnDblClick(Self);
end;
procedure TFMXTrayIcon.DoOnPopup;
{$IFDEF MSWINDOWS}
var
CurPos: TPoint;
begin
SetForegroundWindow(ApplicationHWND);
GetCursorPos(CurPos);
if Assigned(FPopupMenu) then
begin
FPopupMenu.CloseMenu;
FShowingPopup := True;
try
FPopupMenu.Popup(CurPos.X, CurPos.Y - FPopupOffset);
finally
FShowingPopup := False;
end;
end;
{$ELSE}
begin
{$ENDIF}
end;
procedure TFMXTrayIcon.DoOnRightClick;
begin
if Assigned(FOnPopup) then
FOnPopup(Self)
else
DoOnPopup;
end;
procedure TFMXTrayIcon.FOnPopupForm(const Sender: TObject; const M: TMessage);
var
Msg: TFormBeforeShownMessage absolute M;
begin
if FShowingPopup and (Msg.Value is TCustomPopupForm) then
begin
SetWindowPos(
FormToHWND(Msg.Value),
HWND_TOPMOST, 0, 0, 0, 0,
SWP_NOSIZE or SWP_NOMOVE or SWP_NOACTIVATE
);
end;
end;
function TFMXTrayIcon.GetWindowHandle: HWND;
begin
Result := FmxHandleToHWND((Owner as TForm).Handle);
end;
{ TTrayIcon.TTrayList }
procedure TFMXTrayIcon.TTrayList.Delete(TrayIcon: TFMXTrayIcon);
var
i: Integer;
begin
for i := 0 to Self.Count - 1 do
if Self[i] = TrayIcon then
begin
Self.Delete(i);
Break;
end;
end;
function TFMXTrayIcon.TTrayList.GetByID(ID: Integer; var TrayIcon: TFMXTrayIcon): Boolean;
var
i: Integer;
begin
Result := False;
for i := 0 to Self.Count - 1 do
begin
if Self[i].ID = ID then
begin
Result := True;
TrayIcon := Self[i];
Exit;
end;
end;
end;
initialization
TFMXTrayIcon.TrayList := TFMXTrayIcon.TTrayList.Create;
TFMXTrayIcon.NeedHook := True;
TFMXTrayIcon.IDs := 0;
finalization
TFMXTrayIcon.TrayList.Free;
end.