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
| #include "stdafx.h"
#include <iostream>
#include "Lazariev_CourseProjectWinAPI_21_03_(1).h"
using namespace std;
#define MAX_LOADSTRING 100
// Global Variables:
HINSTANCE hInst;
HDC hdc,memDC;
TCHAR szTitle[MAX_LOADSTRING], szWindowClass[MAX_LOADSTRING];
wchar_t szFile[260];
OPENFILENAME ofn;
HBRUSH brush = CreateSolidBrush(RGB(255,255,255));
HBITMAP hBitmap;
BITMAP bm;
static POINT pos;
RECT rc;
POINT pt;
PAINTSTRUCT ps;
BYTE R, G, B;
double covolutionMatrix[3][3];
COLORREF color;
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
//BOOL OpenFileDialog(HWND hwnd, LPTSTR pFileName, LPTSTR pTitleName);
//Open
int MyOpenFile(HWND hWnd);
//PictureMoving
void image_free( HBITMAP img);
void image_focus(HDC hDC, BITMAP* img, const LPPOINT pt);
void drawPicture(HWND hWnd, HDC hdc, HDC memDC);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: Place code here.
// Initialize global strings
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_LAZARIEV_COURSEPROJECTWINAPI_21_03_1, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Perform application initialization:
if (!InitInstance (hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_LAZARIEV_COURSEPROJECTWINAPI_21_03_1));
MSG msg;
// Main message loop:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&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 = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_LAZARIEV_COURSEPROJECTWINAPI_21_03_1));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_LAZARIEV_COURSEPROJECTWINAPI_21_03_1);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Store instance handle in our global variable
HWND hWnd = CreateWindowW(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, nullptr, nullptr, hInstance, nullptr);
if (!hWnd)
{
return FALSE;
}
ShowWindow(hWnd, nCmdShow);
UpdateWindow(hWnd);
return TRUE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
float div = 9;
static HDC thdc;
//static image_t img;
static POINT offset, prev;
static bool isMove = false;
static HCURSOR hcur = NULL, scur = NULL;
switch (message)
{
case WM_CREATE:
thdc = GetDC(hWnd);
GetClientRect(hWnd, &rc);
pos.x = (rc.right - bm.bmWidth) / 2;
pos.y = (rc.bottom - bm.bmHeight) / 2;
hcur = LoadCursor(NULL, IDC_SIZEALL);
scur = LoadCursor(NULL, IDC_ARROW);
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Parse the menu selections:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
case ID_FILE_OPEN:
MyOpenFile(hWnd);
break;
case ID_FILE_SAVE:
break;
case ID_FILE_SAVEAS:
break;
// ФИЛЬТР
case ID_FILTERONE:
//MessageBox(hWnd,L"Filter has applied",L"Filter",MB_OK);
//размытие
covolutionMatrix[0][0] = 1 / div;
covolutionMatrix[0][1] = 1 / div;
covolutionMatrix[0][2] = 1 / div;
covolutionMatrix[1][0] = 1 / div;
covolutionMatrix[1][1] = 1 / div;
covolutionMatrix[1][2] = 1 / div;
covolutionMatrix[2][0] = 1 / div;
covolutionMatrix[2][1] = 1 / div;
covolutionMatrix[2][2] = 1 / div;
for (int i = 0; i < bm.bmWidth; i++)
{
for (int j = 0; j < bm.bmHeight; j++)
{
int koefR = 0, koefG = 0, koefB = 0;
for (int i2 = -1; i2 < 2; i2++)
{
for (int j2 = -1; j2 < 2; j2++)
{
int columnIndex = i + i2, rowIndex = j + j2;
if (columnIndex < 0) columnIndex++;
else if (columnIndex >= bm.bmWidth) columnIndex--;
if (rowIndex < 0)rowIndex++;
else if (rowIndex >= bm.bmHeight) rowIndex--;
color = GetPixel(memDC,columnIndex,rowIndex);
R = color = RGB(255,0,0);
G = color = RGB(0, 255, 0);
B = color = RGB(0, 0, 255);
koefR += R * covolutionMatrix[1 + i2][1 + j2];
koefG += G * covolutionMatrix[1 + i2][1 + j2];
koefB += B * covolutionMatrix[1 + i2][1 + j2];
}
}
if (koefR < 0)koefR = 0;
if (koefG < 0)koefG = 0;
if (koefB < 0)koefB = 0;
if (koefR > 255)koefR = 255;
if (koefG > 255)koefG = 255;
if (koefB > 255)koefB = 255;
SetPixel(memDC,i,j,color);
}
}
InvalidateRect(hWnd,&rc,TRUE);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
hdc = BeginPaint(hWnd, &ps);
drawPicture(hWnd,hdc,memDC);
EndPaint(hWnd, &ps);
}
break;
case WM_LBUTTONDOWN:
{
GetCursorPos(&offset);
ScreenToClient(hWnd, &offset);
SetRect(&rc, pos.x, pos.y, pos.x + bm.bmWidth, pos.y + bm.bmHeight);
if (PtInRect(&rc, offset))
{
isMove = true;
prev.x = offset.x - (offset.x - pos.x);
prev.y = offset.y - (offset.y - pos.y);
offset.x -= pos.x;
offset.y -= pos.y;
SetCursor(hcur);
}
}
break;
case WM_MOUSEMOVE:
if (isMove)
{
GetCursorPos(&pt);
ScreenToClient(hWnd, &pt);
image_focus(thdc, &bm, &prev);
prev.x = pt.x - offset.x;
prev.y = pt.y - offset.y;
image_focus(thdc, &bm, &prev);
}
break;
case WM_LBUTTONUP:
if (isMove)
{
pt = pos;
pos = prev;
SetRect(&rc, prev.x, prev.y, prev.x + bm.bmWidth, prev.y + bm.bmHeight);
InvalidateRect(hWnd, &rc, TRUE);
SetRect(&rc, pt.x, pt.y, pt.x + bm.bmWidth, pt.y + bm.bmHeight);
InvalidateRect(hWnd, &rc, TRUE);
SetCursor(scur);
}
isMove = false;
break;
case WM_SETCURSOR:
if (!isMove)
return DefWindowProc(hWnd, message, wParam, lParam);
break;
case WM_DESTROY:
image_free(hBitmap);
DestroyCursor(hcur);
DestroyCursor(scur);
ReleaseDC(hWnd, memDC);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
UNREFERENCED_PARAMETER(lParam);
switch (message)
{
case WM_INITDIALOG:
return (INT_PTR)TRUE;
case WM_COMMAND:
if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
{
EndDialog(hDlg, LOWORD(wParam));
return (INT_PTR)TRUE;
}
break;
}
return (INT_PTR)FALSE;
}
int MyOpenFile(HWND hWnd)
{
ZeroMemory(&ofn, sizeof(ofn));
ofn.lStructSize = sizeof(ofn);
ofn.hwndOwner = hWnd;
ofn.lpstrFile = szFile;
ofn.lpstrFile[0] = '\0';
ofn.nMaxFile = sizeof(szFile);
ofn.lpstrFilter = L"Bitmap files(*.bmp)\0*.bmp\0JPEG files(*.jpg)\0*.jpg\0All files(*.*)\0*.*\0\0";
ofn.nFilterIndex = 1;
ofn.lpstrFileTitle = NULL;
ofn.nMaxFileTitle = 0;
ofn.lpstrInitialDir = NULL;
ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
GetOpenFileName(&ofn);
hdc = GetDC(hWnd);
memDC = CreateCompatibleDC(hdc);
hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hBitmap, sizeof(BITMAP), &bm);
SelectObject(memDC, hBitmap);
BitBlt(hdc, pos.x, pos.y, bm.bmWidth, bm.bmHeight, memDC, 0, 0, SRCCOPY);
DeleteDC(memDC);
ReleaseDC(hWnd, memDC);
DeleteObject(hBitmap);
ReleaseDC(hWnd, hdc);
UpdateWindow(hWnd);
return 0;
}
void image_focus(HDC hDC, BITMAP* img, const LPPOINT pt)
{
RECT rc;
SetROP2(hDC, R2_NOT);
SetRect(&rc, pt->x, pt->y, pt->x + img->bmWidth, pt->y + img->bmHeight);
DrawFocusRect(hDC, &rc);
}
void image_free(HBITMAP img)
{
if (hBitmap != NULL)
{
//SelectObject(memDC,brush);
DeleteObject(hBitmap);
hBitmap = NULL;
}
if (memDC != NULL)
{
DeleteDC(memDC);
memDC = NULL;
}
}
void drawPicture(HWND hWnd, HDC hdc, HDC memDC)
{
hdc = GetDC(hWnd);
memDC = CreateCompatibleDC(hdc);
hBitmap = (HBITMAP)LoadImage(NULL, ofn.lpstrFile, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
GetObject(hBitmap, sizeof(BITMAP), &bm);
SelectObject(memDC, hBitmap);
BitBlt(hdc, pos.x, pos.y, bm.bmWidth, bm.bmHeight, memDC, 0, 0, SRCCOPY);
} |