-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtrayicon.cpp
229 lines (175 loc) · 7.46 KB
/
trayicon.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
//+-------------------------------------------------------------------------
//
// TaskMan - NT TaskManager
// Copyright (C) Microsoft
//
// File: trayicon.CPP
//
// History: Jan-27-96 DavePl Created
//
//--------------------------------------------------------------------------
#include "precomp.h"
/*++ TrayThreadMessageLoop (WORKER THREAD CODE)
Routine Description:
Waits for messages telling it a notification packet is ready
in the queue, then dispatches it to the tray
Mar-27-95 Davepl Created
May-28-99 Jonburs Check for NIM_DELETE during PM_QUITTRAYTHREAD
--*/
DWORD TrayThreadMessageLoop(LPVOID)
{
MSG msg;
//
// Loop forever and process our messages
//
while( GetMessage( &msg, NULL, 0, 0 ) )
{
switch(msg.message)
{
case PM_INITIALIZEICONS:
{
//
// Add the tray icons to the tray icon cache by adding them all hidden.
//
NOTIFYICONDATA NotifyIconData;
ZeroMemory( &NotifyIconData, sizeof(NotifyIconData) );
NotifyIconData.cbSize = sizeof(NotifyIconData);
NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_STATE;
NotifyIconData.dwState = NIS_HIDDEN;
NotifyIconData.dwStateMask = NotifyIconData.dwState;
NotifyIconData.hWnd = g_hMainWnd;
NotifyIconData.uCallbackMessage = PWM_TRAYICON;
for ( UINT idx = 0; idx < g_cTrayIcons; idx ++ )
{
NotifyIconData.uID = ~idx; // anything but zero
NotifyIconData.hIcon = g_aTrayIcons[ idx ];
Shell_NotifyIcon( NIM_ADD, &NotifyIconData );
}
//
// We now add the zero-th icon which we will used to refer to the hidden
// cached icons we added above. This is the visible icon that users see
// in notification area.
//
NotifyIconData.uFlags = NIF_MESSAGE | NIF_ICON | NIF_TIP;
NotifyIconData.uID = 0;
NotifyIconData.hIcon = g_aTrayIcons[ 0 ];
//
// Initialize with the app title, so that the tray knows that it is
// task manager starting up, rather than "CPU Usage blah blah.."...
//
LoadString( g_hInstance, IDS_APPTITLE, NotifyIconData.szTip, ARRAYSIZE(NotifyIconData.szTip) );
Shell_NotifyIcon( NIM_ADD, &NotifyIconData );
}
break;
case PM_NOTIFYWAITING:
{
NOTIFYICONDATA NotifyIconData;
UINT uIcon = (UINT) msg.wParam;
LPCWSTR pszTipText = (LPCWSTR) msg.lParam;
//
// We need to update the icon. To do this, we tell the tray to
// use one of the icons we cached using NIS_HIDDEN into the
// zero-th position and make it visible. The hIcon indicates
// which icon to retrieve and display.
//
ZeroMemory( &NotifyIconData, sizeof(NotifyIconData) );
NotifyIconData.cbSize = sizeof(NotifyIconData);
NotifyIconData.hWnd = g_hMainWnd;
// NotifyIconData.uID = 0; - zero'ed above
NotifyIconData.uFlags = NIF_STATE | NIF_ICON;
NotifyIconData.dwStateMask = NIS_SHAREDICON;
NotifyIconData.dwState = NotifyIconData.dwStateMask;
NotifyIconData.hIcon = g_aTrayIcons[ uIcon ];
//
// If there is tool tip data to update, add it here and free
// the buffer.
//
if ( NULL != pszTipText)
{
NotifyIconData.uFlags |= NIF_TIP;
StringCchCopy( NotifyIconData.szTip, ARRAYSIZE(NotifyIconData.szTip), pszTipText );
HeapFree( GetProcessHeap( ), 0, (LPVOID) pszTipText );
}
Shell_NotifyIcon( NIM_MODIFY, &NotifyIconData );
}
break;
case PM_QUITTRAYTHREAD:
{
//
// Remove the hidden tray icons.
//
NOTIFYICONDATA NotifyIconData;
ZeroMemory( &NotifyIconData, sizeof(NotifyIconData) );
NotifyIconData.cbSize = sizeof(NotifyIconData);
NotifyIconData.hWnd = g_hMainWnd;
for ( UINT idx = 0; idx < g_cTrayIcons; idx ++ )
{
NotifyIconData.uID = ~idx;
Shell_NotifyIcon( NIM_DELETE, &NotifyIconData );
}
//
// Before we leave, update the tool tip so the "notification
// area manager" has something better than "CPU Usage: 49%"
// to show.
//
LoadString( g_hInstance, IDS_APPTITLE, NotifyIconData.szTip, ARRAYSIZE(NotifyIconData.szTip) );
NotifyIconData.uID = 0;
NotifyIconData.uFlags = NIF_TIP;
Shell_NotifyIcon( NIM_MODIFY, &NotifyIconData );
//
// And now delete the last icon (the one we actually show).
//
NotifyIconData.uFlags = 0;
Shell_NotifyIcon( NIM_DELETE, &NotifyIconData );
g_idTrayThread = 0;
PostQuitMessage(0);
}
break;
default:
ASSERT(0 && "Taskman tray worker got unexpected message");
break;
}
}
return 0;
}
/*++ Tray_Notify (MAIN THREAD CODE)
Routine Description:
Handles notifications sent by the tray
Revision History:
Jan-27-95 Davepl Created
--*/
void Tray_Notify(HWND hWnd, LPARAM lParam)
{
switch (lParam)
{
case WM_LBUTTONDBLCLK:
ShowRunningInstance();
break;
case WM_RBUTTONDOWN:
{
HMENU hPopup = LoadPopupMenu(g_hInstance, IDR_TRAYMENU);
// Display the tray icons context menu at the current cursor location
if (hPopup)
{
POINT pt;
GetCursorPos(&pt);
if (IsWindowVisible(g_hMainWnd))
{
DeleteMenu(hPopup, IDM_RESTORETASKMAN, MF_BYCOMMAND);
}
else
{
SetMenuDefaultItem(hPopup, IDM_RESTORETASKMAN, FALSE);
}
CheckMenuItem(hPopup, IDM_ALWAYSONTOP,
MF_BYCOMMAND | (g_Options.m_fAlwaysOnTop ? MF_CHECKED : MF_UNCHECKED));
SetForegroundWindow(hWnd);
g_fInPopup = TRUE;
TrackPopupMenuEx(hPopup, 0, pt.x, pt.y, hWnd, NULL);
g_fInPopup = FALSE;
DestroyMenu(hPopup);
}
break;
}
}
}