-
Notifications
You must be signed in to change notification settings - Fork 29
Expand file tree
/
Copy pathSystem.Tray.Helper.pas
More file actions
134 lines (106 loc) · 2.78 KB
/
System.Tray.Helper.pas
File metadata and controls
134 lines (106 loc) · 2.78 KB
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
unit System.Tray.Helper;
interface
uses System.Classes, System.SysUtils, JvTrayIcon, System.Classes.Helper,
vcl.Forms, WinApi.Windows,
System.SysUtils.Helper, vcl.Menus.helpers, vcl.Menus;
type
IJvTrayIcon = Interface
['{BF0C33CF-49C8-4A21-BCB1-A352CDA29CB6}']
End;
TJvTrayIconHelper = class(TJvTrayIcon, IJvTrayIcon)
private
FPopUp: TPopupmenu;
FForm: TForm;
FOldOnShow: TNotifyEvent;
FClosing: boolean;
procedure DoOnShow(sender: TObject);
procedure SetForm(const AForm: TForm);
procedure SetClosing(const Value: boolean);
public
class function new: IJvTrayIcon;
constructor create(AOwner: TComponent); override;
function createItem(ACaption: string; AProc: TProc; AIdx: integer = -1)
: TMenuItem;
property Closing: boolean read FClosing write SetClosing;
end;
function CreateTrayIcon(AForm:TForm):TJvTrayIconHelper;
implementation
function CreateTrayIcon(AForm:TForm):TJvTrayIconHelper;
begin
result :=TJvTrayIconHelper.Create(AForm);
end;
{ TJvTrayIconHelper }
constructor TJvTrayIconHelper.create(AOwner: TComponent);
begin
inherited;
FPopUp := TPopupmenu.create(self);
if assigned(AOwner) and AOwner.InheritsFrom(TForm) then
begin
SetForm(TForm(AOwner));
end;
FPopUp.AutoPopup := true;
PopupMenu := FPopUp;
SetWindowLong(Application.Handle,GWL_EXSTYLE,WS_EX_TOOLWINDOW);
end;
function TJvTrayIconHelper.createItem(ACaption: string; AProc: TProc;
AIdx: integer): TMenuItem;
begin
FPopUp.CreateItemAnonimous(ACaption, AProc, AIdx);
end;
procedure TJvTrayIconHelper.DoOnShow(sender: TObject);
begin
if assigned(FOldOnShow) then
FOldOnShow(sender);
Active := true;
if assigned(FForm) then
begin
FForm.WindowState := wsMinimized;
end;
ShowWindow(Application.Handle, SW_HIDE);
end;
class function TJvTrayIconHelper.new: IJvTrayIcon;
begin
result := TJvTrayIconHelper.create(nil);
end;
procedure TJvTrayIconHelper.SetClosing(const Value: boolean);
begin
FClosing := Value;
end;
procedure TJvTrayIconHelper.SetForm(const AForm: TForm);
begin
if FForm = AForm then
exit;
if assigned(FForm) then
begin // desligar
FPopUp.Items.Clear;
FForm.OnShow := FOldOnShow;
FForm := nil;
end;
FForm := AForm;
FOldOnShow := FForm.OnShow;
FForm.OnShow := DoOnShow;
FPopUp.CreateItemAnonimous('&Abrir',
procedure
begin
if assigned(FForm) then
begin
FForm.WindowState := wsNormal;
FForm.Show;
end;
end);
FPopUp.CreateItemAnonimous('&Minimizar',
procedure
begin
if assigned(FForm) then
FForm.WindowState := wsMinimized;
end);
FPopUp.AddSeparator();
FPopUp.CreateItemAnonimous('&Sair',
procedure
begin
FClosing := true;
if assigned(FForm) then
FForm.Close;
end);
end;
end.