-
Notifications
You must be signed in to change notification settings - Fork 1
/
TearingExample.c
316 lines (238 loc) · 7.18 KB
/
TearingExample.c
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
// Right click to capture a screenshot after launching the program.
#include <stdio.h>
#include <windows.h>
#include <windowsx.h>
#include <assert.h>
#include <commctrl.h>
LRESULT CALLBACK WndProc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
SCROLLINFO si;
static HDC hdcWin;
static HDC hdcScreen;
static HDC hdcScreenCompat;
static HBITMAP hbmpCompat;
static BITMAP bmp;
static BOOL fBlt;
static BOOL fScroll;
static BOOL fSize;
static int xMinScroll;
static int xCurrentScroll;
static int xMaxScroll;
static int yMinScroll;
static int yCurrentScroll;
static int yMaxScroll;
switch (uMsg) {
case WM_CREATE: {
hdcScreen = CreateDC("DISPLAY", (LPCTSTR) NULL, (LPCTSTR) NULL, (CONST DEVMODE *) NULL);
hdcScreenCompat = CreateCompatibleDC(hdcScreen);
bmp.bmBitsPixel = (BYTE) GetDeviceCaps(hdcScreen, BITSPIXEL);
bmp.bmPlanes = (BYTE) GetDeviceCaps(hdcScreen, PLANES);
bmp.bmWidth = GetDeviceCaps(hdcScreen, HORZRES);
bmp.bmHeight = GetDeviceCaps(hdcScreen, VERTRES);
bmp.bmWidthBytes = ((bmp.bmWidth + 15) & ~15) / 8;
hbmpCompat = CreateBitmap(bmp.bmWidth, bmp.bmHeight, bmp.bmPlanes, bmp.bmBitsPixel, (CONST VOID *) NULL);
SelectObject(hdcScreenCompat, hbmpCompat);
fBlt = FALSE;
fScroll = FALSE;
fSize = FALSE;
xMinScroll = 0;
xCurrentScroll = 0;
xMaxScroll = 0;
yMinScroll = 0;
yCurrentScroll = 0;
yMaxScroll = 0;
break;
}
case WM_SIZE: {
int xNewSize;
int yNewSize;
xNewSize = LOWORD(lParam);
yNewSize = HIWORD(lParam);
if (fBlt) {
fSize = TRUE;
}
xMaxScroll = max(bmp.bmWidth - xNewSize, 0);
xCurrentScroll = min(xCurrentScroll, xMaxScroll);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = xMinScroll;
si.nMax = bmp.bmWidth;
si.nPage = xNewSize;
si.nPos = xCurrentScroll;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
yMaxScroll = max(bmp.bmHeight - yNewSize, 0);
yCurrentScroll = min(yCurrentScroll, yMaxScroll);
si.cbSize = sizeof(si);
si.fMask = SIF_RANGE | SIF_PAGE | SIF_POS;
si.nMin = yMinScroll;
si.nMax = bmp.bmHeight;
si.nPage = yNewSize;
si.nPos = yCurrentScroll;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
break;
}
case WM_PAINT: {
PRECT prect;
hdc = BeginPaint(hwnd, &ps);
if (fSize) {
BitBlt(ps.hdc, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcScreenCompat, xCurrentScroll, yCurrentScroll, SRCCOPY);
fSize = FALSE;
}
if (fScroll) {
Sleep(350);
prect = &ps.rcPaint;
BitBlt(ps.hdc, prect->left, prect->top, (prect->right - prect->left), (prect->bottom - prect->top),
hdcScreenCompat, prect->left + xCurrentScroll, prect->top + yCurrentScroll, SRCCOPY);
fScroll = FALSE;
}
EndPaint(hwnd, &ps);
break;
}
case WM_HSCROLL: {
int xDelta;
int xNewPos;
int yDelta = 0;
switch (LOWORD(wParam)) {
case SB_PAGEUP:
xNewPos = xCurrentScroll - 50;
break;
case SB_PAGEDOWN:
xNewPos = xCurrentScroll + 50;
break;
case SB_LINEUP:
xNewPos = xCurrentScroll - 5;
break;
case SB_LINEDOWN:
xNewPos = xCurrentScroll + 5;
break;
case SB_THUMBPOSITION:
xNewPos = HIWORD(wParam);
break;
case SB_THUMBTRACK:
xNewPos = HIWORD(wParam);
break;
default:
xNewPos = xCurrentScroll;
}
xNewPos = max(0, xNewPos);
xNewPos = min(xMaxScroll, xNewPos);
if (xNewPos == xCurrentScroll) {
break;
}
fScroll = TRUE;
xDelta = xNewPos - xCurrentScroll;
xCurrentScroll = xNewPos;
ScrollWindowEx(hwnd, -xDelta, -yDelta, (CONST RECT *) NULL,
(CONST RECT *) NULL, (HRGN) NULL, (PRECT) NULL, SW_INVALIDATE);
UpdateWindow(hwnd);
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = xCurrentScroll;
SetScrollInfo(hwnd, SB_HORZ, &si, TRUE);
break;
}
case WM_VSCROLL: {
int xDelta = 0;
int yDelta;
int yNewPos;
switch (LOWORD(wParam)) {
case SB_PAGEUP:
yNewPos = yCurrentScroll - 50;
break;
case SB_PAGEDOWN:
yNewPos = yCurrentScroll + 50;
break;
case SB_LINEUP:
yNewPos = yCurrentScroll - 5;
break;
case SB_LINEDOWN:
yNewPos = yCurrentScroll + 5;
break;
case SB_THUMBPOSITION:
yNewPos = HIWORD(wParam);
break;
case SB_THUMBTRACK:
yNewPos = HIWORD(wParam);
break;
default:
yNewPos = yCurrentScroll;
}
yNewPos = max(0, yNewPos);
yNewPos = min(yMaxScroll, yNewPos);
if (yNewPos == yCurrentScroll) {
break;
}
fScroll = TRUE;
yDelta = yNewPos - yCurrentScroll;
yCurrentScroll = yNewPos;
ScrollWindowEx(hwnd, -xDelta, -yDelta, (CONST RECT *) NULL,
(CONST RECT *) NULL, (HRGN) NULL, (PRECT) NULL, SW_INVALIDATE);
UpdateWindow(hwnd);
si.cbSize = sizeof(si);
si.fMask = SIF_POS;
si.nPos = yCurrentScroll;
SetScrollInfo(hwnd, SB_VERT, &si, TRUE);
break;
}
case WM_RBUTTONDOWN: {
hdcWin = GetDC(hwnd);
RECT rect;
GetClientRect(hwnd, &rect);
FillRect(hdcWin, &rect, (HBRUSH)(COLOR_WINDOW + 1));
BitBlt(hdcScreenCompat, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcScreen, 0, 0, SRCCOPY);
BitBlt(hdcWin, 0, 0, bmp.bmWidth, bmp.bmHeight, hdcScreenCompat, 0, 0, SRCCOPY);
ReleaseDC(hwnd, hdcWin);
fBlt = TRUE;
break;
}
case WM_CLOSE: {
DestroyWindow(hwnd);
}
case WM_DESTROY: {
PostQuitMessage (0);
return 0;
}
}
return DefWindowProc (hwnd, uMsg, wParam, lParam);
}
const char g_szClassName[] = "myWindowClass";
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG Msg;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = 0;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszMenuName = NULL;
wc.lpszClassName = g_szClassName;
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if(!RegisterClassEx(&wc)) {
MessageBox(NULL, "Window Registration Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return -1;
}
hwnd = CreateWindowEx(
WS_EX_CLIENTEDGE,
g_szClassName,
"Tearing Example",
WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, CW_USEDEFAULT, 640, 480,
NULL, NULL, hInstance, NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!", "Error!", MB_ICONEXCLAMATION | MB_OK);
return -1;
}
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
while(GetMessage(&Msg, NULL, 0, 0) > 0) {
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
return Msg.wParam;
}