-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathClipboardMonitor.cpp
547 lines (465 loc) · 13 KB
/
ClipboardMonitor.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
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
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
#include <sdkddkver.h>
#define WIN32_LEAN_AND_MEAN
#define NOMINMAX
#include <Windows.h>
#include <windowsx.h>
#include <assert.h>
#include <strsafe.h>
#include <stdlib.h>
#include <Uxtheme.h>
#pragma comment(lib, "uxtheme.lib")
#include "Win32Toolbox.h"
#pragma comment(linker,"\"/manifestdependency:type='win32' name='Microsoft.Windows.Common-Controls' version='6.0.0.0' processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
// Forward declarations of functions included in this code module:
static ATOM MyRegisterClass(HINSTANCE hInstance);
static LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
static void UpdateCapturedContent(HWND hWnd);
static HINSTANCE hInst;
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
hInst = hInstance;
// Initialize global strings
ATOM Atom_MainWindow = MyRegisterClass(hInstance);
HWND hWnd = CreateWindowExW(0, MAKEINTATOM(Atom_MainWindow), L"Clipboard Monitor", WS_OVERLAPPEDWINDOW | WS_VSCROLL | WS_HSCROLL,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
assert(hWnd != nullptr);
ShowWindow(hWnd, nCmdShow);
MSG msg;
// Main message loop:
while (GetMessageW(&msg, nullptr, 0, 0))
{
TranslateMessage(&msg);
DispatchMessageW(&msg);
}
return (int)msg.wParam;
}
ATOM MyRegisterClass(HINSTANCE hInstance)
{
WNDCLASSEXW wcex;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.style = CS_HREDRAW | CS_VREDRAW;
wcex.lpfnWndProc = WndProc;
wcex.cbClsExtra = 0;
wcex.cbWndExtra = 0;
wcex.hInstance = hInstance;
wcex.hIcon = nullptr;
wcex.hCursor = LoadCursorW(nullptr, IDC_ARROW);
wcex.hbrBackground = nullptr;
wcex.lpszMenuName = nullptr;
wcex.lpszClassName = L"ClipboardMonitorMainWindow";
wcex.hIconSm = nullptr;
return RegisterClassExW(&wcex);
}
#define IDM_CLEAR_CLIPBOARD 100
#define IDM_REFRESH 101
#define IDM_TOGGLE_AUTO 102
static HBITMAP CurrentImage;
static LONG CurrentImageWidth;
static LONG CurrentImageHeight;
static LPWSTR CurrentText;
static HWND CurrentEditControl;
static HFONT FontMonospace;
static void SetEditControlFont(HWND Edit, HWND Parent)
{
if (FontMonospace == nullptr)
{
HDC hdc = GetDC(Parent);
INT dpi = GetDpi(Parent, hdc);
INT FontSizePx = MulDiv(12 /*12pt*/, dpi, 72);
FONT_DESC font_descs[] =
{
{ L"Consolas", FontSizePx },
{ L"Courier New", FontSizePx }
};
FontMonospace = GetFirstMatchingFont(hdc, font_descs, sizeof(font_descs) / sizeof(font_descs[0]), nullptr);
ReleaseDC(Parent, hdc);
if (FontMonospace == NULL)
{
// Did not find an appropriate font.
FontMonospace = (HFONT)GetStockObject(ANSI_FIXED_FONT);
}
}
SendMessageW(Edit, WM_SETFONT, (WPARAM)(HFONT)FontMonospace, 0);
}
static HWND CreateEditControl(HWND Parent)
{
SIZE ClientSize = GetClientSize(Parent);
HWND Edit = CreateWindowExW(0, WC_EDITW, nullptr, WS_CHILD | WS_VISIBLE | WS_VSCROLL | WS_HSCROLL | ES_MULTILINE | ES_WANTRETURN, 0, 0, ClientSize.cx, ClientSize.cy, Parent, nullptr, hInst, nullptr);
assert(Edit != nullptr);
SetEditControlFont(Edit, Parent);
return Edit;
}
static BOOL OpenClipboard_ButTryABitHarder(HWND hWnd)
{
for (int i = 0; i < 20; ++i)
{
// This can fail if the clipboard is currently being accessed by another application.
if (OpenClipboard(hWnd)) return true;
Sleep(10);
}
return false;
}
static void UpdateClipboard(HWND hWnd)
{
DeleteObject(CurrentImage);
CurrentImage = nullptr;
free(CurrentText);
CurrentText = nullptr;
if (OpenClipboard_ButTryABitHarder(hWnd))
{
UINT KnownFormats[] =
{
CF_DIB,
CF_UNICODETEXT
};
int ClipboardFormat = GetPriorityClipboardFormat(KnownFormats, _countof(KnownFormats));
switch (ClipboardFormat)
{
case CF_DIB:
{
HGLOBAL ClipboardDataHandle = GetClipboardData(CF_DIB);
if (ClipboardDataHandle)
{
BITMAPINFOHEADER *BitmapInfoHeader = (BITMAPINFOHEADER *)GlobalLock(ClipboardDataHandle);
SIZE_T ClipboardDataSize = GlobalSize(ClipboardDataHandle);
if (BitmapInfoHeader)
{
BITMAP BitmapDesc = {};
HBITMAP hBitmap = CreateDIBFromPackedDIB(BitmapInfoHeader, ClipboardDataSize, &BitmapDesc);
if (hBitmap)
{
CurrentImage = hBitmap;
CurrentImageWidth = BitmapDesc.bmWidth;
CurrentImageHeight = BitmapDesc.bmHeight;
}
}
}
break;
}
case CF_UNICODETEXT:
{
HANDLE ClipboardTextHandle = GetClipboardData(CF_UNICODETEXT);
if (ClipboardTextHandle != nullptr)
{
LPCWSTR ClipboardText = (LPCWSTR)GlobalLock(ClipboardTextHandle);
SIZE_T ClipboardTextLength = GlobalSize(ClipboardTextHandle);
if (ClipboardText != nullptr)
{
// Add a terminating 0 just in case the original data doesn't have one.
CurrentText = (LPWSTR)malloc(sizeof(WCHAR) * (ClipboardTextLength + 1));
if (CurrentText != nullptr)
{
CurrentText[ClipboardTextLength] = 0;
memcpy(CurrentText, ClipboardText, sizeof(WCHAR) * ClipboardTextLength);
}
}
GlobalUnlock(ClipboardTextHandle);
}
break;
}
}
CloseClipboard();
}
UpdateCapturedContent(hWnd);
}
// Called initially, and inside UpdateClipboard after the clipboard contents have been captured.
static void UpdateCapturedContent(HWND hWnd)
{
// Update scroll bars
SCROLLINFO ScrollInfo = {};
ScrollInfo.cbSize = sizeof(ScrollInfo);
if (CurrentImage != nullptr)
{
// This is bugged.
// Even if you toggle scroll bar visibility by turning the window style on and off it doesn't fix it.
// Forcing the scroll bars to be visible *before* changing the scroll bar parameters is required to fix this.
// If the scroll bar is not already visible at the time a SIF_DISABLENOSCROLL thing is sent, it won't show.
ShowScrollBar(hWnd, SB_VERT, true);
ShowScrollBar(hWnd, SB_HORZ, true);
SIZE ClientSize = GetClientSize(hWnd);
ScrollInfo.fMask = SIF_DISABLENOSCROLL | SIF_PAGE | SIF_RANGE;
ScrollInfo.nPage = ClientSize.cy;
ScrollInfo.nMax = CurrentImageHeight - 1;
SetScrollInfo(hWnd, SB_VERT, &ScrollInfo, true);
ScrollInfo.nPage = ClientSize.cx;
ScrollInfo.nMax = CurrentImageWidth - 1;
SetScrollInfo(hWnd, SB_HORZ, &ScrollInfo, true);
}
else
{
ScrollInfo.fMask = SIF_PAGE | SIF_RANGE;
// This makes the scroll bars effectively invisible.
SetScrollInfo(hWnd, SB_VERT, &ScrollInfo, true);
SetScrollInfo(hWnd, SB_HORZ, &ScrollInfo, true);
}
// Update edit control
if (CurrentText != nullptr)
{
if (CurrentEditControl == nullptr)
{
CurrentEditControl = CreateEditControl(hWnd);
}
SetWindowTextW(CurrentEditControl, CurrentText);
}
else
{
// Text is null; remove EDIT control
if (CurrentEditControl != nullptr)
{
DestroyWindow(CurrentEditControl);
CurrentEditControl = nullptr;
}
}
InvalidateRect(hWnd, nullptr, true);
}
static int ScrollAmountPerLine = 10;
static BOOL Panning;
static int PanningX;
static int PanningY;
enum MONITORING_MODE
{
MONITORING_AUTO,
MONITORING_OFF,
MONITORING_ONESHOT,
MONITORING_MODE_COUNT
};
static MONITORING_MODE MonitoringMode = MONITORING_AUTO;
// Updates the menu according to TurnedOn.
static void UpdateMenuState(HWND hWnd, HMENU hMenu)
{
if (hMenu == nullptr)
{
hMenu = GetMenu(hWnd);
}
MENUITEMINFOW MenuItemInfo = {};
MenuItemInfo.cbSize = sizeof(MenuItemInfo);
MenuItemInfo.fMask = MIIM_STRING;
LPCWSTR Text;
switch (MonitoringMode)
{
case MONITORING_AUTO:
Text = L"Toggle Mode (currently AUTO)";
break;
case MONITORING_OFF:
default:
Text = L"Toggle Mode (currently OFF)";
break;
case MONITORING_ONESHOT:
Text = L"Toggle Mode (currently ONE SHOT)";
break;
}
MenuItemInfo.dwTypeData = (LPWSTR)Text;
BOOL b = SetMenuItemInfoW(hMenu, IDM_TOGGLE_AUTO, false, &MenuItemInfo); assert(b);
b = DrawMenuBar(hWnd); assert(b);
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
HRESULT hr = BufferedPaintInit(); assert(SUCCEEDED(hr));
BOOL b = AddClipboardFormatListener(hWnd); assert(b);
HMENU Menu = CreateMenu();
assert(Menu != nullptr);
MENUITEMINFOW MenuItemInfo = {};
MenuItemInfo.cbSize = sizeof(MenuItemInfo);
MenuItemInfo.fMask = MIIM_FTYPE | MIIM_ID | MIIM_STRING;
MenuItemInfo.fType = MFT_STRING;
MenuItemInfo.wID = IDM_CLEAR_CLIPBOARD;
MenuItemInfo.dwTypeData = (LPWSTR)L"Clear Clipboard Data";
b = InsertMenuItemW(Menu, 0, false, &MenuItemInfo); assert(b);
MenuItemInfo.wID = IDM_REFRESH;
MenuItemInfo.dwTypeData = (LPWSTR)L"Refresh (F5)";
b = InsertMenuItemW(Menu, 0, false, &MenuItemInfo); assert(b);
MenuItemInfo.wID = IDM_TOGGLE_AUTO;
MenuItemInfo.dwTypeData = (LPWSTR)L"";
b = InsertMenuItemW(Menu, 0, false, &MenuItemInfo); assert(b);
b = SetMenu(hWnd, Menu); assert(b);
UpdateMenuState(hWnd, Menu);
UpdateCapturedContent(hWnd);
return 0;
}
case WM_DPICHANGED:
{
if (CurrentEditControl != nullptr)
{
HFONT OldFont = FontMonospace;
FontMonospace = nullptr;
SetEditControlFont(CurrentEditControl, hWnd);
if (OldFont)
{
DeleteObject(OldFont);
}
}
break;
}
case WM_KEYDOWN:
{
switch (wParam)
{
case VK_F5:
{
UpdateClipboard(hWnd);
break;
}
}
return 0;
}
case WM_COMMAND:
{
USHORT CommandID = LOWORD(wParam);
switch (CommandID)
{
case IDM_CLEAR_CLIPBOARD:
{
if (OpenClipboard_ButTryABitHarder(hWnd))
{
EmptyClipboard();
CloseClipboard();
InvalidateRect(hWnd, nullptr, false);
}
break;
}
case IDM_REFRESH:
{
UpdateClipboard(hWnd);
break;
}
case IDM_TOGGLE_AUTO:
{
MonitoringMode = (MONITORING_MODE)((MonitoringMode + 1) % MONITORING_MODE_COUNT);
UpdateMenuState(hWnd, nullptr);
break;
}
}
return 0;
}
case WM_CLIPBOARDUPDATE:
{
switch (MonitoringMode)
{
case MONITORING_AUTO:
case MONITORING_ONESHOT:
{
UpdateClipboard(hWnd);
if (MonitoringMode == MONITORING_ONESHOT)
{
MonitoringMode = MONITORING_OFF;
UpdateMenuState(hWnd, nullptr);
}
break;
}
}
return 0;
}
case WM_VSCROLL:
{
HandleWindowMessage_Scroll(hWnd, wParam, SB_VERT, ScrollAmountPerLine, nullptr);
return 0;
}
case WM_HSCROLL:
{
HandleWindowMessage_Scroll(hWnd, wParam, SB_HORZ, ScrollAmountPerLine, nullptr);
return 0;
}
case WM_MOUSEWHEEL:
{
int nBar = GetKeyState(VK_SHIFT) < 0 ? SB_HORZ : SB_VERT;
HandleWindowMessage_MouseWheel(hWnd, wParam, nBar, ScrollAmountPerLine, nullptr);
return 0;
}
case WM_LBUTTONDOWN:
{
PanningX = GET_X_LPARAM(lParam);
PanningY = GET_Y_LPARAM(lParam);
Panning = true;
return 0;
}
case WM_MOUSEMOVE:
{
if (Panning && (wParam & MK_LBUTTON))
{
int x = GET_X_LPARAM(lParam);
int y = GET_Y_LPARAM(lParam);
int dx = x - PanningX;
int dy = y - PanningY;
PanningX = x;
PanningY = y;
ScrollTo(hWnd, SB_VERT, SCROLLTO_RELATIVE, -dy, nullptr);
ScrollTo(hWnd, SB_HORZ, SCROLLTO_RELATIVE, -dx, nullptr);
}
else
{
Panning = false;
}
return 0;
}
case WM_SIZE:
{
SIZE ClientSize = GetClientSize(hWnd);
if (CurrentEditControl != nullptr)
{
SetWindowPos(CurrentEditControl, nullptr, 0, 0, ClientSize.cx, ClientSize.cy, 0);
}
if (CurrentImage != nullptr)
{
SCROLLINFO ScrollInfo = {};
ScrollInfo.cbSize = sizeof(ScrollInfo);
ScrollInfo.fMask = SIF_DISABLENOSCROLL | SIF_PAGE;
ScrollInfo.nPage = ClientSize.cy;
SetScrollInfo(hWnd, SB_VERT, &ScrollInfo, true);
ScrollInfo.nPage = ClientSize.cx;
SetScrollInfo(hWnd, SB_HORZ, &ScrollInfo, true);
}
return 0;
}
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc0 = BeginPaint(hWnd, &ps);
if (CurrentEditControl == nullptr)
{
HDC hdc = nullptr;
HPAINTBUFFER PaintBuffer = BeginBufferedPaint(hdc0, &ps.rcPaint, BPBF_DIB, nullptr, &hdc);
assert(PaintBuffer != nullptr);
FillRect(hdc, &ps.rcPaint, (HBRUSH)GetStockObject(BLACK_BRUSH));
if (CurrentImage != nullptr)
{
// Adjust for scrolling
SCROLLINFO ScrollInfo = {};
ScrollInfo.cbSize = sizeof(ScrollInfo);
ScrollInfo.fMask = SIF_POS;
GetScrollInfo(hWnd, SB_VERT, &ScrollInfo);
int ScrollV = ScrollInfo.nPos;
GetScrollInfo(hWnd, SB_HORZ, &ScrollInfo);
int ScrollH = ScrollInfo.nPos;
SetViewportOrgEx(hdc, -ScrollH, -ScrollV, nullptr);
ps.rcPaint.top += ScrollV;
ps.rcPaint.bottom += ScrollV;
ps.rcPaint.left += ScrollH;
ps.rcPaint.right += ScrollH;
HDC src = CreateCompatibleDC(hdc);
SelectObject(src, CurrentImage);
BitBlt(hdc, 0, 0, CurrentImageWidth, CurrentImageHeight, src, 0, 0, SRCCOPY);
DeleteDC(src);
}
SetViewportOrgEx(hdc, 0, 0, nullptr);
EndBufferedPaint(PaintBuffer, true);
}
EndPaint(hWnd, &ps);
return 0;
}
case WM_DESTROY:
{
BufferedPaintUnInit();
RemoveClipboardFormatListener(hWnd);
PostQuitMessage(0);
return 0;
}
}
return DefWindowProcW(hWnd, message, wParam, lParam);
}