-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathSnipEx.c
More file actions
3963 lines (2805 loc) · 106 KB
/
SnipEx.c
File metadata and controls
3963 lines (2805 loc) · 106 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
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
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// SnipEx.c
// Author: Joseph Ryan Ries, 2017-2020
// The snip.exe that comes bundled with Microsoft Windows is *almost* good enough.
// So I made one just a little better.
#ifndef UNICODE
#define UNICODE // 100% Unicode.
#endif
#ifndef _UNICODE
#define _UNICODE
#endif
#pragma warning(push, 0) // Temporarily disable warnings in header files over which I have no control.
#include <windowsx.h> // Windows API
#include <ShObjIdl.h> // For save dialog
#include <ShellScalingApi.h> // For detecting monitor DPI (Win 8.1 or above)
#include <initguid.h> // For doing stuff with GUIDs (Needed for COM interop)
#pragma warning(pop) // Restore warnings.
#pragma comment(lib, "Msimg32.lib") // For TransparentBlt
#pragma comment(lib, "Shcore.lib") // For detecting monitor DPI (Win 8.1 or above)
#pragma warning(disable: 4820) // Disable compiler warning about padding bytes being added to structs
#pragma warning(disable: 4710) // Disable compiler warning about functions not being inlined
#pragma warning(disable: 5045) // Disable Spectre mitigation informational warning
#include <stdio.h> // For doing stuff with strings
#include <math.h> // Needed to introduce some math to draw the arrow head for the arrow tool
#include "resource.h" // Images, cursors, etc.
#include "SnipEx.h" // My custom definitions
#include "ButtonDefs.h" // Buttons!
#include "GdiPlusInterop.h" // Stuff to make GDI+ work in pure C
APPSTATE gAppState = APPSTATE_BEFORECAPTURE; // To track the overall state of the application
BOOL gMainWindowIsRunning; // Set this to FALSE to exit the app immediately.
HWND gMainWindowHandle; // Handle to the main window, i.e. the window with all the buttons on it.
HWND gCaptureWindowHandle; // Handle to the capture window, i.e. the grey selection window that overlays the entire desktop.
const INT8 gStartingDelayCountdown = 6; // Default seconds to wait after clicking the 'Delay' button.
INT8 gCurrentDelayCountdown = 6; // The value of the countdown timer at this moment.
UINT16 gDisplayWidth; // Display width, accounting for multiple monitors.
UINT16 gDisplayHeight; // Display height, accounting for multiple monitors.
INT16 gDisplayLeft; // Depending on how the monitors are arranged, the left-most coordinate might not be zero.
INT16 gDisplayTop; // Depending on how the monitors are arranged, the top-most coordinate might not be zero.
UINT16 gStartingMainWindowWidth = 668; // The beginning width of the tool window - just enough to fit all the buttons.
UINT16 gStartingMainWindowHeight = 92; // The beginning height of the tool window - just enough to fit the buttons.
HBITMAP gCleanScreenShot; // A clean copy of the screenshot from before we started drawing on it.
HBITMAP gScratchBitmap; // For use during drawing.
RECT gCaptureSelectionRectangle; // The rectangle the user draws with the mouse to select a subsection of the screen.
int gCaptureWidth; // Width in pixels of the user's captured snip.
int gCaptureHeight; // Height in pixels of the user's captured snip.
BOOL gLeftMouseButtonIsDown; // When the user is drawing with the mouse, the left mouse button is down.
UINT8 gCurrentSnipState; // An array of bitmap states that we can revert back to, so we can undo changes. ctrl-z.
HBITMAP gSnipStates[32]; // Snip state 0 will always be the unaltered clip right as the user first took it.
HBITMAP gUACIcon; // The UAC icon that sits next to the "Replace Windows Snipping Tool with SnipEx" menu item.
DWORD gShouldAddDropShadow; // Does the user want to add a drop-shadow effect to the snip?
DWORD gRememberLastTool; // Does the user want to remember the previously-used tool?
DWORD gAutoCopy; // Does the user want to automatically copy each snip to the clipboard?
DWORD gLastTool; // What was the last tool that the user used? (NOT save, copy, new, or delay.)
HFONT gFont; // The font the user selects for the Text tool.
COLORREF gFontColor; // The color of the font that the user selects for the Text tool.
POINT gTextBoxLocation; // The coordinates where we will spawn text
wchar_t gTextBuffer[1024]; // Buffer for Text tool.
// Application entry-point.
// MSDN says that if WinMain fails before reaching the message loop, we should return zero.
int CALLBACK WinMain(_In_ HINSTANCE Instance, _In_opt_ HINSTANCE PreviousInstance, _In_ LPSTR CommandLine, _In_ int WindowShowCode)
{
MyOutputDebugStringW(L"[%s] Line %d: Entered.\n", __FUNCTIONW__, __LINE__);
UNREFERENCED_PARAMETER(PreviousInstance);
UNREFERENCED_PARAMETER(CommandLine);
UNREFERENCED_PARAMETER(WindowShowCode);
if ((gFont = GetStockObject(DEFAULT_GUI_FONT)) == NULL)
{
MessageBoxW(NULL, L"Failed to retrieve default GUI font!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
gFontColor = RGB(255, 0, 255);
// NOTE: This width and height are the bounding box that includes all of the user's monitors.
// I have only tested with two monitors of equal dimensions, and with a primary monitor that is larger
// than the secondary monitor. No other configurations were tested, so this may not work correctly
// if the user has several irregularly-arranged monitors. Also, if the monitors are arranged
// in reverse order, the left-most X coordinate may be e.g. negative 1920! In other words, 0,0 may not
// necessarily be the top-left corner of the user's viewing area.
gDisplayWidth = (UINT16)GetSystemMetrics(SM_CXVIRTUALSCREEN);
gDisplayHeight = (UINT16)GetSystemMetrics(SM_CYVIRTUALSCREEN);
gDisplayLeft = (INT16)GetSystemMetrics(SM_XVIRTUALSCREEN);
gDisplayTop = (INT16)GetSystemMetrics(SM_YVIRTUALSCREEN);
if (gDisplayWidth == 0 || gDisplayHeight == 0)
{
MessageBoxW(NULL, L"Failed to retrieve display area via GetSystemMetrics!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
MyOutputDebugStringW(L"[%s] Line %d: Detected a screen area of %dx%d.\n", __FUNCTIONW__, __LINE__, gDisplayWidth, gDisplayHeight);
// If the user had a custom scaling/DPI level set, this app was originally not high-DPI aware, and so what would happen
// is that the buttons would get cropped as the non-client area of the window got bigger, and the screen would "zoom in"
// whenever the user clicked "New"!
//if (AdjustForCustomScaling() == FALSE)
//{
// return(0);
//}
WNDCLASSEXW CaptureWindowClass = { sizeof(WNDCLASSEXW) };
CaptureWindowClass.style = CS_HREDRAW | CS_VREDRAW;
CaptureWindowClass.hInstance = Instance;
CaptureWindowClass.lpszClassName = L"SnipExCaptureWindowClass";
CaptureWindowClass.hCursor = LoadCursorW(NULL, IDC_CROSS);
CaptureWindowClass.hbrBackground = GetSysColorBrush(COLOR_DESKTOP);
CaptureWindowClass.lpfnWndProc = CaptureWindowCallback;
if (RegisterClassEx(&CaptureWindowClass) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: RegisterClassEx failed with 0x%lx!\n", __FUNCTIONW__, __LINE__, GetLastError());
MessageBoxW(NULL, L"Failed to register CaptureWindowClass with error 0x%lx!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
// This window will capture the entire display surface, including multiple monitors. It will then display an exact
// screenshot of the desktop over the real desktop. Then the user will be able to select a subsection of that using
// the mouse. This could also be used to play a prank on somebody and make them think their desktop was hung.
gCaptureWindowHandle = CreateWindowExW(
WS_EX_TOPMOST | WS_EX_TOOLWINDOW, // No taskbar icon
CaptureWindowClass.lpszClassName,
L"", // No title
0, // Not visible
CW_USEDEFAULT,
CW_USEDEFAULT,
0, // Will size it later
0,
NULL,
NULL,
Instance,
NULL);
if (gCaptureWindowHandle == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: CreateWindowEx (capture window) failed with 0x%lx!\n", __FUNCTIONW__, __LINE__, GetLastError());
MessageBoxW(NULL, L"Failed to create Capture Window!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
// Remove all window style, including title bar. We're trying to make the "capture window" indistinguishable from the real desktop that lay underneath it.
if (SetWindowLongPtrW(gCaptureWindowHandle, GWL_STYLE, 0) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: SetWindowLongPtwW failed with 0x%lx!\n", __FUNCTIONW__, __LINE__, GetLastError());
MessageBoxW(NULL, L"SetWindowLongPtrW failed!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
// The main window class that appears when the application is first launched.
WNDCLASSEXW MainWindowClass = { sizeof(WNDCLASSEXW) };
MainWindowClass.style = CS_HREDRAW | CS_VREDRAW;
MainWindowClass.hInstance = Instance;
MainWindowClass.lpszClassName = L"SnipExWindowClass";
MainWindowClass.hCursor = LoadCursorW(NULL, IDC_ARROW);
MainWindowClass.hbrBackground = GetSysColorBrush(COLOR_WINDOW);
MainWindowClass.lpfnWndProc = MainWindowCallback;
MainWindowClass.hIcon = LoadIconW(Instance, MAKEINTRESOURCEW(IDI_MAINAPPICON));
MainWindowClass.hIconSm = LoadIconW(Instance, MAKEINTRESOURCEW(IDI_MAINAPPICON));
if (RegisterClassExW(&MainWindowClass) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: RegisterClassExW failed with 0x%lx!\n", __FUNCTIONW__, __LINE__, GetLastError());
MessageBoxW(NULL, L"RegisterClassExW failed!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
gMainWindowHandle = CreateWindowExW(
0,
MainWindowClass.lpszClassName,
L"SnipEx",
WS_VISIBLE | (WS_OVERLAPPEDWINDOW ^ (WS_MAXIMIZEBOX | WS_THICKFRAME)),
CW_USEDEFAULT,
CW_USEDEFAULT,
gStartingMainWindowWidth,
gStartingMainWindowHeight,
NULL,
NULL,
Instance,
NULL);
if (gMainWindowHandle == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: CreateWindowEx (main window) failed with 0x%lx!\n", __FUNCTIONW__, __LINE__, GetLastError());
MessageBoxW(NULL, L"CreateWindowEx (main window) failed!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
#if _DEBUG
wchar_t TitleBarBuffer[64] = { 0 };
GetWindowTextW(gMainWindowHandle, TitleBarBuffer, _countof(TitleBarBuffer));
wcscat_s(TitleBarBuffer, _countof(TitleBarBuffer), L" - *DEBUG BUILD*");
SetWindowTextW(gMainWindowHandle, TitleBarBuffer);
MyOutputDebugStringW(L"[%s] Line %d: Setting window text for *DEBUG BUILD*.\n", __FUNCTIONW__, __LINE__);
#endif
// Create all the buttons.
for (UINT8 Counter = 0; Counter < _countof(gButtons); Counter++)
{
HWND ButtonHandle = CreateWindowExW(
0,
L"BUTTON",
gButtons[Counter]->Caption,
BS_OWNERDRAW | WS_VISIBLE | WS_CHILD,
gButtons[Counter]->Rectangle.left,
gButtons[Counter]->Rectangle.top,
gButtons[Counter]->Rectangle.right - gButtons[Counter]->Rectangle.left,
gButtons[Counter]->Rectangle.bottom,
gMainWindowHandle,
(HMENU)gButtons[Counter]->Id,
(HINSTANCE)GetWindowLongPtr(gMainWindowHandle, GWLP_HINSTANCE),
NULL);
if (ButtonHandle == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: Failed to create %s button with error 0x%lx\n", __FUNCTIONW__, __LINE__, gButtons[Counter]->Caption, GetLastError());
MessageBoxW(NULL, L"Failed to create button!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
gButtons[Counter]->Handle = ButtonHandle;
if (gButtons[Counter]->EnabledIconId > 0)
{
gButtons[Counter]->EnabledIcon = (HBITMAP)LoadImageW(Instance, MAKEINTRESOURCEW(gButtons[Counter]->EnabledIconId), IMAGE_BITMAP, 0, 0, 0);
if (gButtons[Counter]->EnabledIcon == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: Loading resource %d failed! Error: 0x%lx\n", __FUNCTIONW__, __LINE__, gButtons[Counter]->EnabledIconId, GetLastError());
MessageBoxW(NULL, L"Failed to create button!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
}
if (gButtons[Counter]->DisabledIconId > 0)
{
gButtons[Counter]->DisabledIcon = (HBITMAP)LoadImageW(Instance, MAKEINTRESOURCEW(gButtons[Counter]->DisabledIconId), IMAGE_BITMAP, 0, 0, 0);
if (gButtons[Counter]->DisabledIcon == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: Loading resource %d failed! Error: 0x%lx\n", __FUNCTIONW__, __LINE__, gButtons[Counter]->DisabledIconId, GetLastError());
MessageBoxW(NULL, L"Failed to create button!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
}
if (gButtons[Counter]->CursorId > 0)
{
gButtons[Counter]->Cursor = LoadCursor(Instance, MAKEINTRESOURCE(gButtons[Counter]->CursorId));
if (gButtons[Counter]->Cursor == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: Loading resource %d failed! Error: 0x%lx\n", __FUNCTIONW__, __LINE__, gButtons[Counter]->CursorId, GetLastError());
MessageBoxW(NULL, L"Failed to create button!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
}
MyOutputDebugStringW(L"[%s] Line %d: %s button created.\n", __FUNCTIONW__, __LINE__, gButtons[Counter]->Caption);
}
// Add custom menu items to the form's system menu in the top left
if ((AddAllMenuItems(Instance)) != S_OK)
{
MyOutputDebugStringW(L"[%s] Line %d: ERROR: Adding drop-down menu items failed!\n", __FUNCTIONW__, __LINE__);
MessageBoxW(NULL, L"Failed to create drop-down menu items!", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
return(0);
}
AdjustWindowSizeForThickTitleBars();
gMainWindowIsRunning = TRUE;
MSG MainWindowMessage = { 0 };
MSG CaptureWindowMessage = { 0 };
MyOutputDebugStringW(L"[%s] Line %d: Setup is finished. Entering message loop.\n", __FUNCTIONW__, __LINE__);
///////////////////////
// //
// MAIN MESSAGE LOOP //
// //
///////////////////////
while (gMainWindowIsRunning)
{
// Drain message queue for main window.
while (PeekMessageW(&MainWindowMessage, gMainWindowHandle, 0, 0, PM_REMOVE))
{
DispatchMessageW(&MainWindowMessage);
}
// Drain message queue for capture window.
while (PeekMessageW(&CaptureWindowMessage, gCaptureWindowHandle, 0, 0, PM_REMOVE))
{
DispatchMessageW(&CaptureWindowMessage);
}
if ((gAppState == APPSTATE_AFTERCAPTURE) && gLeftMouseButtonIsDown)
{
if (GetAsyncKeyState(VK_LBUTTON) == 0)
{
gLeftMouseButtonIsDown = FALSE;
SendMessageW(gMainWindowHandle, WM_LBUTTONUP, 0, 0);
}
}
Sleep(1); // Could be anywhere from 0.5ms to 15.6ms
}
return(0);
}
// Handles window messages for the main window.
LRESULT CALLBACK MainWindowCallback(_In_ HWND Window, _In_ UINT Message, _In_ WPARAM WParam, _In_ LPARAM LParam)
{
LRESULT Result = 0;
static BOOL CurrentlyDrawing;
static POINT MousePosWhenDrawingStarted;
static POINT PreviousMousePos;
static SMALLPOINT HilighterPixelsAlreadyDrawn[32768] = { 0 }; // 128k of memory
static UINT16 HilighterPixelsAlreadyDrawnCounter = 0;
switch (Message)
{
case WM_CLOSE:
{
PostQuitMessage(0);
gMainWindowIsRunning = FALSE;
break;
}
case WM_KEYDOWN:
{
// Ctrl+Z, Undo
if ((WParam == 0x5A) && GetKeyState(VK_CONTROL) && (gAppState == APPSTATE_AFTERCAPTURE) && !CurrentlyDrawing)
{
if (gCurrentSnipState > 0)
{
gCurrentSnipState--;
MyOutputDebugStringW(L"[%s] Line %d: Deleting gSnipStates[%d]\n", __FUNCTIONW__, __LINE__, gCurrentSnipState + 1);
memset(HilighterPixelsAlreadyDrawn, 0, sizeof(HilighterPixelsAlreadyDrawn));
HilighterPixelsAlreadyDrawnCounter = 0;
if (DeleteObject(gSnipStates[gCurrentSnipState + 1]) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteObject failed!\n", __FUNCTIONW__, __LINE__);
CRASH(0);
}
gSnipStates[gCurrentSnipState + 1] = NULL;
if (gAutoCopy)
{
MyOutputDebugStringW(L"[%s] Line %d: Auto copy enabled. Copying snip to clipboard.\n", __FUNCTIONW__, __LINE__);
if (CopyButton_Click() == FALSE)
{
MyOutputDebugStringW(L"[%s] Line %d: Auto copy failed!\n", __FUNCTIONW__, __LINE__);
CRASH(0);
}
}
}
}
// Allow Escape to terminate the app
if ((WParam == VK_ESCAPE) && ((gAppState == APPSTATE_AFTERCAPTURE) || (gAppState == APPSTATE_BEFORECAPTURE)))
{
MyOutputDebugStringW(L"[%s] Line %d: Escape key was pressed. Exiting application.\n", __FUNCTIONW__, __LINE__);
PostQuitMessage(0);
gMainWindowIsRunning = FALSE;
}
if ((WParam == VK_ESCAPE) && (gAppState == APPSTATE_DELAYCOOKING))
{
// Cancel a current delay countdown.
MyOutputDebugStringW(L"[%s] Line %d: Escape key was pressed during delay countdown. Cancelling countdown.\n", __FUNCTIONW__, __LINE__);
gAppState = APPSTATE_BEFORECAPTURE;
KillTimer(gMainWindowHandle, DELAY_TIMER);
gCurrentDelayCountdown = gStartingDelayCountdown;
}
InvalidateRect(Window, NULL, FALSE);
break;
}
case WM_KEYUP:
{
for (UINT8 Counter = 0; Counter < _countof(gButtons); Counter++)
{
if (WParam == gButtons[Counter]->Hotkey && gButtons[Counter]->Enabled == TRUE)
{
MyOutputDebugStringW(L"[%s] Line %d: Hotkey released, executing button '%s'\n", __FUNCTIONW__, __LINE__, gButtons[Counter]->Caption);
SendMessageW(gMainWindowHandle, WM_COMMAND, gButtons[Counter]->Id, 0);
}
}
break;
}
case WM_SETCURSOR:
{
// To keep Windows from automatically trying to set my cursor for me.
break;
}
case WM_LBUTTONDOWN:
{
MyOutputDebugStringW(L"[%s] Line %d: Left mouse button down.\n", __FUNCTIONW__, __LINE__);
gLeftMouseButtonIsDown = TRUE;
if (CurrentlyDrawing == FALSE && gAppState == APPSTATE_AFTERCAPTURE)
{
BOOL DrawingToolSelected = FALSE;
for (UINT8 Counter = 0; Counter < _countof(gButtons); Counter++)
{
if (gButtons[Counter]->SelectedTool == TRUE)
{
DrawingToolSelected = TRUE;
}
}
if (DrawingToolSelected == FALSE)
{
MyOutputDebugStringW(L"[%s] Line %d: No drawing tool is selected. Won't start drawing.\n", __FUNCTIONW__, __LINE__);
break;
}
POINT Mouse = { 0 };
GetCursorPos(&Mouse);
ScreenToClient(gMainWindowHandle, &Mouse);
MousePosWhenDrawingStarted = Mouse;
if (gCurrentSnipState >= _countof(gSnipStates) - 1)
{
MessageBoxW(gMainWindowHandle, L"Maximum number of changes exceeded. Ctrl+Z to undo a change first.", L"Error", MB_OK | MB_ICONERROR | MB_SYSTEMMODAL);
break;
}
if (Mouse.x < 2 || Mouse.y < 52)
{
MyOutputDebugStringW(L"[%s] Line %d: Mouse was not over the screen capture area. Will not start drawing.\n", __FUNCTIONW__, __LINE__);
break;
}
CurrentlyDrawing = TRUE;
if (gTextButton.SelectedTool == TRUE)
{
MyOutputDebugStringW(L"[%s] Line %d: Text entry mode started.\n", __FUNCTIONW__, __LINE__);
}
else
{
MyOutputDebugStringW(L"[%s] Line %d: Drawing started.\n", __FUNCTIONW__, __LINE__);
}
if (gScratchBitmap != NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: gScratchBitmap was not null, but it was expected to be!\n", __FUNCTIONW__, __LINE__);
}
gScratchBitmap = CopyImage(gSnipStates[gCurrentSnipState], IMAGE_BITMAP, 0, 0, 0);
gCurrentSnipState++;
MyOutputDebugStringW(L"[%s] Line %d: Snips: %i\n", __FUNCTIONW__, __LINE__, gCurrentSnipState);
}
break;
}
case WM_LBUTTONUP:
{
if (gTextButton.SelectedTool == FALSE)
{
MyOutputDebugStringW(L"[%s] Line %d: Left mouse button up. Drawing stopped.\n", __FUNCTIONW__, __LINE__);
}
gLeftMouseButtonIsDown = FALSE;
if (gTextButton.SelectedTool == TRUE)
{
if (MousePosWhenDrawingStarted.x < 2 || MousePosWhenDrawingStarted.y < 52)
{
MyOutputDebugStringW(L"[%s] Line %d: Mouse was not over the screen capture area. Will not place text.\n", __FUNCTIONW__, __LINE__);
break;
}
MyOutputDebugStringW(L"[%s] Line %d: Placing text at %dx%d.\n", __FUNCTIONW__, __LINE__, MousePosWhenDrawingStarted.x, MousePosWhenDrawingStarted.y);
HDC ScratchDC = CreateCompatibleDC(NULL);
SetBkMode(ScratchDC, TRANSPARENT);
SelectObject(ScratchDC, (HBITMAP)gScratchBitmap);
SelectObject(ScratchDC, (HFONT)gFont);
SetTextColor(ScratchDC, gFontColor);
gTextBoxLocation.x = MousePosWhenDrawingStarted.x;
gTextBoxLocation.y = MousePosWhenDrawingStarted.y;
DialogBoxW(NULL, MAKEINTRESOURCEW(IDD_DIALOG1), gMainWindowHandle, (DLGPROC)TextEditCallback);
// Get the size of the font that the user has selected, since the font size will determine the exact coordinates of where the text box and text will go.
HDC DC = CreateCompatibleDC(NULL);
SelectObject(DC, (HFONT)gFont);
TEXTMETRICW TextMetrics = { 0 };
GetTextMetricsW(DC, &TextMetrics);
DeleteDC(DC);
TextOutW(ScratchDC, MousePosWhenDrawingStarted.x - 6, MousePosWhenDrawingStarted.y - 58 - (TextMetrics.tmHeight / 2), gTextBuffer, (int)wcslen(gTextBuffer));
DeleteDC(ScratchDC);
RECT SnipRect = { 0 };
GetClientRect(Window, &SnipRect);
SnipRect.top = 54;
InvalidateRect(Window, &SnipRect, FALSE);
UpdateWindow(gMainWindowHandle);
}
MousePosWhenDrawingStarted.x = 0;
MousePosWhenDrawingStarted.y = 0;
PreviousMousePos.x = 0;
PreviousMousePos.y = 0;
CurrentlyDrawing = FALSE;
if (gScratchBitmap != NULL)
{
gSnipStates[gCurrentSnipState] = CopyImage(gScratchBitmap, IMAGE_BITMAP, 0, 0, 0);
if (DeleteObject(gScratchBitmap) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteObject failed! Was the bitmap still selected into a DC?\n", __FUNCTIONW__, __LINE__);
CRASH(0);
}
else
{
MyOutputDebugStringW(L"[%s] Line %d: gScratchBitmap deleted.\n", __FUNCTIONW__, __LINE__);
}
gScratchBitmap = NULL;
if (gAutoCopy)
{
MyOutputDebugStringW(L"[%s] Line %d: Auto copy enabled. Copying snip to clipboard.\n", __FUNCTIONW__, __LINE__);
if (CopyButton_Click() == FALSE)
{
MyOutputDebugStringW(L"[%s] Line %d: Auto copy failed!\n", __FUNCTIONW__, __LINE__);
CRASH(0);
}
}
}
break;
}
case WM_MOUSEMOVE:
case WM_NCMOUSEMOVE:
{
// Handle drawing first
if (CurrentlyDrawing)
{
if (gScratchBitmap == NULL)
{
MyOutputDebugStringW(L"[%s] Line %d: gScratchBitmap is NULL\n", __FUNCTIONW__, __LINE__);
break;
}
if (gHilighterButton.SelectedTool == TRUE)
{
POINT Mouse = { 0 };
GetCursorPos(&Mouse);
ScreenToClient(gMainWindowHandle, &Mouse);
// Adjust for snip area, maintain Y axis
Mouse.x -= 7;
Mouse.y = MousePosWhenDrawingStarted.y - 66;
HDC ScratchDC = CreateCompatibleDC(NULL);
HDC HilightDC = CreateCompatibleDC(NULL);
SelectObject(ScratchDC, (HBITMAP)gScratchBitmap);
BYTE HilightBits[] = { 0, 0, 0, 0 };
switch (gHilighterButton.Color)
{
case COLOR_YELLOW:
{
memset(&HilightBits[0], 0x00, 1);
memset(&HilightBits[1], 0xFF, 1);
memset(&HilightBits[2], 0xFF, 1);
memset(&HilightBits[3], 0xFF, 1);
break;
}
case COLOR_PINK:
{
memset(&HilightBits[0], 0xDC, 1);
memset(&HilightBits[1], 0x00, 1);
memset(&HilightBits[2], 0xFF, 1);
memset(&HilightBits[3], 0xFF, 1);
break;
}
case COLOR_ORANGE:
{
memset(&HilightBits[0], 0x00, 1);
memset(&HilightBits[1], 0x6A, 1);
memset(&HilightBits[2], 0xFF, 1);
memset(&HilightBits[3], 0xFF, 1);
break;
}
case COLOR_GREEN:
{
memset(&HilightBits[0], 0x00, 1);
memset(&HilightBits[1], 0xFF, 1);
memset(&HilightBits[2], 0x00, 1);
memset(&HilightBits[3], 0xFF, 1);
break;
}
default:
{
MyOutputDebugStringW(L"[%s] Line %d: BUG: Unknown color in hilighter function!\n", __FUNCTIONW__, __LINE__);
}
}
HBITMAP HilightPixel = CreateBitmap(1, 1, 1, 32, HilightBits);
SelectObject(HilightDC, HilightPixel);
BLENDFUNCTION InverseBlendFunction = { AC_SRC_OVER, 0, 0, AC_SRC_ALPHA };
for (UINT8 XPixel = 0; XPixel < 10; XPixel++)
{
for (UINT8 YPixel = 0; YPixel < 20; YPixel++)
{
BOOL PixelAlreadyDrawn = FALSE;
for (UINT16 Counter = 0; Counter < _countof(HilighterPixelsAlreadyDrawn); Counter++)
{
if ((HilighterPixelsAlreadyDrawn[Counter].x == Mouse.x + XPixel) && (HilighterPixelsAlreadyDrawn[Counter].y == Mouse.y + YPixel))
{
PixelAlreadyDrawn = TRUE;
}
}
if (PixelAlreadyDrawn == FALSE)
{
COLORREF CurrentPixelColor = GetPixel(ScratchDC, Mouse.x + XPixel, Mouse.y + YPixel);
UINT16 CurrentPixelBrightness = GetRValue(CurrentPixelColor) + GetGValue(CurrentPixelColor) + GetBValue(CurrentPixelColor);
// x/3, 765 = 255, 382.5 = 128, 0 = 0
InverseBlendFunction.SourceConstantAlpha = (BYTE)(CurrentPixelBrightness / 3);
GdiAlphaBlend(ScratchDC, Mouse.x + XPixel, Mouse.y + YPixel, 1, 1, HilightDC, 0, 0, 1, 1, InverseBlendFunction);
HilighterPixelsAlreadyDrawn[HilighterPixelsAlreadyDrawnCounter].x = (UINT16)(Mouse.x + XPixel);
HilighterPixelsAlreadyDrawn[HilighterPixelsAlreadyDrawnCounter].y = (UINT16)(Mouse.y + YPixel);
HilighterPixelsAlreadyDrawnCounter++;
if (HilighterPixelsAlreadyDrawnCounter >= _countof(HilighterPixelsAlreadyDrawn) - 1)
{
HilighterPixelsAlreadyDrawnCounter = 0;
}
}
}
}
if (DeleteDC(HilightDC) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteDC(HilightDC) failed!\n", __FUNCTIONW__, __LINE__);
}
if (DeleteDC(ScratchDC) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteDC(gScratchDC) failed!\n", __FUNCTIONW__, __LINE__);
}
if (DeleteObject(HilightPixel) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteObject(HilightPixel) failed!\n", __FUNCTIONW__, __LINE__);
}
PreviousMousePos.x = Mouse.x;
RECT SnipRect = { 0 };
GetClientRect(Window, &SnipRect);
SnipRect.top = MousePosWhenDrawingStarted.y - 10;
SnipRect.bottom = MousePosWhenDrawingStarted.y + 10;
InvalidateRect(Window, &SnipRect, FALSE);
UpdateWindow(gMainWindowHandle);
}
else if (gRectangleButton.SelectedTool == TRUE)
{
HBITMAP CleanCopy = CopyImage(gSnipStates[gCurrentSnipState - 1], IMAGE_BITMAP, 0, 0, 0);
HDC CopyDC = CreateCompatibleDC(NULL);
SelectObject(CopyDC, CleanCopy);
HPEN Pen = NULL; //CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
switch (gRectangleButton.Color)
{
case COLOR_RED:
{
Pen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
break;
}
case COLOR_GREEN:
{
Pen = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
break;
}
case COLOR_BLUE:
{
Pen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
break;
}
case COLOR_BLACK:
{
Pen = CreatePen(PS_SOLID, 2, RGB(0, 0, 0));
break;
}
case COLOR_WHITE:
{
Pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 255));
break;
}
case COLOR_YELLOW:
{
Pen = CreatePen(PS_SOLID, 2, RGB(255, 255, 0));
break;
}
default:
{
MyOutputDebugStringW(L"[%s] Line %d: BUG: Unknown color for arrow tool!\n", __FUNCTIONW__, __LINE__);
}
}
SelectObject(CopyDC, Pen);
SelectObject(CopyDC, (HBRUSH)GetStockObject(NULL_BRUSH));
POINT CurrentMousePos = { 0 };
GetCursorPos(&CurrentMousePos);
ScreenToClient(gMainWindowHandle, &CurrentMousePos);
Rectangle(CopyDC, MousePosWhenDrawingStarted.x, MousePosWhenDrawingStarted.y - 56, CurrentMousePos.x, CurrentMousePos.y - 56);
HDC ScratchDC = CreateCompatibleDC(NULL);
SelectObject(ScratchDC, (HBITMAP)gScratchBitmap);
BitBlt(ScratchDC, 0, 0, gCaptureWidth, gCaptureHeight, CopyDC, 0, 0, SRCCOPY);
// DeleteObject will fail if the object is still selected into a DC.
if (DeleteDC(CopyDC) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteDC(CopyDC) failed!\n", __FUNCTIONW__, __LINE__);
}
if (DeleteDC(ScratchDC) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteDC(gScratchDC) failed!\n", __FUNCTIONW__, __LINE__);
}
if (DeleteObject(Pen) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteObject(Pen) failed!\n", __FUNCTIONW__, __LINE__);
}
if (DeleteObject(CleanCopy) == 0)
{
MyOutputDebugStringW(L"[%s] Line %d: DeleteObject(CleanCopy) failed!\n", __FUNCTIONW__, __LINE__);
}
RECT SnipRect = { 0 };
GetClientRect(Window, &SnipRect);
SnipRect.top = 54;
InvalidateRect(Window, &SnipRect, FALSE);
UpdateWindow(gMainWindowHandle);
}
else if (gArrowButton.SelectedTool == TRUE)
{
HBITMAP CleanCopy = CopyImage(gSnipStates[gCurrentSnipState - 1], IMAGE_BITMAP, 0, 0, 0);
HDC CopyDC = CreateCompatibleDC(NULL);
SelectObject(CopyDC, CleanCopy);
HPEN Pen = NULL;
HBRUSH Brush = NULL;
switch (gArrowButton.Color)
{
case COLOR_RED:
{
Pen = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
Brush = CreateSolidBrush(RGB(255, 0, 0));
break;
}
case COLOR_GREEN:
{
Pen = CreatePen(PS_SOLID, 2, RGB(0, 255, 0));
Brush = CreateSolidBrush(RGB(0, 255, 0));
break;
}
case COLOR_BLUE:
{
Pen = CreatePen(PS_SOLID, 2, RGB(0, 0, 255));
Brush = CreateSolidBrush(RGB(0, 0, 255));