-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
365 lines (314 loc) · 12.9 KB
/
Copy pathmain.cpp
File metadata and controls
365 lines (314 loc) · 12.9 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
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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
#define _WIN32_WINNT 0x0501 // define _WIN32_WINNT as 0x0500 or greater before including windows.h to have access to GetConsoleWindow() and SetLayeredWindowAttributes()
#define IDI_ICON 1001
#define WM_SHELLICON (WM_USER + 1)
#if defined(UNICODE) && !defined(_UNICODE)
#define _UNICODE
#elif defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif
#include <tchar.h>
#include <windows.h>
#include <shellapi.h>
#include <string>
#include <sstream>
#include <vector>
#include <cmath>
#define ID_SHOW 2000
#define ID_EXIT 2001
#define ID_SEPARATOR 2002
#define ID_OPACITY_50 2003
#define ID_OPACITY_60 2004
#define ID_OPACITY_70 2005
#define ID_OPACITY_80 2006
#define ID_OPACITY_90 2007
#define ID_FULL_SCR 1000 // ID for each monitor goes like this: 1st = 100, 2nd = 101,...
POINT cursor; // Cursor position
BOOL is_hidden = false;
HINSTANCE instance;
HMENU menu_instance;
INT selected_monitor = 0;
INT checked_item = GetPrivateProfileInt(_T("Settings"), _T("checked_item"), ID_OPACITY_80, _T("./config.ini"));
std::vector <HMONITOR> monitorVec;
BOOL CALLBACK MonitorEnumProc(HMONITOR hMonitor, HDC hdcMonitor, LPRECT lprcMonitor, LPARAM dwData)
{
std::vector<HMONITOR>&monitorVec = *reinterpret_cast<std::vector<HMONITOR>*>(dwData);
monitorVec.push_back(hMonitor);
return TRUE;
}
BOOL ShowPopupMenu(HWND hWnd, POINT *curpos, int wDefaultItem)
{
HMENU hPop = CreatePopupMenu();
HMENU hSub = CreatePopupMenu();
menu_instance = hPop;
InsertMenu(hPop, 0, MF_BYPOSITION | MF_STRING, ID_SHOW, _T("Show/Hide"));
InsertMenu(hPop, 1, MF_BYPOSITION | MF_SEPARATOR, ID_SEPARATOR, NULL);
for (int i = 1; i < 6; i++)
{
std::stringstream ss;
ss << (i+4)*10 << " %";
std::string percentage = ss.str();
if (checked_item == (2002 + i))
{
InsertMenu(hPop, (i + 1), MF_BYPOSITION | MF_CHECKED, (2002 + i), percentage.c_str());
}
else
{
InsertMenu(hPop, (i + 1), MF_BYPOSITION | MF_UNCHECKED, (2002 + i), percentage.c_str());
}
}
if (monitorVec.size() > 1)
{
InsertMenu(hPop, 7, MF_BYPOSITION | MF_SEPARATOR, ID_SEPARATOR, NULL);
InsertMenu(hPop, 8, MF_BYPOSITION | MF_POPUP, UINT(hSub), _T("Show on:"));
InsertMenu(hSub, 0, MF_BYPOSITION | MF_STRING, ID_FULL_SCR, _T("All Monitors"));
InsertMenu(hSub, 1, MF_BYPOSITION | MF_SEPARATOR, ID_SEPARATOR, NULL);
for (std::vector <HMONITOR>::iterator it=monitorVec.begin(); it != monitorVec.end(); ++it)
{
INT i = std::distance(monitorVec.begin(), it);
std::stringstream ss;
ss << "Monitor " << i+1;
std::string monitor_num = ss.str();
if (selected_monitor == (100+i))
{
InsertMenu(hSub, (i+2), MF_BYPOSITION | MF_CHECKED, (100+i), monitor_num.c_str());
}
else
{
InsertMenu(hSub, (i+2), MF_BYPOSITION | MF_UNCHECKED, (100+i), monitor_num.c_str());
}
}
}
InsertMenu(hPop, 9, MF_BYPOSITION | MF_SEPARATOR, ID_SEPARATOR, NULL);
InsertMenu(hPop, 10, MF_BYPOSITION | MF_STRING, ID_EXIT, _T("Exit"));
SetMenuDefaultItem(hPop, ID_SHOW, FALSE);
SetMenuDefaultItem(hSub, ID_FULL_SCR, FALSE);
SetForegroundWindow(hWnd);
SendMessage(hWnd, WM_INITMENUPOPUP, (WPARAM)hPop, 0);
GetCursorPos(&cursor);
WORD cmd = TrackPopupMenu(hPop, TPM_LEFTALIGN | TPM_RIGHTBUTTON | TPM_RETURNCMD | TPM_NONOTIFY, cursor.x, cursor.y, 0, hWnd, NULL);
SendMessage(hWnd, WM_COMMAND, cmd, 0);
DestroyMenu(hPop);
return 0;
}
void RemoveTrayIcon(HWND hWnd, UINT uID)
{
NOTIFYICONDATA nid;
nid.hWnd = hWnd;
nid.uID = uID;
Shell_NotifyIcon(NIM_DELETE, &nid);
DestroyIcon(nid.hIcon);
}
void AddTrayIcon(HWND hWnd, UINT uID, UINT uIcon)
{
NOTIFYICONDATA nid;
nid.cbSize = sizeof(NOTIFYICONDATA);
nid.hWnd = hWnd;
nid.uID = uID;
nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
nid.uCallbackMessage = WM_SHELLICON;
nid.hIcon = LoadIcon(instance, _T("MAINICON"));
_tcscpy(nid.szTip, _T("The Amazing Overlay App"));
Shell_NotifyIcon(NIM_ADD, &nid);
}
/* Declare Windows procedure */
LRESULT CALLBACK WindowProcedure(HWND, UINT, WPARAM, LPARAM);
/* Make the class name into a global variable */
TCHAR szClassName[] = _T("MyWindowsApp");
int WINAPI WinMain(HINSTANCE hThisInstance, HINSTANCE hPrevInstance, LPSTR lpszArgument, int nCmdShow)
{
HWND hwnd; // This is the handle for our window
MSG messages; // Here messages to the application are saved
WNDCLASSEX wincl; // Data structure for the windowclass
POINT cursor; // Cursor position
HANDLE file = CreateFile(_T("./config.ini"), GENERIC_WRITE, 0, 0, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, 0);
if (file != INVALID_HANDLE_VALUE)
{
CloseHandle(file);
}
else if (GetLastError() != ERROR_FILE_EXISTS)
{
// file exists
}
EnumDisplayMonitors(NULL, NULL, MonitorEnumProc, (LPARAM)&monitorVec); /* Get monitors count */
GetCursorPos(&cursor);
instance = hThisInstance;
/* The Window structure */
wincl.hInstance = hThisInstance;
wincl.lpszClassName = szClassName;
wincl.lpfnWndProc = WindowProcedure; /* This function is called by windows */
wincl.style = CS_DBLCLKS; /* Catch double-clicks */
wincl.cbSize = sizeof(WNDCLASSEX);
/* Use default icon and mouse-pointer */
// ExtractIconEx( "./icons/icon_256.ico", 0, NULL, &(wincl.hIcon), 1 );
wincl.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wincl.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
wincl.hCursor = LoadCursor(NULL, IDC_ARROW);
wincl.lpszMenuName = NULL; /* No menu */
wincl.cbClsExtra = 0; /* No extra bytes after the window class */
wincl.cbWndExtra = 0; /* structure or the window instance */
/* Use Windows's default colour as the background of the window */
wincl.hbrBackground = (HBRUSH)COLOR_WINDOWTEXT;
/* Register the window class, and if it fails quit the program */
if (!RegisterClassEx(&wincl))
return 0;
/* The class is registered, let's create the program*/
hwnd = CreateWindowEx(
WS_EX_COMPOSITED | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOPMOST | WS_EX_TOOLWINDOW, /* Extended possibilites for variation */
szClassName, /* Classname */
_T("The Amazing Overlay App"), /* Title Text */
WS_POPUP | WS_VISIBLE, /* default window */
cursor.x, cursor.y, 1, 1, /*The position on the screen to create the window and window size*/
HWND_DESKTOP, /* The window is a child-window to desktop */
NULL, /* No menu */
hThisInstance, /* Program Instance handler */
NULL /* No Window Creation data */
);
switch (checked_item)
{
case ID_OPACITY_50:
SetLayeredWindowAttributes(hwnd, 0, 127, LWA_ALPHA);
checked_item = ID_OPACITY_50;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2003"), _T("./config.ini"));
break;
case ID_OPACITY_60:
SetLayeredWindowAttributes(hwnd, 0, 153, LWA_ALPHA);
checked_item = ID_OPACITY_60;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2004"), _T("./config.ini"));
break;
case ID_OPACITY_70:
SetLayeredWindowAttributes(hwnd, 0, 178, LWA_ALPHA);
checked_item = ID_OPACITY_70;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2005"), _T("./config.ini"));
break;
case ID_OPACITY_80:
SetLayeredWindowAttributes(hwnd, 0, 204, LWA_ALPHA);
checked_item = ID_OPACITY_80;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2006"), _T("./config.ini"));
break;
case ID_OPACITY_90:
SetLayeredWindowAttributes(hwnd, 0, 230, LWA_ALPHA);
checked_item = ID_OPACITY_90;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2007"), _T("./config.ini"));
break;
default:
break;
}
/* Make the window visible on the screen */
ShowWindow(hwnd, SW_MAXIMIZE);
/* Set the selected monitor (the monitor in which the window is on) */
for (std::size_t i = 0, max = monitorVec.size(); i != max; ++i)
{
if (MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST) == monitorVec[i])
{
selected_monitor = (100+i);
break;
}
}
/* Run the message loop. It will run until GetMessage() returns 0 */
while (GetMessage(&messages, NULL, 0, 0))
{
/* Translate virtual-key messages into character messages */
TranslateMessage(&messages);
/* Send message to WindowProcedure */
DispatchMessage(&messages);
}
UnregisterClass(szClassName, hThisInstance);
/* The program return-value is 0 - The value that PostQuitMessage() gave */
return messages.wParam;
}
/* This function is called by the Windows function DispatchMessage() */
LRESULT CALLBACK WindowProcedure(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
MONITORINFO mi; mi.cbSize = sizeof ( MONITORINFO );
switch (message)
{ /* handle the messages */
case WM_CREATE:
AddTrayIcon(hwnd, 1, 0);
break;
case WM_QUIT:
case WM_CLOSE:
RemoveTrayIcon(hwnd, 1);
DestroyWindow(hwnd);
break;
case WM_DESTROY:
PostQuitMessage(0); /* send a WM_QUIT to the message queue */
return 0;
case WM_COMMAND:
switch (LOWORD(wParam))
{
case ID_SHOW:
if (is_hidden)
{
ShowWindow(hwnd, SW_SHOW);
is_hidden = false;
}
else
{
ShowWindow(hwnd, SW_HIDE);
is_hidden = true;
}
break;
case ID_OPACITY_50:
SetLayeredWindowAttributes(hwnd, 0, 127, LWA_ALPHA);
checked_item = ID_OPACITY_50;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2003"), _T("./config.ini"));
break;
case ID_OPACITY_60:
SetLayeredWindowAttributes(hwnd, 0, 153, LWA_ALPHA);
checked_item = ID_OPACITY_60;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2004"), _T("./config.ini"));
break;
case ID_OPACITY_70:
SetLayeredWindowAttributes(hwnd, 0, 178, LWA_ALPHA);
checked_item = ID_OPACITY_70;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2005"), _T("./config.ini"));
break;
case ID_OPACITY_80:
SetLayeredWindowAttributes(hwnd, 0, 204, LWA_ALPHA);
checked_item = ID_OPACITY_80;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2006"), _T("./config.ini"));
break;
case ID_OPACITY_90:
SetLayeredWindowAttributes(hwnd, 0, 230, LWA_ALPHA);
checked_item = ID_OPACITY_90;
WritePrivateProfileString(_T("Settings"), _T("checked_item"), _T("2007"), _T("./config.ini"));
break;
case ID_FULL_SCR:
SetWindowPos(hwnd, NULL, GetSystemMetrics(SM_XVIRTUALSCREEN), GetSystemMetrics(SM_YVIRTUALSCREEN), GetSystemMetrics(SM_CXVIRTUALSCREEN), GetSystemMetrics(SM_CYVIRTUALSCREEN), SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
selected_monitor = ID_FULL_SCR;
break;
case ID_EXIT:
PostMessage(hwnd, WM_CLOSE, 0, 0);
break;
default:
for (std::size_t i = 0, max = monitorVec.size(); i != max; ++i)
{
if (LOWORD(wParam) == (100+i))
{
GetMonitorInfo(monitorVec[i], &mi);
int width = std::abs(mi.rcMonitor.right - mi.rcMonitor.left);
int height = std::abs(mi.rcMonitor.bottom - mi.rcMonitor.top);
SetWindowPos(hwnd, NULL, mi.rcMonitor.left, mi.rcMonitor.top, width, height, SWP_NOREDRAW | SWP_NOSENDCHANGING | SWP_NOZORDER | SWP_NOOWNERZORDER | SWP_FRAMECHANGED);
selected_monitor = LOWORD(wParam);
break;
}
}
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
case WM_SHELLICON:
switch (lParam)
{
case WM_RBUTTONUP:
ShowPopupMenu(hwnd, NULL, -1);
PostMessage(hwnd, WM_SHELLICON + 1, 0, 0);
break;
default:
return DefWindowProc(hwnd, message, wParam, lParam);
}
break;
default: /* for messages that we don't deal with */
return DefWindowProc(hwnd, message, wParam, lParam);
}
return 0;
}