-
Notifications
You must be signed in to change notification settings - Fork 14
/
Tray.Notify.Window.pas
542 lines (459 loc) · 15.8 KB
/
Tray.Notify.Window.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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
unit Tray.Notify.Window;
interface
uses
Winapi.Windows, Winapi.Messages,
System.SysUtils, System.Variants, System.Classes, System.Types,
Vcl.Graphics, Vcl.Controls, Vcl.Forms,
Core.UI.Controls, Core.UI.Notifications,
Tray.Icon, Tray.Helpers, Tray.Icon.Notifications,
Versions.Helpers;
type
TSystemBorder = (sbDisable, sbDefault, sbWithoutBorder);
TSystemBorderChangedEvent = procedure(Sender: TObject; SystemBorder: TSystemBorder) of object;
TTrayNotifyWindow = class(TCompatibleForm)
strict private const
MSGFLT_ALLOW = 1;
MSGFLT_DISALLOW = 2;
MSGFLT_RESET = 0;
TimerReposition = 1;
TimerLoadIcon = 2;
protected type
tagCHANGEFILTERSTRUCT = record
cbSize: DWORD;
ExtStatus: DWORD;
end;
CHANGEFILTERSTRUCT = tagCHANGEFILTERSTRUCT;
PCHANGEFILTERSTRUCT = ^CHANGEFILTERSTRUCT;
TChangeWindowMessageFilterEx = function (Wnd: HWND; Msg: UINT; action: DWORD;
pChangeFilterStruct: PCHANGEFILTERSTRUCT): BOOL; stdcall;
strict private
TickCountDeactivate: DWORD;
FTrayIcon: TTrayIcon;
FTrayNotification: INotification;
FAeroEnabled: Boolean;
FWindows10OrGreater: Boolean;
FShowFirst: Boolean;
FShowFix: Boolean;
FPanelHeader: TPanel;
FSystemBorder: TSystemBorder;
FOnSystemBorderChanged: TSystemBorderChangedEvent;
FIsLockedOpened: Boolean;
procedure FormReposition(IconRect: TRect; AeroEnabled: Boolean);
procedure InterfaceBuild;
function PixelToDpi(Pixel: Integer): Integer;
procedure TrayIconClick(Sender: TObject);
procedure SetSystemBorder(const Value: TSystemBorder);
procedure SetOnSystemBorderChanged(const Value: TSystemBorderChangedEvent);
protected
function IsAeroEnabled: Boolean;
procedure WMNCHitTest(var Msg: TWMNCHitTest); message WM_NCHITTEST;
procedure WMActivate (var Msg: TMessage); message WM_ACTIVATE;
procedure WMDisplayChange(var Msg: TWMDisplayChange); message WM_DISPLAYCHANGE;
procedure WMDWMCompositionChanged(var Msg: TMessage); message WM_DWMCOMPOSITIONCHANGED;
procedure WMSize(var Msg: TWMSize); message WM_SIZE;
procedure WMShowWindow(var Msg: TWMShowWindow); message WM_SHOWWINDOW;
procedure WMDpiChanged(var Message: TMessage); message WM_DPICHANGED;
procedure WMTimer(var Message: TWMTimer); message WM_TIMER;
procedure DoCreate; override;
procedure KeyDown(var Key: Word; Shift: TShiftState); override;
procedure KeyPress(var Key: Char); override;
procedure LoadIcon; virtual; abstract;
procedure DoSystemBorderChanged(var SystemBorder: TSystemBorder); virtual;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure Reposition;
procedure LockOpened;
procedure UnlockAndClose;
property TrayIcon: TTrayIcon read FTrayIcon;
property TrayNotification: INotification read FTrayNotification;
property PanelHeader: TPanel read FPanelHeader;
property SystemBorder: TSystemBorder read FSystemBorder write SetSystemBorder;
property OnSystemBorderChanged: TSystemBorderChangedEvent read FOnSystemBorderChanged write SetOnSystemBorderChanged;
end;
implementation
uses
Winapi.MultiMon;
{ TTrayNotifyWindow }
constructor TTrayNotifyWindow.Create(AOwner: TComponent);
var
Lib: HMODULE;
ChangeWindowMessageFilterEx: TChangeWindowMessageFilterEx;
begin
inherited;
FWindows10OrGreater := IsWindows10OrGreater;
FShowFirst := True;
FShowFix := False;
FSystemBorder := sbDefault;
FIsLockedOpened := False;
FPanelHeader := TPanel.Create(Self);
with FPanelHeader do
begin
Align := alTop;
BevelOuter := bvNone;
Height := 3;
ParentColor := True;
Parent := Self;
ShowCaption := False;
end;
FTrayIcon := TTrayIcon.Create(Self);
FTrayIcon.OnClick := TrayIconClick;
FTrayIcon.Icon := Application.Icon.Handle;
FTrayNotification := TTrayNotificationManager.Create(FTrayIcon);
Lib := LoadLibrary(user32);
if Lib <> 0 then begin
ChangeWindowMessageFilterEx := GetProcAddress(Lib, 'ChangeWindowMessageFilterEx');
if Assigned(ChangeWindowMessageFilterEx) then begin
ChangeWindowMessageFilterEx(Handle, WM_SHOWWINDOW, MSGFLT_ALLOW, nil);
ChangeWindowMessageFilterEx(Handle, WM_ACTIVATE, MSGFLT_ALLOW, nil);
end;
FreeLibrary(Lib);
end;
InterfaceBuild;
end;
destructor TTrayNotifyWindow.Destroy;
begin
FIsLockedOpened := False;
FPanelHeader.Free;
FTrayIcon.Free;
inherited;
end;
procedure TTrayNotifyWindow.DoCreate;
begin
inherited;
SendMessage(Handle, WM_UPDATEUISTATE, MakeWParam(UIS_SET, UISF_HIDEFOCUS), 0);
FPanelHeader.Top := 0;
FPanelHeader.TabOrder := 0;
DoSystemBorderChanged(FSystemBorder);
// Перемещение окна к иконке в трее
Reposition;
end;
procedure TTrayNotifyWindow.FormReposition(IconRect: TRect;
AeroEnabled: Boolean);
var
Spacing: TRect;
WorkArea, TaskBar: TRect;
Point: TPoint;
MonInfo: TMonitorInfo;
begin
case SystemBorder of
sbDisable:
Spacing.Create(0, 0, 0, 0);
sbWithoutBorder:
begin
if AeroEnabled then
Spacing.Create(PixelToDpi(8), PixelToDpi(8), PixelToDpi(8), PixelToDpi(8))
else
Spacing.Create(0, 0, 0, 0);
end
else
begin
if AeroEnabled and IsWindows10OrGreater then
Spacing.Create(0, PixelToDpi(8), 0, 0)
else if AeroEnabled then
Spacing.Create(PixelToDpi(8), PixelToDpi(8), PixelToDpi(8), PixelToDpi(8))
else
Spacing.Create(0, 0, 0, 0);
end;
end;
if IconRect.IsEmpty then
Point := Mouse.CursorPos
else
Point := IconRect.CenterPoint;
MonInfo.cbSize := SizeOf(TMonitorInfo);
GetMonitorInfo(MonitorFromPoint(Point, MONITOR_DEFAULTTONEAREST), @MonInfo);
WorkArea := MonInfo.rcWork;
if IsTaskbarAutoHideOn then begin
TaskBar := GetTaskbarPos;
// ---X---
if TaskBar.Left > WorkArea.Left then begin
// Справа
if (TaskBar.Left < WorkArea.Right) and
(TaskBar.Left > WorkArea.Left) then
WorkArea.Right := TaskBar.Left;
end else begin
// Слева
if (TaskBar.Right > WorkArea.Left) and
(TaskBar.Right < WorkArea.Right) then
WorkArea.Left := TaskBar.Right;
end;
// ---Y---
if TaskBar.Top > WorkArea.Top then begin
// Внизу
if (TaskBar.Top < WorkArea.Bottom) and
(TaskBar.Top > WorkArea.Top) then
WorkArea.Bottom := TaskBar.Top;
end else begin
// Вверху
if (TaskBar.Bottom > WorkArea.Top) and
(TaskBar.Bottom < WorkArea.Bottom) then
WorkArea.Top := TaskBar.Bottom;
end;
end;
//---X---
if Point.X - Width div 2 - Spacing.Left < WorkArea.Left then
Left := WorkArea.Left + Spacing.Left
else if Point.X + Width div 2 + Spacing.Right > WorkArea.Right then
Left := WorkArea.Right - Width - Spacing.Right
else
Left := Point.X - Self.Width div 2;
//---Y---
if WorkArea.Contains(Point) and IsNotifyIconOverflowWindowVisible then begin
//Иконка на дополнительной панели
if IconRect.Top - Height - (Spacing.Top + Spacing.Bottom) >= WorkArea.Top then
Top := IconRect.Top - Height - Spacing.Bottom
else
Top := IconRect.Bottom + Spacing.Top;
end else begin
//Иконка на панели уведомлений
if Point.Y - Height div 2 - Spacing.Top < WorkArea.Top then
Top := WorkArea.Top + Spacing.Top
else if Point.Y + Height div 2 + Spacing.Bottom > WorkArea.Bottom then
Top := WorkArea.Bottom - Height - Spacing.Bottom
else
Top := Point.Y - Height div 2;
end;
// Корректировка при малом разрешении или высоком DPI
if Left < WorkArea.Left then Left := WorkArea.Left;
if Top < WorkArea.Top then Top := WorkArea.Top;
end;
procedure TTrayNotifyWindow.InterfaceBuild;
begin
FAeroEnabled := IsAeroEnabled;
AutoSize := False;
case SystemBorder of
sbDisable, sbWithoutBorder:
begin
SetWindowLong(Handle, GWL_STYLE, NativeInt(WS_CLIPCHILDREN or WS_CLIPSIBLINGS or WS_POPUP or WS_BORDER));
SetWindowLong(Handle, GWL_EXSTYLE, NativeInt(WS_EX_TOOLWINDOW));
end;
else
begin
if FWindows10OrGreater then begin
SetWindowLong(Handle, GWL_STYLE, NativeInt(WS_CLIPCHILDREN or WS_CLIPSIBLINGS or WS_POPUP or WS_SIZEBOX));
SetWindowLong(Handle, GWL_EXSTYLE, NativeInt(WS_EX_WINDOWEDGE or WS_EX_TOOLWINDOW));
end else if FAeroEnabled then begin
SetWindowLong(Handle, GWL_STYLE, NativeInt(WS_CLIPCHILDREN or WS_CLIPSIBLINGS or WS_POPUP or WS_SIZEBOX));
SetWindowLong(Handle, GWL_EXSTYLE, NativeInt(WS_EX_DLGMODALFRAME or WS_EX_TOOLWINDOW));
end else begin
SetWindowLong(Handle, GWL_STYLE, NativeInt(WS_CLIPCHILDREN or WS_CLIPSIBLINGS or WS_POPUP or WS_BORDER));
SetWindowLong(Handle, GWL_EXSTYLE, NativeInt(WS_EX_TOOLWINDOW));
end;
end;
end;
Height := Height - GetSystemMetrics(SM_CYCAPTION);
if IsWindows10OrGreater then
begin
FPanelHeader.Visible := (FSystemBorder <> sbDefault) or not FAeroEnabled;
FPanelHeader.Top := 0;
end;
AutoSize := True;
Reposition;
end;
function TTrayNotifyWindow.IsAeroEnabled: Boolean;
type
TDwmIsCompositionEnabledFunc = function(out pfEnabled: BOOL): HRESULT; stdcall;
var
IsEnabled: BOOL;
ModuleHandle: HMODULE;
DwmIsCompositionEnabledFunc: TDwmIsCompositionEnabledFunc;
begin
Result := False;
if Win32MajorVersion >= 6 then begin // Vista or Windows 7+
ModuleHandle := LoadLibrary('dwmapi.dll');
if ModuleHandle <> 0 then
try
@DwmIsCompositionEnabledFunc := GetProcAddress(ModuleHandle, 'DwmIsCompositionEnabled');
if Assigned(DwmIsCompositionEnabledFunc) then
if DwmIsCompositionEnabledFunc(IsEnabled) = S_OK then
Result := IsEnabled;
finally
FreeLibrary(ModuleHandle);
end;
end;
end;
function TTrayNotifyWindow.PixelToDpi(Pixel: Integer): Integer;
begin
Result := MulDiv(Pixel, GetCurrentPPI, 96);
end;
procedure TTrayNotifyWindow.KeyDown(var Key: Word; Shift: TShiftState);
begin
inherited;
if (ssAlt in Shift) and (Key = VK_F4) then ShowWindow(Handle, SW_HIDE);
end;
procedure TTrayNotifyWindow.KeyPress(var Key: Char);
begin
inherited;
if (Key = Char(VK_ESCAPE)) and not FIsLockedOpened then ShowWindow(Handle, SW_HIDE);
end;
procedure TTrayNotifyWindow.Reposition;
var
IconRect: TRect;
begin
if Assigned(FTrayIcon) then
begin
IconRect := FTrayIcon.GetIconRect;
if IconRect.IsEmpty then
GetWindowRect(GetTrayNotifyWndToolbar, IconRect);
FormReposition(IconRect, FAeroEnabled);
end;
end;
procedure TTrayNotifyWindow.LockOpened;
begin
ShowWindow(Handle, SW_RESTORE);
FIsLockedOpened := True;
end;
procedure TTrayNotifyWindow.UnlockAndClose;
begin
FIsLockedOpened := False;
ShowWindow(Handle, SW_HIDE);
end;
procedure TTrayNotifyWindow.WMActivate(var Msg: TMessage);
begin
inherited;
if FIsLockedOpened then Exit;
if Msg.wParam = WA_INACTIVE then begin
TickCountDeactivate := GetTickCount;
ShowWindow(Handle, SW_HIDE);
if Assigned(OnDeactivate) then OnDeactivate(Self);
end else
if Assigned(OnActivate) then OnActivate(Self);
end;
procedure TTrayNotifyWindow.WMShowWindow(var Msg: TWMShowWindow);
var
lpStartupInfo: TStartupInfo;
begin
if FShowFix then
begin
Msg.Result := DefWindowProc(Handle, Msg.Msg, TMessage(Msg).WParam, TMessage(Msg).LParam);
Exit;
end;
if FShowFirst then
try
// Исправление запуска в срернутом состоянии
GetStartupInfo(lpStartupInfo);
if (lpStartupInfo.dwFlags and STARTF_USESHOWWINDOW = STARTF_USESHOWWINDOW) and
(lpStartupInfo.wShowWindow <> SW_SHOWDEFAULT) then
begin
FShowFix := True;
try
ShowWindow(Handle, SW_RESTORE);
ShowWindow(Handle, SW_HIDE);
finally
FShowFix := False;
end;
end;
finally
FShowFirst := False;
end;
if Msg.Show then begin
Reposition;
try
Visible := True;
except
end;
Realign;
inherited;
SetActiveWindow(Handle);
if not SetForegroundWindow(Handle) then
begin
// Workaround for Windows 10 Start and Notification Center
AttachThreadInput(GetWindowThreadProcessId(GetForegroundWindow), GetCurrentThreadId, True);
end;
NotifyWinEvent(EVENT_SYSTEM_MENUPOPUPSTART, Handle, OBJID_CLIENT, 0);
end else begin
SelectFirst;
try
Visible := False;
except
end;
inherited;
NotifyWinEvent(EVENT_SYSTEM_MENUPOPUPEND, Handle, OBJID_CLIENT, 0);
SendMessage(Handle, WM_UPDATEUISTATE, MakeWParam(UIS_SET, UISF_HIDEFOCUS), 0);
end;
end;
procedure TTrayNotifyWindow.WMDisplayChange(var Msg: TWMDisplayChange);
begin
inherited;
SetTimer(Handle, TimerReposition, 1000, nil);
end;
procedure TTrayNotifyWindow.WMDWMCompositionChanged(var Msg: TMessage);
begin
inherited;
InterfaceBuild;
end;
procedure TTrayNotifyWindow.WMNCHitTest(var Msg: TWMNCHitTest);
begin
inherited;
if (Msg.Result = HTLEFT) or (Msg.Result = HTRIGHT) or
(Msg.Result = HTTOP) or (Msg.Result = HTBOTTOM) or
(Msg.Result = HTTOPLEFT) or (Msg.Result = HTBOTTOMLEFT) or
(Msg.Result = HTTOPRIGHT) or (Msg.Result = HTBOTTOMRIGHT)
then Msg.Result:=HTBORDER;
end;
procedure TTrayNotifyWindow.WMSize(var Msg: TWMSize);
begin
inherited;
Reposition;
end;
procedure TTrayNotifyWindow.WMDpiChanged(var Message: TMessage);
begin
if not FWindows10OrGreater then
begin
Message.Result := DefWindowProc(Handle, Message.Msg, Message.WParam, Message.LParam);
Exit;
end;
AutoSize := False;
DisableAlign;
try
inherited;
FPanelHeader.Height := 3;
finally
EnableAlign;
AutoSize := True;
if WindowCreated then LoadIcon;
SetTimer(Handle, TimerLoadIcon, 1500, nil); // Workaround for Windows 10 Shell_NotifyIcon bug
end;
end;
procedure TTrayNotifyWindow.WMTimer(var Message: TWMTimer);
begin
Message.Result := DefWindowProc(Handle, Message.Msg, TMessage(Message).WParam, TMessage(Message).LParam);
case Message.TimerID of
TimerReposition: Reposition;
TimerLoadIcon: LoadIcon;
end;
KillTimer(Handle, Message.TimerID);
end;
procedure TTrayNotifyWindow.TrayIconClick(Sender: TObject);
begin
if FIsLockedOpened then Exit;
if IsWindowVisible(Handle) then
ShowWindow(Handle, SW_HIDE) // Скрываем форму
else
if Abs(GetTickCount - TickCountDeactivate) > 200 then
ShowWindow(Handle, SW_RESTORE); // Показываем форму, если с момента деативации прошло > 200 ms
end;
procedure TTrayNotifyWindow.SetSystemBorder(const Value: TSystemBorder);
begin
if FSystemBorder = Value then Exit;
if Value in [Low(TSystemBorder) .. High(TSystemBorder)] then
FSystemBorder := Value
else
FSystemBorder := sbDefault;
if IsWindows10OrGreater then ShowWindow(Handle, SW_HIDE);
InterfaceBuild;
DoSystemBorderChanged(FSystemBorder);
if IsWindows10OrGreater and FIsLockedOpened then ShowWindow(Handle, SW_RESTORE);
end;
procedure TTrayNotifyWindow.SetOnSystemBorderChanged(
const Value: TSystemBorderChangedEvent);
begin
FOnSystemBorderChanged := Value;
DoSystemBorderChanged(FSystemBorder);
end;
procedure TTrayNotifyWindow.DoSystemBorderChanged(var SystemBorder: TSystemBorder);
begin
if Assigned(FOnSystemBorderChanged) then
FOnSystemBorderChanged(Self, SystemBorder);
end;
end.