This repository was archived by the owner on Aug 24, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTray.h
More file actions
executable file
·193 lines (155 loc) · 4.86 KB
/
Tray.h
File metadata and controls
executable file
·193 lines (155 loc) · 4.86 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
#ifndef TRAY_H_
#define TRAY_H_
#include <windows.h>
#include <vector>
#include <string>
#include <functional>
using std::vector, std::string, std::function;
class TrayEntry {
function<void(TrayEntry&)> clickHandler;
enum { DIVIDER, MENU_ENTRY, SUBMENU } type;
public:
vector<TrayEntry> submenu;
string text;
bool enabled = true;
bool checked = false;
TrayEntry() { type = DIVIDER; }
TrayEntry(string text, function<void(TrayEntry&)> clickHandler = 0, bool enabled = true, bool checked = false) {
this->text = text;
this->clickHandler = clickHandler;
this->enabled = enabled;
this->checked = checked;
type = MENU_ENTRY;
}
TrayEntry(string text, vector<TrayEntry> submenu) {
this->submenu = submenu;
this->text = text;
type = SUBMENU;
}
friend class Tray;
};
#define WM_TRAY_CB_MSG (WM_USER + 1)
class Tray {
HWND hwnd;
NOTIFYICONDATAA nid = {0};
HMENU menuHandle = 0;
LRESULT HandleMessage(UINT msg, WPARAM wparam, LPARAM lparam) {
switch(msg) {
case WM_CLOSE: DestroyWindow(hwnd); return 0;
case WM_DESTROY: PostQuitMessage(0); return 0;
case WM_TRAY_CB_MSG:
if(lparam == WM_LBUTTONUP || lparam == WM_RBUTTONUP) {
POINT p;
GetCursorPos(&p);
SetForegroundWindow(hwnd);
WORD cmd = TrackPopupMenu(menuHandle, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, p.x, p.y, 0, hwnd, NULL);
SendMessageA(hwnd, WM_COMMAND, cmd, 0);
return 0;
}
break;
case WM_COMMAND:
if(wparam >= 1) {
MENUITEMINFOA item = {
.cbSize = sizeof(MENUITEMINFO),
.fMask = MIIM_ID | MIIM_DATA,
};
if(GetMenuItemInfoA(menuHandle, wparam, FALSE, &item)) {
TrayEntry* entry = (TrayEntry*)item.dwItemData;
if(entry && entry->clickHandler) {
entry->clickHandler(*entry);
update();
}
}
return 0;
}
break;
}
return DefWindowProc(hwnd, msg, wparam, lparam);
}
static LRESULT CALLBACK WindowProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
Tray* pThis = 0;
if(uMsg == WM_NCCREATE) {
pThis = (Tray*)((CREATESTRUCT*)lParam)->lpCreateParams;
pThis->hwnd = hwnd;
SetWindowLongPtrA(hwnd, GWLP_USERDATA, (LONG_PTR)pThis);
} else
pThis = (Tray*)GetWindowLongPtrA(hwnd, GWLP_USERDATA);
if(pThis)
return pThis->HandleMessage(uMsg, wParam, lParam);
else
return DefWindowProcA(hwnd, uMsg, wParam, lParam);
};
HMENU createMenu(vector<TrayEntry>& menu, int& id) {
HMENU newMenu = CreatePopupMenu();
for(int i = 0; i < menu.size(); i++) {
if(menu[i].type == TrayEntry::DIVIDER) {
InsertMenuA(newMenu, id++, MF_SEPARATOR, true, "");
} else {
MENUITEMINFOA item = {0};
item.cbSize = sizeof(MENUITEMINFOA);
item.fMask = MIIM_ID | MIIM_TYPE | MIIM_STATE | MIIM_DATA;
item.fType = 0;
item.fState = 0;
if(menu[i].type == TrayEntry::MENU_ENTRY) {
if(!menu[i].enabled) { item.fState |= MFS_DISABLED; }
if(menu[i].checked) { item.fState |= MFS_CHECKED; }
} else if(menu[i].type == TrayEntry::SUBMENU) {
item.fMask = item.fMask | MIIM_SUBMENU;
item.hSubMenu = createMenu(menu[i].submenu, id);
}
item.wID = id++;
item.dwTypeData = (LPSTR)menu[i].text.c_str();
item.dwItemData = (ULONG_PTR)&menu[i];
InsertMenuItemA(newMenu, item.wID, true, &item);
}
}
return newMenu;
}
public:
vector<TrayEntry> menu;
void update() {
HMENU prev = menuHandle;
int id = 1;
menuHandle = createMenu(menu, id);
SendMessageA(hwnd, WM_INITMENUPOPUP, (WPARAM)menuHandle, 0);
if(prev) DestroyMenu(prev);
}
Tray(string title, string iconPath, vector<TrayEntry> menu) {
this->menu = menu;
WNDCLASSA wc = {0};
wc.lpfnWndProc = WindowProc;
wc.hInstance = GetModuleHandleA(0);
wc.lpszClassName = "TRAY_CLASS_CPP";
RegisterClassA(&wc);
hwnd = CreateWindowExA(0, wc.lpszClassName, NULL, 0, 0, 0, 0, 0, 0, 0, 0, this);
UpdateWindow(hwnd);
nid.cbSize = sizeof(NOTIFYICONDATAA);
nid.hWnd = hwnd;
nid.uID = 0;
nid.uFlags = NIF_ICON | NIF_MESSAGE;
nid.uCallbackMessage = WM_TRAY_CB_MSG;
ExtractIconExA(iconPath.c_str(), 0, 0, &nid.hIcon, 1);
Shell_NotifyIconA(NIM_ADD, &nid);
update();
}
void popup(string title, string message) {
NOTIFYICONDATAA notification = {0};
notification.cbSize = sizeof(NOTIFYICONDATAA);
notification.hWnd = GetWindow(0, 0);
notification.uFlags = NIF_INFO;
notification.dwInfoFlags = NIIF_INFO;
strcpy(notification.szInfoTitle, title.c_str());
strcpy(notification.szInfo, message.c_str());
Shell_NotifyIconA(NIM_ADD, ¬ification);
Shell_NotifyIconA(NIM_DELETE, ¬ification);
}
void run() {
MSG msg = {0};
while(GetMessageA(&msg, 0, 0, 0)) {
TranslateMessage(&msg);
DispatchMessageA(&msg);
}
Shell_NotifyIconA(NIM_DELETE, &nid);
}
};
#endif