-
Notifications
You must be signed in to change notification settings - Fork 12
/
Tray.Icon.Notifications.pas
117 lines (92 loc) · 3.34 KB
/
Tray.Icon.Notifications.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
unit Tray.Icon.Notifications;
interface
uses
Winapi.Windows, Winapi.ShellAPI,
System.Classes,
Core.UI.Notifications,
Tray.Icon;
type
TTrayNotificationManager = class(TInterfacedObject, INotification)
private
FTitle: string;
FTrayIcon: TTrayIcon;
FCallback: TNotificationCallback;
function _GetTitle: string;
procedure _SetTitle(const Value: string);
procedure TrayIconBalloonClick(Sender: TObject);
public
constructor Create(TrayIcon: TTrayIcon);
function Notify(Msg: string): Boolean; overload; inline;
function Notify(Msg: string; Callback: TNotificationCallback): Boolean; overload; inline;
function Notify(Msg: string; Flags: TNotificationFlags): Boolean; overload; inline;
function Notify(Msg: string; Flags: TNotificationFlags; Callback: TNotificationCallback): Boolean; overload; inline;
function Notify(Msg: string; Title: string): Boolean; overload; inline;
function Notify(Msg: string; Title: string; Flags: TNotificationFlags): Boolean; overload; inline;
function Notify(Msg: string; Title: string; Flags: TNotificationFlags; Callback: TNotificationCallback): Boolean; overload;
property Title: string read FTitle write FTitle;
property TrayIcon: TTrayIcon read FTrayIcon;
end;
implementation
{ TNotificationManager }
constructor TTrayNotificationManager.Create(TrayIcon: TTrayIcon);
begin
FTrayIcon := TrayIcon;
FTrayIcon.OnBalloonClick2 := TrayIconBalloonClick;
end;
function TTrayNotificationManager.Notify(Msg: string): Boolean;
begin
Result := Notify(Msg, TNotificationCallback(nil));
end;
function TTrayNotificationManager.Notify(Msg: string; Callback: TNotificationCallback): Boolean;
begin
Result := Notify(Msg, [nfInfo], Callback);
end;
function TTrayNotificationManager.Notify(Msg: string;
Flags: TNotificationFlags): Boolean;
begin
Result := Notify(Msg, Flags, TNotificationCallback(nil));
end;
function TTrayNotificationManager.Notify(Msg: string; Flags: TNotificationFlags;
Callback: TNotificationCallback): Boolean;
begin
Result := Notify(Msg, Title, Flags, Callback);
end;
function TTrayNotificationManager.Notify(Msg, Title: string): Boolean;
begin
Result := Notify(Msg, Title, [nfInfo]);
end;
function TTrayNotificationManager.Notify(Msg, Title: string;
Flags: TNotificationFlags): Boolean;
begin
Result := Notify(Msg, Title, Flags, TNotificationCallback(nil));
end;
function TTrayNotificationManager.Notify(Msg, Title: string;
Flags: TNotificationFlags; Callback: TNotificationCallback): Boolean;
var
Niif: DWORD;
begin
if not Assigned(FTrayIcon) then Exit(False);
FCallback := Callback;
Niif := 0;
if nfInfo in Flags then Niif := Niif or NIIF_INFO;
if nfWarning in Flags then Niif := Niif or NIIF_WARNING;
if nfError in Flags then Niif := Niif or NIIF_ERROR;
FTrayIcon.BalloonTitle := Title;
FTrayIcon.BalloonHint := Msg;
FTrayIcon.ShowBalloonHint(Niif);
Result := True;
end;
procedure TTrayNotificationManager.TrayIconBalloonClick(Sender: TObject);
begin
if Assigned(FCallback) then
FCallback(Sender, IDOK);
end;
function TTrayNotificationManager._GetTitle: string;
begin
Result := FTitle;
end;
procedure TTrayNotificationManager._SetTitle(const Value: string);
begin
FTitle := Value;
end;
end.