View difference between Paste ID: 7qq0gQd8 and AVLsfend
SHOW: | | - or go back to the newest paste.
1
#include <iostream>
2
#include <windows.h>
3
#include <tchar.h>
4
#include <stdio.h>
5
#include "Shlwapi.h"
6
#include <fstream>
7
#pragma comment(lib, "Shlwapi.lib")
8
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
9
WNDCLASS w;
10
INT N = 10;
11
COLORREF lineColor = RGB(255, 0, 0);
12
COLORREF colorBackground = RGB(0, 0, 255);
13
COLORREF colorCircle = RGB(255, 0, 0);
14
int locat[105][105];
15
const int idVoidCell = 0;
16
const int idСrossCell = 1;
17
const int idСircleCell = 2;
18
HBRUSH hBrush;
19
HWND hWnd;
20
HDC hdc;
21
int WIDTH_WINDOW = 320;
22
int HEIGHT_WINDOW = 240;
23
int* mas = NULL;
24
int size=30;
25
LPCWSTR WM_COORDINATE_O = L"WM_COORDINATE_O";
26
LPCWSTR WM_COORDINATE_X = L"WM_COORDINATE_X";
27
28
UINT MyMSG_O = RegisterWindowMessage(WM_COORDINATE_O);
29
UINT MyMSG_X = RegisterWindowMessage(WM_COORDINATE_X);
30
void setIndent() {
31
    RECT rect;
32
    rect = { 0 };
33
34
    GetWindowRect(hWnd, &rect);
35
    
36
37
    size = int(min(0.85 * (rect.bottom - rect.top) / N, 0.85 * (rect.right - rect.left) / N));
38
}
39
void HLS() {
40
   
41
    int R = (int(GetRValue(lineColor)));
42
    int G = (int(GetGValue(lineColor)));
43
    int B = (int(GetBValue(lineColor)));
44
45
    
46
    
47
    COLORREF RgbColor = RGB(R, G, B);
48
49
    
50
    WORD Hue = 0;
51
    WORD Luminance = 0;
52
    WORD Saturation = 0;
53
    ColorRGBToHLS(RgbColor, &Hue, &Luminance, &Saturation);
54
55
    
56
    RgbColor = ColorHLSToRGB(Hue+10, Luminance, Saturation);
57
    lineColor = ColorHLSToRGB(Hue + 10, Luminance, Saturation);
58
   
59
    
60
    SendMessage(hWnd, WM_PAINT, ' ', 1L);
61
   
62
63
}
64
void drawCross(int x = 0, int y = 0) {
65
66
    hdc = GetDC(hWnd);
67
68
    HPEN hPen;
69
    hPen = CreatePen(1, 4, RGB(250, 0, 0));
70
    SelectObject(hdc, hPen);
71
72
    MoveToEx(hdc, x * size, y * size, NULL);
73
    LineTo(hdc, (x * size) + size, (y * size) + size);
74
75
    MoveToEx(hdc, x * size + size, y * size, NULL);
76
    LineTo(hdc, x * size, y * size + size);
77
78
79
    DeleteObject(hPen);
80
    ReleaseDC(hWnd, hdc);
81
82
83
}
84
void drawCircle(int x = 0, int y = 0) {
85
86
    hdc = GetDC(hWnd);
87
    
88
   HBRUSH hBrush = CreateSolidBrush(colorCircle); //задаём сплошную кисть, закрашенную цветом RGB
89
    SelectObject(hdc, hBrush); //делаем кисть активной
90
91
    Ellipse(hdc, x * size, y * size, x * size + size, y * size + size);
92
93
    ReleaseDC(hWnd, hdc);
94
    DeleteObject(hBrush);
95
}
96
void drawField() {
97
98
99
    //закрасить фон
100
    HBRUSH hBrush2 = hBrush;
101
    hBrush = CreateSolidBrush(colorBackground);
102
   SetClassLongPtr(hWnd, GCL_HBRBACKGROUND, (LONG)hBrush);
103
    InvalidateRect(hWnd, NULL, TRUE);
104
105
    PAINTSTRUCT ps;
106
    HPEN hPen;
107
    hPen = CreatePen(PS_DASHDOT, 2, lineColor);
108
    hdc = BeginPaint(hWnd, &ps);
109
    int i;
110
111
    for (i = 1; i <= N; i++) {
112
113
114
        MoveToEx(hdc, 0, i * size, NULL);
115
        SelectObject(hdc, hPen);
116
        LineTo(hdc, N * size, i * size);
117
118
        MoveToEx(hdc, i * size, 0, NULL);
119
        SelectObject(hdc, hPen);
120
        LineTo(hdc, i * size, N * size);
121
122
    }
123
124
    for (int i = 0; i < N; ++i)
125
        for (int j = 0; j < N; ++j)
126
            switch (mas[i * N + j])
127
            {
128
            case idСrossCell:
129
                drawCross(i, j);
130
                break;
131
            case idСircleCell:
132
                drawCircle(i, j);
133
                break;
134
            default:
135
                break;
136
            }
137
138
139
    EndPaint(hWnd, &ps);
140
    DeleteObject(hPen);
141
    DeleteObject(hBrush2);
142
143
}
144
void saveConfig() {
145
    RECT rect;
146
    rect = { 0 };
147
    GetWindowRect(hWnd, &rect);
148
    std::ofstream config;
149
    config.open("config.txt");
150
    config << N << std::endl;
151
    config << rect.right - rect.left << " " << rect.bottom - rect.top << std::endl;
152
    config << int(GetRValue(colorBackground)) << " " << int(GetGValue(colorBackground)) << " " << int(GetBValue(colorBackground)) << std::endl;
153
    config << int(GetRValue(lineColor)) << " " << int(GetGValue(lineColor)) << " " << int(GetBValue(lineColor)) << std::endl;
154
    config.close();
155
156
}
157
void loadConfig() {
158
    std::ifstream config;
159
    config.open("config.txt");
160
    if (!config.is_open()) {
161
        std::cout << "Error opening file";
162
        return;
163
    }
164
    config >> N;
165
    config >> WIDTH_WINDOW >> HEIGHT_WINDOW;
166
    int R, G, B;
167
    config >> R >> G >> B;
168
    colorBackground = RGB(R, G, B);
169
    config >> R >> G >> B;
170
    lineColor = RGB(R, G, B);
171
    config.close();
172
}
173
void RunNotepad(void)
174
{
175
    STARTUPINFO sInfo;
176
    PROCESS_INFORMATION pInfo;
177
178
    ZeroMemory(&sInfo, sizeof(STARTUPINFO));
179
180
181
    CreateProcess(_T("C:\\Windows\\Notepad.exe"),
182
        NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo);
183
    
184
}
185
int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
186
    
187
    MSG lpMsg;
188
    loadConfig();
189
    LPWSTR* szArglist;
190
    int nArgs;
191
    szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
192
    if (nArgs > 1)
193
        N = _wtoi(szArglist[1]);
194
195
    LocalFree(szArglist);
196
197
198
    HANDLE File_Map = CreateFileMapping(
199
        INVALID_HANDLE_VALUE,
200
        NULL,
201
        PAGE_READWRITE,
202
        0,
203
        sizeof(int) * N * N,
204
        L"FileMapping"
205
    );
206
207
    mas = (int*)MapViewOfFile(
208
        File_Map,
209
        FILE_MAP_ALL_ACCESS,
210
        0,
211
        0,
212
        sizeof(int) * N * N
213
    );
214
215
216
217
    
218
219
220
    w.lpszClassName = L"MyAppClass"; //имя программы 
221
    w.hInstance = hInstance; //идентификатор текущего приложения
222
    w.lpfnWndProc = WndProc; //указатель на функцию окна
223
    w.hCursor = LoadCursor(NULL, IDC_ARROW); //загружаем курсор
224
    w.hIcon = 0;
225
    w.lpszMenuName = 0;
226
    hBrush = CreateSolidBrush(lineColor);
227
    w.hbrBackground = hBrush;
228
229
230
    w.style = CS_HREDRAW | CS_VREDRAW;
231
    w.cbClsExtra = 0;
232
    w.cbWndExtra = 0;
233
234
    //Если не удалось зарегистрировать класс окна - выходим
235
    if (!RegisterClass(&w))
236
        return 0;
237
238
    //Создадим окно в памяти, заполнив аргументы CreateWindow
239
    hWnd = CreateWindow(L"MyAppClass", //Имя программы
240
        L"Грфические возможности Win32 API", //Заголовок окна
241
        WS_OVERLAPPEDWINDOW, //Стиль окна - перекрывающееся
242
        CW_USEDEFAULT, //положение окна на экране по х
243
        CW_USEDEFAULT, //положение по у
244
        WIDTH_WINDOW, //ширина
245
        HEIGHT_WINDOW,
246
        HWND_DESKTOP, //идентификатор родительского окна
247
        (HMENU)NULL, //идентификатор меню
248
        (HINSTANCE)hInstance, //идентификатор экземпляра программы
249
        (HINSTANCE)NULL); //отсутствие дополнительных параметров
250
251
    //Выводим окно из памяти на экран
252
    ShowWindow(hWnd, nCmdShow);
253
254
    //Обновим содержимое окна
255
    UpdateWindow(hWnd);
256
257
    //Цикл обработки сообщений
258
259
    while (GetMessage(&lpMsg, NULL, 0, 0)) {
260
        TranslateMessage(&lpMsg);
261
        DispatchMessage(&lpMsg);
262
    }
263
264
    return(lpMsg.wParam);
265
266
    DestroyWindow(hWnd);
267
    UnregisterClass(L"MyAppClass", hInstance);
268
    UnmapViewOfFile(mas);
269
    CloseHandle(File_Map);
270
    UnregisterHotKey(hWnd, 1);
271
    UnregisterHotKey(hWnd, 2);
272
    DeleteObject(hBrush);
273
    
274
    return 0;
275
}
276
277
//Функция окна
278
LRESULT CALLBACK WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam) {
279
    HDC hdc; //создаём контекст устройства
280
    PAINTSTRUCT ps; //создаём экземпляр структуры графического вывода
281
    (RegisterHotKey(
282
        hWnd,
283
        1,
284
        MOD_CONTROL,
285
        0x51));
286
    (RegisterHotKey(
287
        hWnd,
288
        2,
289
        MOD_SHIFT,
290
        0x43));
291
292
    setIndent();
293
294
    int x = 0, y = 0;
295
    if (messg == MyMSG_O)
296
    {
297
        SendMessage(hWnd, WM_PAINT, ' ', 1L);
298
299
    }
300
    //Цикл обработки сообщений
301
    switch (messg) {
302
    case WM_LBUTTONDOWN:
303
        x = LOWORD(lParam);//координаты с которыми нажата мышь - аргументы WndProc
304
        y = HIWORD(lParam);
305
306
        x /= size;
307
        y /= size;
308
        if (mas[x * N + y] != idСircleCell)
309
        {
310
            mas[x * N + y] = idСircleCell;
311
            PostMessage(HWND_BROADCAST, MyMSG_O, x,
312
                y);
313
        }
314
        break;
315
    case WM_RBUTTONDOWN:
316
317
        x = LOWORD(lParam);//координаты с которыми нажата мышь - аргументы WndProc
318
        y = HIWORD(lParam);
319
320
        x /= size;
321
        y /= size;
322
        if (mas[x * N + y] != idСrossCell)
323
        {
324
            mas[x * N + y] = idСrossCell;
325
            SendMessage(HWND_BROADCAST, MyMSG_O,
326
                x,
327
                y);
328
        }
329
330
        break;
331
        //сообщение рисования
332
    case WM_PAINT:
333
334
        drawField();
335
        break;
336
337
    case WM_KEYDOWN:
338
339
        if (wParam == VK_RETURN) {
340
            PAINTSTRUCT ps;
341
            colorBackground = RGB(rand() % 255, rand() % 255, rand() % 255);
342
            HBRUSH hBrush2 = hBrush;
343
            hBrush = CreateSolidBrush(colorBackground);
344
            SetClassLongPtr(hWnd, GCL_HBRBACKGROUND, (LONG)hBrush);
345
            InvalidateRect(hWnd, NULL, TRUE);
346
            DeleteObject(hBrush2);
347
           
348
            return 0;
349
        }
350
        if (wParam == VK_ESCAPE) {
351
            DestroyWindow(hWnd);
352
353
        }
354
355
        break;
356
    case WM_MOUSEWHEEL:
357
        HLS();
358
       
359
        break;
360
    case WM_HOTKEY:
361
362
        if (wParam == 1)
363
        {
364
            DestroyWindow(hWnd);
365
366
367
        }
368
        if (wParam == 2) {
369
            RunNotepad();
370
        }
371
        break;
372
373
374
375
    case WM_DESTROY:
376
        saveConfig();
377
        PostQuitMessage(0);       /* send a WM_QUIT to the message queue */
378
379
380
381
        break;
382
383
384
    default:
385
        return(DefWindowProc(hWnd, messg, wParam, lParam)); //освобождаем очередь приложения от нераспознаных
386
    }
387
    return 0;
388
}