-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
198 lines (163 loc) · 6.58 KB
/
Copy pathmain.cpp
File metadata and controls
198 lines (163 loc) · 6.58 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
#include "LensIt.h"
#include "WinHandles.h"
AppConfig g_config;
AppState g_app;
HWND g_hwndOverlay = NULL;
HWND g_hwndSettings = NULL;
HWND g_hwndToast = NULL;
BindingMode g_bindingMode = BindingMode::None;
float g_currentZoom = 1.0f;
float g_targetZoom = 1.0f;
float g_camX = 0.0f;
float g_camY = 0.0f;
HICON g_appIcon = NULL;
NOTIFYICONDATA g_nid = { sizeof(NOTIFYICONDATA) };
typedef BOOL(WINAPI* pfnMagSetFullscreenUseBitmapSmoothing)(BOOL);
typedef BOOL(WINAPI* pfnMagSetFullscreenWindowFilterList)(DWORD, int, HWND*);
void LoadAppIcon(HINSTANCE hInstance) {
int cxSmall = GetSystemMetrics(SM_CXSMICON);
int cySmall = GetSystemMetrics(SM_CYSMICON);
g_appIcon = reinterpret_cast<HICON>(LoadImageW(
hInstance,
MAKEINTRESOURCE(IDI_APP_ICON),
IMAGE_ICON,
cxSmall,
cySmall,
LR_DEFAULTCOLOR
));
if (!g_appIcon) {
g_appIcon = LoadIconW(hInstance, MAKEINTRESOURCE(IDI_APP_ICON));
}
}
void InitTray(HWND hwnd, HINSTANCE hInstance) {
(void)hInstance;
g_nid.hWnd = hwnd;
g_nid.uID = 1;
g_nid.uFlags = NIF_ICON | NIF_MESSAGE | NIF_TIP;
g_nid.uCallbackMessage = WM_APP_TRAYMSG;
g_nid.hIcon = g_appIcon;
wcscpy_s(g_nid.szTip, L"LensIt");
Shell_NotifyIcon(NIM_ADD, &g_nid);
}
static bool s_zoomTimerArmed = false;
void StartZoomTimer() {
if (!g_hwndOverlay || s_zoomTimerArmed) return;
s_zoomTimerArmed = SetTimer(g_hwndOverlay, 1, 14, NULL) != 0;
}
void StopZoomTimer() {
s_zoomTimerArmed = false;
ProcessOverlayFrame();
if (g_hwndOverlay) KillTimer(g_hwndOverlay, 1);
}
bool RequiresZoomTimer() {
if (g_app.isDrawingLine || g_app.isDrawingArrow || g_app.isDrawRectangle || g_app.isDrawingHighlight || g_app.isDrawingBlur) return true;
if (fabsf(g_currentZoom - g_targetZoom) > 0.001f) return true;
return false;
}
static LONG WINAPI UnhandledExceptionCleanup(EXCEPTION_POINTERS* pExceptionInfo) {
(void)pExceptionInfo;
MagSetFullscreenTransform(1.0f, 0, 0);
return EXCEPTION_CONTINUE_SEARCH;
}
int APIENTRY WinMain(
_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPSTR lpCmdLine,
_In_ int nCmdShow
) {
(void)hPrevInstance;
(void)lpCmdLine;
(void)nCmdShow;
HANDLE hSingleInstanceMutex = CreateMutexW(NULL, TRUE, L"LensIt_SingleInstance_Mutex");
if (hSingleInstanceMutex && GetLastError() == ERROR_ALREADY_EXISTS) {
HWND hExisting = FindWindowW(L"LensItOverlay", NULL);
if (hExisting) {
PostMessageW(hExisting, WM_APP_SHOWSETTINGS, 0, 0);
}
if (hSingleInstanceMutex) {
CloseHandle(hSingleInstanceMutex);
}
return 0;
}
SetProcessDpiAwarenessContext(DPI_AWARENESS_CONTEXT_PER_MONITOR_AWARE_V2);
HRESULT hrCo = CoInitializeEx(NULL, COINIT_APARTMENTTHREADED);
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
LoadAppIcon(hInstance);
LoadConfig();
if (!MagInitialize()) {
GdiplusShutdown(gdiplusToken);
if (SUCCEEDED(hrCo)) CoUninitialize();
return 1;
}
MagShowSystemCursor(TRUE);
SetUnhandledExceptionFilter(UnhandledExceptionCleanup);
HMODULE hMag = GetModuleHandle(L"magnification.dll");
if (hMag) {
auto pSetSmoothing = (pfnMagSetFullscreenUseBitmapSmoothing)GetProcAddress(hMag, "MagSetFullscreenUseBitmapSmoothing");
if (pSetSmoothing) pSetSmoothing(TRUE);
}
WNDCLASSEX wcOverlay = { sizeof(WNDCLASSEX), 0, OverlayWndProc, 0, 0, hInstance, g_appIcon, NULL, static_cast<HBRUSH>(GetStockObject(BLACK_BRUSH)), NULL, L"LensItOverlay", g_appIcon };
RegisterClassEx(&wcOverlay);
WNDCLASSEX wcSettings = { sizeof(WNDCLASSEX), 0, SettingsWndProc, 0, 0, hInstance, g_appIcon, LoadCursor(NULL, IDC_ARROW), NULL, NULL, L"LensItSettings", g_appIcon };
RegisterClassEx(&wcSettings);
WNDCLASSEX wcWelcome = { sizeof(WNDCLASSEX), 0, WelcomeWndProc, 0, 0, hInstance, g_appIcon, LoadCursor(NULL, IDC_ARROW), NULL, NULL, L"LensItWelcome", g_appIcon };
RegisterClassEx(&wcWelcome);
WNDCLASSEX wcToast = { sizeof(WNDCLASSEX), 0, ToastWndProc, 0, 0, hInstance, NULL, LoadCursor(NULL, IDC_ARROW), NULL, NULL, L"LensItToast", NULL };
RegisterClassEx(&wcToast);
InitToastWindow(hInstance);
int vScreenX = GetSystemMetrics(SM_XVIRTUALSCREEN);
int vScreenY = GetSystemMetrics(SM_YVIRTUALSCREEN);
int screenW = GetSystemMetrics(SM_CXVIRTUALSCREEN);
int screenH = GetSystemMetrics(SM_CYVIRTUALSCREEN);
g_hwndOverlay = CreateWindowEx(
WS_EX_TOPMOST | WS_EX_LAYERED | WS_EX_TRANSPARENT | WS_EX_TOOLWINDOW | WS_EX_NOACTIVATE,
L"LensItOverlay", L"Overlay", WS_POPUP, vScreenX, vScreenY, screenW, screenH, NULL, NULL, hInstance, NULL
);
SetWindowDisplayAffinity(g_hwndOverlay, WDA_EXCLUDEFROMCAPTURE);
if (hMag) {
auto pSetFilter = (pfnMagSetFullscreenWindowFilterList)GetProcAddress(hMag, "MagSetFullscreenWindowFilterList");
if (pSetFilter) {
HWND hExclude[1] = { g_hwndOverlay };
pSetFilter(MW_FILTERMODE_EXCLUDE, 1, hExclude);
}
}
ShowWindow(g_hwndOverlay, SW_HIDE);
RepositionOverlay();
WM_TASKBARCREATED = RegisterWindowMessageW(L"TaskbarCreated");
InitTray(g_hwndOverlay, hInstance);
if (g_config.isFirstRun) {
ShowWelcomeWindow(hInstance);
}
{
UniqueHook kbdHook(SetWindowsHookEx(WH_KEYBOARD_LL, LowLevelKeyboardProc, hInstance, 0));
UniqueHook mouseHook(SetWindowsHookEx(WH_MOUSE_LL, LowLevelMouseProc, hInstance, 0));
MSG msg;
while (GetMessage(&msg, NULL, 0, 0)) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
Shell_NotifyIcon(NIM_DELETE, &g_nid);
if (g_hwndToast) DestroyWindow(g_hwndToast);
if (g_hwndSettings) DestroyWindow(g_hwndSettings);
if (g_hwndOverlay) DestroyWindow(g_hwndOverlay);
g_hwndSettings = NULL;
g_hwndOverlay = NULL;
DestroyOverlayBackbuffer();
MagSetFullscreenTransform(1.0f, 0, 0);
MagUninitialize();
g_app.strokes.clear();
g_app.currentStroke = Stroke();
g_app.textDraft = Stroke();
GdiplusShutdown(gdiplusToken);
if (SUCCEEDED(hrCo)) CoUninitialize();
if (g_appIcon) DestroyIcon(g_appIcon);
SaveConfig();
if (hSingleInstanceMutex) {
ReleaseMutex(hSingleInstanceMutex);
CloseHandle(hSingleInstanceMutex);
}
return 0;
}