forked from thiagovscoelho/rbtray
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRBTray.cpp
395 lines (353 loc) · 11.6 KB
/
RBTray.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
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
// ****************************************************************************
//
// RBTray
// Copyright (C) 1998-2010 Nikolay Redko, J.D. Purcell
// Copyright (C) 2015 Benbuck Nason
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
//
// ****************************************************************************
#include <windows.h>
#include "RBTray.h"
#include "resource.h"
#define MAXTRAYITEMS 64
static UINT WM_TASKBAR_CREATED;
static HINSTANCE _hInstance;
static HMODULE _hLib;
static HWND _hwndHook;
static HWND _hwndItems[MAXTRAYITEMS];
static HWND _hwndForMenu;
int FindInTray(HWND hwnd) {
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i] == hwnd) {
return i;
}
}
return -1;
}
HICON GetWindowIcon(HWND hwnd) {
HICON icon;
if (icon = (HICON)SendMessage(hwnd, WM_GETICON, ICON_SMALL, 0)) {
return icon;
}
if (icon = (HICON)SendMessage(hwnd, WM_GETICON, ICON_BIG, 0)) {
return icon;
}
if (icon = (HICON)GetClassLongPtr(hwnd, GCLP_HICONSM)) {
return icon;
}
if (icon = (HICON)GetClassLongPtr(hwnd, GCLP_HICON)) {
return icon;
}
return LoadIcon(NULL, IDI_WINLOGO);
}
static bool AddToTray(int i) {
NOTIFYICONDATA nid;
ZeroMemory(&nid, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = _hwndHook;
nid.uID = (UINT)i;
nid.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
nid.uCallbackMessage = WM_TRAYCMD;
nid.hIcon = GetWindowIcon(_hwndItems[i]);
GetWindowText(_hwndItems[i], nid.szTip, sizeof(nid.szTip) / sizeof(nid.szTip[0]));
nid.uVersion = NOTIFYICON_VERSION;
if (!Shell_NotifyIcon(NIM_ADD, &nid)) {
return false;
}
if (!Shell_NotifyIcon(NIM_SETVERSION, &nid)) {
Shell_NotifyIcon(NIM_DELETE, &nid);
return false;
}
return true;
}
static bool AddWindowToTray(HWND hwnd) {
int i = FindInTray(NULL);
if (i == -1) {
return false;
}
_hwndItems[i] = hwnd;
return AddToTray(i);
}
static void MinimizeWindowToTray(HWND hwnd) {
// Don't minimize MDI child windows
if ((UINT)GetWindowLongPtr(hwnd, GWL_EXSTYLE) & WS_EX_MDICHILD) {
return;
}
// If hwnd is a child window, find parent window (e.g. minimize button in
// Office 2007 (ribbon interface) is in a child window)
if ((UINT)GetWindowLongPtr(hwnd, GWL_STYLE) & WS_CHILD) {
hwnd = GetAncestor(hwnd, GA_ROOT);
}
// Hide window before AddWindowToTray call because sometimes RefreshWindowInTray
// can be called from inside ShowWindow before program window is actually hidden
// and as a result RemoveWindowFromTray is called which immediately removes just
// added tray icon.
ShowWindow(hwnd, SW_HIDE);
// Add icon to tray if it's not already there
if (FindInTray(hwnd) == -1) {
if (!AddWindowToTray(hwnd)) {
// If there is something wrong with tray icon restore program window.
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
return;
}
}
}
static bool RemoveFromTray(int i) {
NOTIFYICONDATA nid;
ZeroMemory(&nid, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = _hwndHook;
nid.uID = (UINT)i;
if (!Shell_NotifyIcon(NIM_DELETE, &nid)) {
return false;
}
return true;
}
static bool RemoveWindowFromTray(HWND hwnd) {
int i = FindInTray(hwnd);
if (i == -1) {
return false;
}
if (!RemoveFromTray(i)) {
return false;
}
_hwndItems[i] = NULL;
return true;
}
static void RestoreWindowFromTray(HWND hwnd) {
ShowWindow(hwnd, SW_SHOW);
SetForegroundWindow(hwnd);
RemoveWindowFromTray(hwnd);
}
static void CloseWindowFromTray(HWND hwnd) {
// Use PostMessage to avoid blocking if the program brings up a dialog on exit.
// Also, Explorer windows ignore WM_CLOSE messages from SendMessage.
PostMessage(hwnd, WM_CLOSE, 0, 0);
Sleep(50);
if (IsWindow(hwnd)) {
Sleep(50);
}
if (!IsWindow(hwnd)) {
// Closed successfully
RemoveWindowFromTray(hwnd);
}
}
void RefreshWindowInTray(HWND hwnd) {
int i = FindInTray(hwnd);
if (i == -1) {
return;
}
if (!IsWindow(hwnd) || IsWindowVisible(hwnd)) {
RemoveWindowFromTray(hwnd);
} else {
NOTIFYICONDATA nid;
ZeroMemory(&nid, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = _hwndHook;
nid.uID = (UINT)i;
nid.uFlags = NIF_TIP;
GetWindowText(hwnd, nid.szTip, sizeof(nid.szTip) / sizeof(nid.szTip[0]));
Shell_NotifyIcon(NIM_MODIFY, &nid);
}
}
void ExecuteMenu() {
HMENU hMenu;
POINT point;
hMenu = CreatePopupMenu();
if (!hMenu) {
MessageBox(NULL, L"Error creating menu.", L"RBTray", MB_OK | MB_ICONERROR);
return;
}
AppendMenu(hMenu, MF_STRING, IDM_ABOUT, L"About RBTray");
AppendMenu(hMenu, MF_STRING, IDM_EXIT, L"Exit RBTray");
AppendMenu(hMenu, MF_SEPARATOR, 0, NULL); //--------------
AppendMenu(hMenu, MF_STRING, IDM_CLOSE, L"Close Window");
AppendMenu(hMenu, MF_STRING, IDM_RESTORE, L"Restore Window");
GetCursorPos(&point);
SetForegroundWindow(_hwndHook);
TrackPopupMenu(hMenu, TPM_LEFTBUTTON | TPM_RIGHTBUTTON | TPM_RIGHTALIGN | TPM_BOTTOMALIGN, point.x, point.y, 0, _hwndHook, NULL);
PostMessage(_hwndHook, WM_USER, 0, 0);
DestroyMenu(hMenu);
}
BOOL CALLBACK AboutDlgProc(HWND hWnd, UINT Msg, WPARAM wParam, LPARAM lParam) {
switch (Msg) {
case WM_CLOSE:
PostMessage(hWnd, WM_COMMAND, IDCANCEL, 0);
break;
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDOK:
EndDialog(hWnd, TRUE);
break;
case IDCANCEL:
EndDialog(hWnd, FALSE);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
LRESULT CALLBACK HookWndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {
switch (msg) {
case WM_COMMAND:
switch (LOWORD(wParam)) {
case IDM_RESTORE:
RestoreWindowFromTray(_hwndForMenu);
break;
case IDM_CLOSE:
CloseWindowFromTray(_hwndForMenu);
break;
case IDM_ABOUT:
DialogBox(_hInstance, MAKEINTRESOURCE(IDD_ABOUT), _hwndHook, (DLGPROC)AboutDlgProc);
break;
case IDM_EXIT:
SendMessage(_hwndHook, WM_DESTROY, 0, 0);
break;
}
break;
case WM_ADDTRAY:
MinimizeWindowToTray((HWND)lParam);
break;
case WM_REMTRAY:
RestoreWindowFromTray((HWND)lParam);
break;
case WM_REFRTRAY:
RefreshWindowInTray((HWND)lParam);
break;
case WM_TRAYCMD:
switch ((UINT)lParam) {
case NIN_SELECT:
RestoreWindowFromTray(_hwndItems[wParam]);
break;
case WM_CONTEXTMENU:
_hwndForMenu = _hwndItems[wParam];
ExecuteMenu();
break;
case WM_MOUSEMOVE:
RefreshWindowInTray(_hwndItems[wParam]);
break;
}
break;
case WM_HOTKEY:
{
HWND fgWnd = GetForegroundWindow();
if (!fgWnd)
break;
LONG style = GetWindowLong(fgWnd, GWL_STYLE);
if (!(style & WS_MINIMIZEBOX)) {
// skip, no minimize box
break;
}
MinimizeWindowToTray(fgWnd);
break;
}
case WM_DESTROY:
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i]) {
RestoreWindowFromTray(_hwndItems[i]);
}
}
if (_hLib) {
UnRegisterHook();
FreeLibrary(_hLib);
}
PostQuitMessage(0);
break;
default:
if (msg == WM_TASKBAR_CREATED) {
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i]) {
AddToTray(i);
}
}
}
break;
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE /*hPrevInstance*/, _In_ LPSTR /*szCmdLine*/, _In_ int /*iCmdShow*/) {
_hInstance = hInstance;
int argc;
LPWSTR * argv = CommandLineToArgvW(GetCommandLineW(), &argc);
bool shouldExit = false;
bool useHook = true;
for (int a = 0; a < argc; ++a) {
if (!wcscmp(argv[a], L"--exit")) {
shouldExit = true;
}
if (!wcscmp(argv[a], L"--no-hook")) {
useHook = false;
}
}
_hwndHook = FindWindow(NAME, NAME);
if (_hwndHook) {
if (shouldExit) {
SendMessage(_hwndHook, WM_CLOSE, 0, 0);
} else {
MessageBox(NULL, L"RBTray is already running.", L"RBTray", MB_OK | MB_ICONINFORMATION);
}
return 0;
}
if (useHook) {
if (!(_hLib = LoadLibrary(L"RBHook.dll"))) {
MessageBox(NULL, L"Error loading RBHook.dll.", L"RBTray", MB_OK | MB_ICONERROR);
return 0;
}
if (!RegisterHook(_hLib)) {
MessageBox(NULL, L"Error setting hook procedure.", L"RBTray", MB_OK | MB_ICONERROR);
return 0;
}
}
WNDCLASS wc;
wc.style = 0;
wc.lpfnWndProc = HookWndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = NULL;
wc.hCursor = NULL;
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszMenuName = NULL;
wc.lpszClassName = NAME;
if (!RegisterClass(&wc)) {
MessageBox(NULL, L"Error creating window class", L"RBTray", MB_OK | MB_ICONERROR);
return 0;
}
if (!(_hwndHook = CreateWindow(NAME, NAME, WS_OVERLAPPED, 0, 0, 0, 0, (HWND)NULL, (HMENU)NULL, (HINSTANCE)hInstance, (LPVOID)NULL))) {
MessageBox(NULL, L"Error creating window", L"RBTray", MB_OK | MB_ICONERROR);
return 0;
}
for (int i = 0; i < MAXTRAYITEMS; i++) {
_hwndItems[i] = NULL;
}
WM_TASKBAR_CREATED = RegisterWindowMessage(L"TaskbarCreated");
BOOL registeredHotKey = RegisterHotKey(_hwndHook, 0, MOD_ALT | MOD_CONTROL, VK_DOWN);
if (!registeredHotKey) {
MessageBox(NULL, L"Couldn't register hotkey", L"RBTray", MB_OK | MB_ICONERROR);
}
MSG msg;
while (GetMessage(&msg, nullptr, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if (registeredHotKey) {
UnregisterHotKey(_hwndHook, 0);
}
return (int)msg.wParam;
}