-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrayicon.c
324 lines (244 loc) · 9.05 KB
/
trayicon.c
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
//-----------------------------------------------------------------------------
// trayicon.c
//
// Including most ui and message-piping.
// Based on trayicon.c by wharfinger 8/05/2001
//-----------------------------------------------------------------------------
#include "trayicon.h"
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
// More forward declerations...
HICON LoadSmallIcon( HINSTANCE hInstance, UINT uID );
BOOL ShowPopupMenu( HWND hWnd, POINT *curpos, int wDefaultItem );
void OnInitMenuPopup( HWND hWnd, HMENU hMenu, UINT uID );
BOOL OnCommand( HWND hWnd, WORD wID, HWND hCtl );
void OnTrayIconMouseMove( HWND hWnd );
void OnTrayIconRBtnUp( HWND hWnd );
void OnTrayIconLBtnDblClick( HWND hWnd );
void OnClose( HWND hWnd );
//-----------------------------------------------------------------------------
//-----------------------------------------------------------------------------
static BOOL g_bModalState = FALSE;
// Add an icon to the system tray.
void AddTrayIcon( HWND hWnd, UINT uID, UINT uCallbackMsg, UINT uIcon,
LPTSTR pszToolTip )
{
NOTIFYICONDATA nid;
memset( &nid, 0, sizeof( nid ) );
nid.cbSize = sizeof( nid );
nid.hWnd = hWnd;
nid.uID = uID;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = uCallbackMsg;
// Uncomment this if you've got your own icon. GetModuleHandle( NULL )
// gives us our HINSTANCE. I hate globals.
// nid.hIcon = LoadSmallIcon( GetModuleHandle( NULL ), uIcon );
// Comment this if you've got your own icon.
{
TCHAR szIconFile[512];
GetSystemDirectory( szIconFile, sizeof( szIconFile ) );
if ( szIconFile[ lstrlen( szIconFile ) - 1 ] != '\\' )
lstrcat( szIconFile, _T("\\") );
lstrcat( szIconFile, _T("shell32.dll") );
ExtractIconEx( szIconFile, 17, NULL, &(nid.hIcon), 1 );
}
lstrcpy( nid.szTip, pszToolTip );
Shell_NotifyIcon( NIM_ADD, &nid );
}
void ModifyTrayIcon( HWND hWnd, UINT uID, UINT uIcon, LPTSTR pszToolTip )
{
NOTIFYICONDATA nid;
memset( &nid, 0, sizeof( nid ) );
nid.cbSize = sizeof( nid );
nid.hWnd = hWnd;
nid.uID = uID;
if ( uIcon != (UINT)-1 ) {
nid.hIcon = LoadSmallIcon( GetModuleHandle( NULL ), uIcon );
nid.uFlags |= NIF_ICON;
}
if ( pszToolTip ) {
lstrcpy( nid.szTip, pszToolTip );
nid.uFlags |= NIF_TIP;
}
if ( uIcon != (UINT)-1 || pszToolTip )
Shell_NotifyIcon( NIM_MODIFY, &nid );
}
// Remove an icon from the system tray.
void RemoveTrayIcon( HWND hWnd, UINT uID )
{
NOTIFYICONDATA nid;
memset( &nid, 0, sizeof( nid ) );
nid.cbSize = sizeof( nid );
nid.hWnd = hWnd;
nid.uID = uID;
Shell_NotifyIcon( NIM_DELETE, &nid );
}
static LRESULT call_WindowProc_fallback(
HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam )
{
return WindowProc_fallback ?
(*WindowProc_fallback)( hWnd, uMsg, wParam, lParam )
: DefWindowProc( hWnd, uMsg, wParam, lParam );
}
// OUR NERVE CENTER.
static LRESULT CALLBACK WindowProc( HWND hWnd, UINT uMsg, WPARAM wParam,
LPARAM lParam )
{
switch ( uMsg ) {
case WM_CREATE:
AddTrayIcon( hWnd, ID_TRAYICON, APPWM_TRAYICON, 0, THIS_TITLE );
return 0;
case APPWM_NOP:
// There's a long comment in OnTrayIconRBtnUp() which explains
// what we're doing here.
return 0;
// This is the message which brings tidings of mouse events involving
// our tray icon. We defined it ourselves. See AddTrayIcon() for
// details of how we told Windows about it.
case APPWM_TRAYICON:
SetForegroundWindow( hWnd );
switch ( lParam ) {
case WM_MOUSEMOVE:
OnTrayIconMouseMove( hWnd );
return 0;
case WM_RBUTTONUP:
// There's a long comment in OnTrayIconRBtnUp() which
// explains what we're doing here.
OnTrayIconRBtnUp( hWnd );
return 0;
case WM_LBUTTONDBLCLK:
OnTrayIconLBtnDblClick( hWnd );
return 0;
}
return 0;
case WM_COMMAND:
return OnCommand( hWnd, LOWORD(wParam), (HWND)lParam );
case WM_INITMENUPOPUP:
OnInitMenuPopup( hWnd, (HMENU)wParam, lParam );
return 0;
case WM_CLOSE:
OnClose( hWnd );
return call_WindowProc_fallback( hWnd, uMsg, wParam, lParam );
default:
return call_WindowProc_fallback( hWnd, uMsg, wParam, lParam );
}
}
void OnClose( HWND hWnd )
{
// Remove icon from system tray.
RemoveTrayIcon( hWnd, ID_TRAYICON );
if ( app_close_listener )
(*app_close_listener)( hWnd );
PostQuitMessage( 0 );
}
// Create and display our little popupmenu when the user right-clicks on the
// system tray.
BOOL ShowPopupMenu( HWND hWnd, POINT *curpos, int wDefaultItem )
{
HMENU hPop = NULL;
int i = 0;
WORD cmd;
POINT pt;
if ( g_bModalState )
return FALSE;
hPop = CreatePopupMenu();
if ( ! curpos ) {
GetCursorPos( &pt );
curpos = &pt;
}
InsertMenu( hPop, i++, MF_BYPOSITION | MF_STRING, ID_ABOUT, _T("About...") );
InsertMenu( hPop, i++, MF_BYPOSITION | MF_STRING, ID_EXIT, _T("Exit") );
SetMenuDefaultItem( hPop, ID_ABOUT, FALSE );
SetFocus( hWnd );
SendMessage( hWnd, WM_INITMENUPOPUP, (WPARAM)hPop, 0 );
cmd = TrackPopupMenu( hPop, TPM_LEFTALIGN | TPM_RIGHTBUTTON
| TPM_RETURNCMD | TPM_NONOTIFY,
curpos->x, curpos->y, 0, hWnd, NULL );
SendMessage( hWnd, WM_COMMAND, cmd, 0 );
DestroyMenu( hPop );
return cmd;
}
BOOL OnCommand( HWND hWnd, WORD wID, HWND hCtl )
{
if ( g_bModalState )
return 1;
// Have a look at the command and act accordingly
switch ( wID ) {
case ID_ABOUT:
g_bModalState = TRUE;
MessageBox( hWnd, HELP_ABOUT, THIS_TITLE,
MB_ICONINFORMATION | MB_OK );
g_bModalState = FALSE;
return 0;
case ID_EXIT:
PostMessage( hWnd, WM_CLOSE, 0, 0 );
return 0;
default:
// This happens when the right-click menu is canceled.
return 0;
}
}
// When the mouse pointer drifts over the tray icon, a "tooltip" will be
// displayed. Before that happens, we get notified about the movement, so we
// get a chance to set the "tooltip" text to be something useful.
void OnTrayIconMouseMove( HWND hWnd )
{
// stub
}
// Right-click on tray icon displays menu.
void OnTrayIconRBtnUp( HWND hWnd )
{
/*
This SetForegroundWindow() and PostMessage( , APPWM_NOP, , ) are
recommended in some MSDN sample code; apparently there's a bug in most if
not all versions of Windows: Tray icon menus don't vanish properly when
cancelled unless you provide special coddling.
In MSDN, see: "PRB: Menus for Notification Icons Don't Work Correctly",
Q135788:
mk:@ivt:kb/Source/win32sdk/q135788.htm
Example code:
mk:@ivt:pdref/good/code/graphics/gdi/setdisp/c3447_7lbt.htm
Both of these pseudo-URL's are from the July 1998 MSDN. Those geniuses
have since completely re-broken MSDN at least once, so the pseudo-URL's
are useless with more recent MSDN's.
In the April 2000 MSDN, you can search titles for "Menus for
Notification Icons"; "Don't" in the title has been changed to "Do Not",
and searching for either complete title doesn't work anyway. Good old
MSDN.
*/
SetForegroundWindow( hWnd );
ShowPopupMenu( hWnd, NULL, -1 );
PostMessage( hWnd, APPWM_NOP, 0, 0 );
}
void OnTrayIconLBtnDblClick( HWND hWnd )
{
SendMessage( hWnd, WM_COMMAND, ID_ABOUT, 0 );
}
void OnInitMenuPopup( HWND hWnd, HMENU hPop, UINT uID )
{
// stub
}
void RegisterApplicationClass( HINSTANCE hInstance )
{
WNDCLASSEX wclx;
memset( &wclx, 0, sizeof( wclx ) );
wclx.cbSize = sizeof( wclx );
wclx.style = 0;
wclx.lpfnWndProc = &WindowProc;
wclx.cbClsExtra = 0;
wclx.cbWndExtra = 0;
wclx.hInstance = hInstance;
//wclx.hIcon = LoadIcon( hInstance, MAKEINTRESOURCE( IDI_TRAYICON ) );
//wclx.hIconSm = LoadSmallIcon( hInstance, IDI_TRAYICON );
wclx.hCursor = LoadCursor( NULL, IDC_ARROW );
wclx.hbrBackground = (HBRUSH)( COLOR_BTNFACE + 1 ); // COLOR_* + 1 is
// special magic.
wclx.lpszMenuName = NULL;
wclx.lpszClassName = THIS_CLASSNAME;
RegisterClassEx( &wclx );
}
HICON LoadSmallIcon( HINSTANCE hInstance, UINT uID )
{
return LoadImage( hInstance, MAKEINTRESOURCE( uID ), IMAGE_ICON,
16, 16, 0 );
}