-
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathRBTray.cpp
283 lines (253 loc) · 7.52 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
// ****************************************************************************
//
// RBTray
// Copyright (C) 1998-2010 Nikolay Redko, J.D. Purcell
//
// 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 void 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;
Shell_NotifyIcon(NIM_ADD, &nid);
Shell_NotifyIcon(NIM_SETVERSION, &nid);
}
static void AddWindowToTray(HWND hwnd) {
int i = FindInTray(NULL);
if (i == -1) return;
_hwndItems[i] = hwnd;
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);
}
// Add icon to tray if it's not already there
if (FindInTray(hwnd) == -1) {
AddWindowToTray(hwnd);
}
}
static void RemoveFromTray(int i) {
NOTIFYICONDATA nid;
ZeroMemory(&nid, sizeof(nid));
nid.cbSize = NOTIFYICONDATA_V2_SIZE;
nid.hWnd = _hwndHook;
nid.uID = (UINT)i;
Shell_NotifyIcon(NIM_DELETE, &nid);
}
static void RemoveWindowFromTray(HWND hwnd) {
int i = FindInTray(hwnd);
if (i == -1) return;
RemoveFromTray(i);
_hwndItems[i] = 0;
}
static void RestoreWindowFromTray(HWND hwnd) {
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)) {
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 | NIF_ICON;
nid.hIcon = GetWindowIcon(hwnd);
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_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"Remove 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);
}
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_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:
SetForegroundWindow(_hwndItems[wParam]);
break;
case WM_CONTEXTMENU:
_hwndForMenu = _hwndItems[wParam];
ExecuteMenu();
break;
case WM_MOUSEMOVE:
RefreshWindowInTray(_hwndItems[wParam]);
break;
}
break;
case WM_DESTROY:
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i]) {
RemoveWindowFromTray(_hwndItems[i]);
}
}
UnRegisterHook();
FreeLibrary(_hLib);
PostQuitMessage(0);
break;
}
if (msg == WM_TASKBAR_CREATED) {
for (int i = 0; i < MAXTRAYITEMS; i++) {
if (_hwndItems[i]) {
AddToTray(i);
}
}
}
return DefWindowProc(hwnd, msg, wParam, lParam);
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR szCmdLine, int iCmdShow) {
WNDCLASS wc;
MSG msg;
_hInstance = hInstance;
_hwndHook = FindWindow(NAME, NAME);
if (_hwndHook) {
if (strstr(szCmdLine, "--exit")) {
SendMessage(_hwndHook, WM_CLOSE, 0, 0);
}
else {
MessageBox(NULL, L"RBTray is already running.", L"RBTray", MB_OK | MB_ICONINFORMATION);
}
return 0;
}
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;
}
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");
while (IsWindow(_hwndHook) && GetMessage(&msg, _hwndHook, 0, 0)) {
for (int i = 0; i < MAXTRAYITEMS; i++) {
RefreshWindowInTray(_hwndItems[i]);
}
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return 0;
}