-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathtrayframework.c
More file actions
226 lines (203 loc) · 6.34 KB
/
trayframework.c
File metadata and controls
226 lines (203 loc) · 6.34 KB
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
// Headers
#include <windows.h>
#include <tchar.h>
#include <commctrl.h>
#include "resource.h"
// Libs
#pragma comment(lib, "comctl32.lib")
// Various consts & Defs
#define WM_USER_SHELLICON WM_USER + 1
#define WM_TASKBAR_CREATE RegisterWindowMessage(_T("TaskbarCreated"))
// Globals
HWND hWnd;
HINSTANCE hInst;
NOTIFYICONDATA structNID;
BOOL Enabled;
/*
Name: ... AboutDlgProc(...)
Desc: proccess the about dialog
*/
BOOL CALLBACK AboutDlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
switch(Message)
{
case WM_INITDIALOG:
return TRUE;
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case IDOK:
EndDialog(hwnd, IDOK);
break;
}
break;
default:
return FALSE;
}
return TRUE;
}
/* ================================================================================================================== */
/*
Name: ... WndProc(...)
Desc: Main hidden "Window" that handles the messaging for our system tray
*/
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
{
POINT lpClickPoint;
if(Message == WM_TASKBAR_CREATE) { // Taskbar has been recreated (Explorer crashed?)
// Display tray icon
if(!Shell_NotifyIcon(NIM_ADD, &structNID)) {
MessageBox(NULL, "Systray Icon Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
DestroyWindow(hWnd);
return -1;
}
}
switch(Message)
{
case WM_DESTROY:
Shell_NotifyIcon(NIM_DELETE, &structNID); // Remove Tray Item
PostQuitMessage(0); // Quit
break;
case WM_USER_SHELLICON: // sys tray icon Messages
switch(LOWORD(lParam))
{
case WM_RBUTTONDOWN:
{
HMENU hMenu, hSubMenu;
// get mouse cursor position x and y as lParam has the Message itself
GetCursorPos(&lpClickPoint);
// Load menu resource
hMenu = LoadMenu(hInst, MAKEINTRESOURCE(IDR_POPUP_MENU));
if(!hMenu)
return -1; // !0, message not successful?
// Select the first submenu
hSubMenu = GetSubMenu(hMenu, 0);
if(!hSubMenu) {
DestroyMenu(hMenu); // Be sure to Destroy Menu Before Returning
return -1;
}
// Set Enabled State
if(Enabled)
CheckMenuItem(hMenu, ID_POPUP_ENABLE, MF_BYCOMMAND | MF_CHECKED);
else
CheckMenuItem(hMenu, ID_POPUP_ENABLE, MF_BYCOMMAND | MF_UNCHECKED);
// Display menu
SetForegroundWindow(hWnd);
TrackPopupMenu(hSubMenu, TPM_LEFTALIGN | TPM_LEFTBUTTON | TPM_BOTTOMALIGN, lpClickPoint.x, lpClickPoint.y, 0, hWnd, NULL);
SendMessage(hWnd, WM_NULL, 0, 0);
// Kill off objects we're done with
DestroyMenu(hMenu);
}
break;
}
break;
case WM_CLOSE:
DestroyWindow(hWnd); // Destroy Window
break;
case WM_COMMAND:
switch(LOWORD(wParam))
{
case ID_POPUP_EXIT:
DestroyWindow(hWnd); // Destroy Window
break;
case ID_POPUP_ABOUT: // Open about box
DialogBox(GetModuleHandle(NULL), MAKEINTRESOURCE(IDD_ABOUT), hwnd, (DLGPROC)AboutDlgProc);
break;
case ID_POPUP_ENABLE: // Toggle Enable
Enabled = !Enabled;
break;
}
break;
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0; // Return 0 = Message successfully proccessed
}
/*
Name: ... WinMain(...)
Desc: Main Entry point
*/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
MSG Msg;
WNDCLASSEX wc;
HANDLE hMutexInstance;
INITCOMMONCONTROLSEX iccex;
// Check for single instance
// ------------------------------
// Note: I recommend to use the GUID Creation Tool for the most unique id
// Tools->Create GUID for Visual Studio .Net 2003
// Or search somewhere in the Platform SDK for other environments
hMutexInstance = CreateMutex(NULL, FALSE,_T("TrayApp-{1EB489D6-6702-43cd-A859-C2BA7DB58B06}"));
if(GetLastError() == ERROR_ALREADY_EXISTS || GetLastError() == ERROR_ACCESS_DENIED)
return 0;
// Copy instance so it can be used globally in other methods
hInst = hInstance;
// Init common controls (if you're using them)
// ------------------------------
// See: http://msdn.microsoft.com/library/default.asp?url=/library/en-us/shellcc/platform/commctls/common/structures/initcommoncontrolsex.asp
iccex.dwSize = sizeof(INITCOMMONCONTROLSEX);
iccex.dwICC = ICC_UPDOWN_CLASS | ICC_LISTVIEW_CLASSES;
if(!InitCommonControlsEx(&iccex)) {
MessageBox(NULL, "Cannot Initialize Common Controls!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Window "class"
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(hInstance, (LPCTSTR)MAKEINTRESOURCE(IDI_TRAYICON));
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "Tray Application";
wc.hIconSm = LoadIcon(hInstance, (LPCTSTR)MAKEINTRESOURCE(IDI_TRAYICON));
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Create the hidden window
hWnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
"Tray Application",
"Tray Application Framework",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
CW_USEDEFAULT,
NULL,
NULL,
hInstance,
NULL);
if(hWnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// tray icon settings
structNID.cbSize = sizeof(NOTIFYICONDATA);
structNID.hWnd = (HWND)hWnd;
structNID.uID = IDI_TRAYICON;
structNID.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
strcpy(structNID.szTip, "Tray Application Tip");
structNID.hIcon = LoadIcon(hInstance, (LPCTSTR)MAKEINTRESOURCE(IDI_TRAYICON));
structNID.uCallbackMessage = WM_USER_SHELLICON;
// Display tray icon
if(!Shell_NotifyIcon(NIM_ADD, &structNID)) {
MessageBox(NULL, "Systray Icon Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return 0;
}
// Set mode to enabled
Enabled = TRUE;
// Message Loop
while(GetMessage(&Msg, NULL, 0, 0))
{
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}