-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMainWnd.cpp
307 lines (252 loc) · 5.84 KB
/
MainWnd.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
#include "MainWnd.h"
HINSTANCE hMainInst;
HWND hMainWnd;
HWND hCtrlWnd;
HWND hPaneWnd;
HWND hViewWnd;
HFONT hFont;
HBRUSH hCtrlBrush;
HBRUSH hViewBrush;
HDC hCtrlDC;
HDC hPaneDC;
HDC hViewDC;
HDC hPaneBmpDC;
BOOL bHideCtrl;
BOOL bFull;
int cxScreen;
int cyScreen;
LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
void OnMainSize();
void OnMainDrop(WPARAM);
BOOL CreateMainWnd();
BOOL CreateBackgroundDC();
BOOL LoadBitmapFromResource();
int APIENTRY _tWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPTSTR lpCmdLine, int nCmdShow)
{
MSG msg;
hMainInst = hInst;
if (!BrFailedReturn(!CreateMainWnd(), TRUE))
{
if (lpCmdLine[0] != _T('\0'))
OpenBitmapFile(lpCmdLine);
ShowWindow(hMainWnd, nCmdShow);
UpdateWindow(hMainWnd);
while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
if (hFont)
DeleteObject(hFont);
if (hCtrlBrush)
DeleteObject(hCtrlBrush);
if (hViewBrush)
DeleteObject(hViewBrush);
if (hCtrlDC)
DeleteDC(hCtrlDC);
if (hPaneDC)
DeleteDC(hPaneDC);
if (hViewDC)
DeleteDC(hViewDC);
if (hPaneBmpDC)
DeleteDC(hPaneBmpDC);
OnViewRelease(TRUE);
return 0;
}
LRESULT CALLBACK MainWndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch (uMsg)
{
case WM_SIZE:
OnMainSize();
break;
case WM_DROPFILES:
OnMainDrop(wParam);
break;
case WM_KEYDOWN:
PostMessage(hViewWnd, WM_COMMAND, wParam, 0);
break;
case WM_MOUSEWHEEL:
PostMessage(hViewWnd, WM_COMMAND, ID_ZOOM, wParam);
break;
case WM_DISPLAYCHANGE:
cxScreen = LOWORD(lParam);
cyScreen = HIWORD(lParam);
if (BrFailedReturn(!CreateBackgroundDC(), TRUE))
DestroyWindow(hMainWnd);
break;
case WM_DESTROY:
KillTimer(hViewWnd, 1);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
return 0;
}
void OnMainSize()
{
RECT rc;
int w;
int h;
if (bFull)
{
MoveWindow(hCtrlWnd, (cxScreen - 360), (cyScreen - 62), 350, 52, TRUE);
MoveWindow(hPaneWnd, 0, 0, 350, 52, TRUE);
MoveWindow(hViewWnd, 0, 0, cxScreen, cyScreen, TRUE);
}
else
{
GetClientRect(hMainWnd, &rc);
w = rc.right - rc.left;
h = rc.bottom - rc.top;
MoveWindow(hCtrlWnd, 0, (h - 52), w, 52, TRUE);
MoveWindow(hPaneWnd, (w - 350) / 2, 0, 350, 52, TRUE);
if (!bHideCtrl)
h -= 52;
MoveWindow(hViewWnd, 0, 0, w, h, TRUE);
}
}
void OnMainDrop(WPARAM wParam)
{
UINT ur;
TCHAR szfile[1024];
SetForegroundWindow(hMainWnd);
ur = DragQueryFile((HDROP)wParam, 0, szfile, 1024);
if (!BrFailedReturn(!ur, FALSE))
OpenBitmapFile(szfile);
}
BOOL CreateMainWnd()
{
BOOL br;
NONCLIENTMETRICS ncm;
WNDCLASSEX wcex;
ZeroMemory(&ncm, sizeof(NONCLIENTMETRICS));
ncm.cbSize = sizeof(NONCLIENTMETRICS) - sizeof(ncm.iPaddedBorderWidth);
br = SystemParametersInfo(SPI_GETNONCLIENTMETRICS, ncm.cbSize, &ncm, 0);
if (!br)
return FALSE;
ncm.lfStatusFont.lfHeight = -12;
hFont = CreateFontIndirect(&ncm.lfStatusFont);
if (!hFont)
return FALSE;
hCtrlBrush = CreateSolidBrush(RGB(96, 96, 96));
if (!hCtrlBrush)
return FALSE;
hViewBrush = CreateSolidBrush(RGB(160, 160, 160));
if (!hViewBrush)
return FALSE;
cxScreen = GetSystemMetrics(SM_CXSCREEN);
cyScreen = GetSystemMetrics(SM_CYSCREEN);
br = CreateBackgroundDC();
if (!br)
return FALSE;
br = LoadBitmapFromResource();
if (!br)
return FALSE;
wcex.cbClsExtra = 0;
wcex.cbSize = sizeof(WNDCLASSEX);
wcex.cbWndExtra = 0;
wcex.hbrBackground = hViewBrush;
wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
wcex.hIcon = LoadIcon(hMainInst, MAKEINTRESOURCE(IDI_MAIN));
wcex.hIconSm = wcex.hIcon;
wcex.hInstance = hMainInst;
wcex.lpfnWndProc = MainWndProc;
wcex.lpszClassName = _T("PhotoViewer");
wcex.lpszMenuName = NULL;
wcex.style = 0;
br = RegisterClassEx(&wcex);
if (!br)
return FALSE;
hMainWnd = CreateWindowEx(WS_EX_ACCEPTFILES,
_T("PhotoViewer"), _T("PhotoViewer"),
WS_OVERLAPPEDWINDOW | WS_CLIPCHILDREN,
CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT,
NULL, NULL, hMainInst, NULL);
if (!hMainWnd)
return FALSE;
br = CreateCtrlWnd();
if (!br)
return FALSE;
br = CreateViewWnd();
return br;
}
BOOL CreateBackgroundDC()
{
BOOL br;
HGDIOBJ hobj;
COLORREF color;
br = CreateCanvasDC(&hCtrlDC, hCtrlBrush, cxScreen, 52);
if (!br)
return FALSE;
hobj = SelectObject(hCtrlDC, hFont);
if (hobj == NULL || hobj == HGDI_ERROR)
return FALSE;
br = SetBkMode(hCtrlDC, TRANSPARENT);
if (!br)
return FALSE;
color = SetTextColor(hCtrlDC, RGB(255, 255, 255));
if (color == CLR_INVALID)
return FALSE;
br = CreateCanvasDC(&hPaneDC, hCtrlBrush, 350, 52);
if (!br)
return FALSE;
br = CreateCanvasDC(&hViewDC, hViewBrush, cxScreen, cyScreen);
if (!br)
return FALSE;
hobj = SelectObject(hViewDC, hFont);
if (hobj == NULL || hobj == HGDI_ERROR)
return FALSE;
br = SetBkMode(hViewDC, TRANSPARENT);
if (!br)
return FALSE;
br = SetGraphicsMode(hViewDC, GM_ADVANCED);
if (!br)
return FALSE;
br = SetStretchBltMode(hViewDC, HALFTONE);
return br;
}
BOOL LoadBitmapFromResource()
{
BOOL br = FALSE;
HBITMAP hbmp = NULL;
HDC hdc = NULL;
HDC hbmpdc = NULL;
HGDIOBJ hobj;
BITMAP bmp;
BLENDFUNCTION ftn;
hbmp = LoadBitmap(hMainInst, MAKEINTRESOURCE(IDB_PANE));
if (!hbmp)
goto end;
hdc = GetDC(NULL);
if (!hdc)
goto end;
hbmpdc = CreateCompatibleDC(hdc);
if (!hbmpdc)
goto end;
hobj = SelectObject(hbmpdc, hbmp);
if (hobj == NULL || hobj == HGDI_ERROR)
goto end;
br = GetObject(hbmp, sizeof(BITMAP), &bmp);
if (!br)
goto end;
br = CreateCanvasDC(&hPaneBmpDC, hCtrlBrush, bmp.bmWidth, bmp.bmHeight);
if (!br)
goto end;
ftn.AlphaFormat = AC_SRC_ALPHA;
ftn.BlendFlags = 0;
ftn.BlendOp = 0;
ftn.SourceConstantAlpha = 255;
br = GdiAlphaBlend(hPaneBmpDC, 0, 0, bmp.bmWidth, bmp.bmHeight,
hbmpdc, 0, 0, bmp.bmWidth, bmp.bmHeight, ftn);
end:
if (hbmpdc)
DeleteDC(hbmpdc);
if (hdc)
ReleaseDC(NULL, hdc);
if (hbmp)
DeleteObject(hbmp);
return br;
}