-
Notifications
You must be signed in to change notification settings - Fork 0
/
StatusAreaIcon.cpp
292 lines (242 loc) · 7.72 KB
/
StatusAreaIcon.cpp
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
//---------------------------------------------------------------------------
#include <vcl.h>
#pragma hdrstop
#include "StatusAreaIcon.h"
#pragma package(smart_init)
//---------------------------------------------------------------------------
// ValidCtrCheck is used to assure that the components created do not have
// any pure virtual functions.
//
static inline void ValidCtrCheck(TStatusAreaIcon *)
{
new TStatusAreaIcon(NULL);
}
//---------------------------------------------------------------------------
__fastcall TStatusAreaIcon::TStatusAreaIcon(TComponent* Owner)
: TComponent(Owner)
{
FShellVersion = 0;
FVisible = false;
PDLLVERSIONINFO Vers = (PDLLVERSIONINFO)malloc(sizeof(DLLVERSIONINFO));
if (Vers != NULL) {
Vers->cbSize = sizeof(DLLVERSIONINFO);
INITCOMMONCONTROLSEX a;
a.dwSize = sizeof(INITCOMMONCONTROLSEX);
a.dwICC = ICC_TAB_CLASSES;
InitCommonControlsEx(&a);
Shell32Lib = LoadLibrary("shell32.dll");
if (Shell32Lib != NULL) {
TWinCall GetVers = (TWinCall)GetProcAddress(Shell32Lib, "DllGetVersion");
if (GetVers != NULL)
GetVers(Vers);
}
FShellVersion = Vers->dwMajorVersion;
free(Vers);
}
FIconID = 1;
FIconIndex = 0;
CurrIcon = new TIcon();
FText = new AnsiString();
FTitleText = new AnsiString();
IconData = (PMYNOTIFYICONDATA)malloc(sizeof(MYNOTIFYICONDATA));
if (IconData != NULL)
IconData->cbSize = sizeof(MYNOTIFYICONDATA);
SetTooltipType(Standard);
}
//---------------------------------------------------------------------------
__fastcall TStatusAreaIcon::~TStatusAreaIcon() {
if (Shell32Lib != NULL)
FreeLibrary(Shell32Lib);
if (IconData != NULL)
free(IconData);
delete FTitleText;
delete FText;
delete CurrIcon;
}
//---------------------------------------------------------------------------
namespace Statusareaicon
{
void __fastcall PACKAGE Register()
{
TComponentClass classes[1] = {__classid(TStatusAreaIcon)};
RegisterComponents("Celestial", classes, 0);
}
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::MessageHandler(tagMSG &Message, bool &Handled) {
switch(Message.message)
{
case WM_SYSTEM_TRAY_NOTIFY:
switch(Message.lParam)
{
case WM_LBUTTONDOWN:
break;
case WM_LBUTTONUP:
ShowMenu();
Handled = true;
break;
case WM_LBUTTONDBLCLK:
break;
case WM_RBUTTONDOWN:
break;
case WM_RBUTTONUP:
case NIN_KEYSELECT:
case WM_CONTEXTMENU:
ShowMenu();
Handled = true;
break;
case WM_RBUTTONDBLCLK:
break;
case NIN_BALLOONSHOW:
break;
case NIN_BALLOONHIDE:
break;
case NIN_BALLOONTIMEOUT:
break;
case NIN_BALLOONUSERCLICK:
break;
}
}
TComponent::Dispatch(&Message);
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetTooltipType(TTooltipType t) {
if (FShellVersion < 6)
FTooltipType = Standard;
else
FTooltipType = t;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TStatusAreaIcon::GetText() {
return *FText;
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetText(AnsiString t) {
*FText = t;
}
//---------------------------------------------------------------------------
AnsiString __fastcall TStatusAreaIcon::GetTitleText() {
return *FTitleText;
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetTitleText(AnsiString t) {
*FTitleText = t;
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetIconIndex(int i) {
if (FIconList != NULL)
if ((i >= 0) && (i < FIconList->Count))
FIconIndex = i;
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetPopupMenu(TPopupMenu *m) {
FPopupMenu = m;
FPopupMenu->Alignment = paRight;
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetTooltipText(AnsiString ss) {
AnsiString *s = new AnsiString();
AnsiString *ts = new AnsiString();
*s = ss;
if (FTooltipType == Standard) {
if (FShellVersion >= 5)
*s = FText->SubString(1, 127);
else
*s = FText->SubString(1, 63);
strcpy(IconData->szTip, s->c_str());
strcpy(IconData->szInfo, "");
IconData->uFlags &= (!NIF_INFO);
IconData->uFlags |= NIF_TIP;
}
else {
*s = FText->SubString(1, 254);
*ts = FTitleText->SubString(1, 63);
strcpy(IconData->szTip, "");
strcpy(IconData->szInfo, s->c_str());
strcpy(IconData->szInfoTitle, ts->c_str());
IconData->uTimeout = (5 * 1000); // Millisecs - max=30
switch (FBalloonType) {
case Info:
IconData->dwInfoFlags = NIIF_INFO;
break;
case Warning:
IconData->dwInfoFlags = NIIF_WARNING;
break;
case Error:
IconData->dwInfoFlags = NIIF_ERROR;
break;
case None:
default:
IconData->dwInfoFlags = NIIF_NONE;
}
IconData->uFlags &= (!NIF_TIP);
IconData->uFlags |= NIF_INFO;
}
delete ts;
delete s;
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetIcon() {
if (FIconList != NULL) {
if (FIconList->Count > 0) {
IconData->uID = FIconID;
FIconList->GetIcon(FIconIndex, CurrIcon);
IconData->hIcon = CurrIcon->Handle;
IconData->uFlags |= NIF_ICON;
}
}
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::Show() {
IconData->uFlags = 0;
SetTooltipText("");
SetIcon();
IconData->hWnd = Application->Handle;
IconData->uCallbackMessage = WM_SYSTEM_TRAY_NOTIFY;
IconData->uFlags |= NIF_MESSAGE;
if (!FVisible) {
FVisible = Shell_NotifyIcon(NIM_ADD, (PNOTIFYICONDATA)IconData);
if ((FVisible) && (FShellVersion >= 6))
SetVersion();
}
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::Hide() {
IconData->hWnd = Application->Handle;
IconData->uID = FIconID;
if (FVisible)
FVisible = !Shell_NotifyIcon(NIM_DELETE, (PNOTIFYICONDATA)IconData);
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::SetVersion() {
IconData->hWnd = Application->Handle;
IconData->uVersion = NOTIFYICON_VERSION;
Shell_NotifyIcon(NIM_SETVERSION, (PNOTIFYICONDATA)IconData);
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::Update() {
if (FVisible) {
IconData->uFlags = 0;
SetTooltipText(*FText);
SetIcon();
IconData->hWnd = Application->Handle;
Shell_NotifyIcon(NIM_MODIFY, (PNOTIFYICONDATA)IconData);
}
else
Show();
}
//---------------------------------------------------------------------------
void __fastcall TStatusAreaIcon::ShowMenu() {
if (FVisible) {
TPoint *Point = new TPoint();
if (FPopupMenu != NULL) {
SetForegroundWindow(Application->Handle);
if (GetCursorPos((tagPOINT *)Point))
FPopupMenu->Popup(Point->x, Point->y);
else
throw Exception("Menu pop-up operation failed");
}
delete Point;
}
}
//---------------------------------------------------------------------------