Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <windows.h>
- #include <tchar.h>
- #include <stdio.h>
- #include "Shlwapi.h"
- #include <fstream>
- #pragma comment(lib, "Shlwapi.lib")
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- WNDCLASS w;
- INT N = 10;
- COLORREF lineColor = RGB(255, 0, 0);
- COLORREF colorBackground = RGB(0, 0, 255);
- COLORREF colorCircle = RGB(255, 0, 0);
- int locat[105][105];
- const int idVoidCell = 0;
- const int idСrossCell = 1;
- const int idСircleCell = 2;
- HBRUSH hBrush;
- HWND hWnd;
- HDC hdc;
- int WIDTH_WINDOW = 320;
- int HEIGHT_WINDOW = 240;
- int* mas = NULL;
- int size=30;
- LPCWSTR WM_COORDINATE_O = L"WM_COORDINATE_O";
- LPCWSTR WM_COORDINATE_X = L"WM_COORDINATE_X";
- UINT MyMSG_O = RegisterWindowMessage(WM_COORDINATE_O);
- UINT MyMSG_X = RegisterWindowMessage(WM_COORDINATE_X);
- void setIndent() {
- RECT rect;
- rect = { 0 };
- GetWindowRect(hWnd, &rect);
- size = int(min(0.85 * (rect.bottom - rect.top) / N, 0.85 * (rect.right - rect.left) / N));
- }
- void HLS() {
- int R = (int(GetRValue(lineColor)));
- int G = (int(GetGValue(lineColor)));
- int B = (int(GetBValue(lineColor)));
- COLORREF RgbColor = RGB(R, G, B);
- WORD Hue = 0;
- WORD Luminance = 0;
- WORD Saturation = 0;
- ColorRGBToHLS(RgbColor, &Hue, &Luminance, &Saturation);
- RgbColor = ColorHLSToRGB(Hue+10, Luminance, Saturation);
- lineColor = ColorHLSToRGB(Hue + 10, Luminance, Saturation);
- SendMessage(hWnd, WM_PAINT, ' ', 1L);
- }
- void drawCross(int x = 0, int y = 0) {
- hdc = GetDC(hWnd);
- HPEN hPen;
- hPen = CreatePen(1, 4, RGB(250, 0, 0));
- SelectObject(hdc, hPen);
- MoveToEx(hdc, x * size, y * size, NULL);
- LineTo(hdc, (x * size) + size, (y * size) + size);
- MoveToEx(hdc, x * size + size, y * size, NULL);
- LineTo(hdc, x * size, y * size + size);
- DeleteObject(hPen);
- ReleaseDC(hWnd, hdc);
- }
- void drawCircle(int x = 0, int y = 0) {
- hdc = GetDC(hWnd);
- HBRUSH hBrush = CreateSolidBrush(colorCircle); //задаём сплошную кисть, закрашенную цветом RGB
- SelectObject(hdc, hBrush); //делаем кисть активной
- Ellipse(hdc, x * size, y * size, x * size + size, y * size + size);
- ReleaseDC(hWnd, hdc);
- DeleteObject(hBrush);
- }
- void drawField() {
- //закрасить фон
- HBRUSH hBrush2 = hBrush;
- hBrush = CreateSolidBrush(colorBackground);
- SetClassLongPtr(hWnd, GCL_HBRBACKGROUND, (LONG)hBrush);
- InvalidateRect(hWnd, NULL, TRUE);
- PAINTSTRUCT ps;
- HPEN hPen;
- hPen = CreatePen(PS_DASHDOT, 2, lineColor);
- hdc = BeginPaint(hWnd, &ps);
- int i;
- for (i = 1; i <= N; i++) {
- MoveToEx(hdc, 0, i * size, NULL);
- SelectObject(hdc, hPen);
- LineTo(hdc, N * size, i * size);
- MoveToEx(hdc, i * size, 0, NULL);
- SelectObject(hdc, hPen);
- LineTo(hdc, i * size, N * size);
- }
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- switch (mas[i * N + j])
- {
- case idСrossCell:
- drawCross(i, j);
- break;
- case idСircleCell:
- drawCircle(i, j);
- break;
- default:
- break;
- }
- EndPaint(hWnd, &ps);
- DeleteObject(hPen);
- DeleteObject(hBrush2);
- }
- void saveConfig() {
- RECT rect;
- rect = { 0 };
- GetWindowRect(hWnd, &rect);
- std::ofstream config;
- config.open("config.txt");
- config << N << std::endl;
- config << rect.right - rect.left << " " << rect.bottom - rect.top << std::endl;
- config << int(GetRValue(colorBackground)) << " " << int(GetGValue(colorBackground)) << " " << int(GetBValue(colorBackground)) << std::endl;
- config << int(GetRValue(lineColor)) << " " << int(GetGValue(lineColor)) << " " << int(GetBValue(lineColor)) << std::endl;
- config.close();
- }
- void loadConfig() {
- std::ifstream config;
- config.open("config.txt");
- if (!config.is_open()) {
- std::cout << "Error opening file";
- return;
- }
- config >> N;
- config >> WIDTH_WINDOW >> HEIGHT_WINDOW;
- int R, G, B;
- config >> R >> G >> B;
- colorBackground = RGB(R, G, B);
- config >> R >> G >> B;
- lineColor = RGB(R, G, B);
- config.close();
- }
- void RunNotepad(void)
- {
- STARTUPINFO sInfo;
- PROCESS_INFORMATION pInfo;
- ZeroMemory(&sInfo, sizeof(STARTUPINFO));
- CreateProcess(_T("C:\\Windows\\Notepad.exe"),
- NULL, NULL, NULL, FALSE, 0, NULL, NULL, &sInfo, &pInfo);
- }
- int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
- MSG lpMsg;
- loadConfig();
- LPWSTR* szArglist;
- int nArgs;
- szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
- if (nArgs > 1)
- N = _wtoi(szArglist[1]);
- LocalFree(szArglist);
- HANDLE File_Map = CreateFileMapping(
- INVALID_HANDLE_VALUE,
- NULL,
- PAGE_READWRITE,
- 0,
- sizeof(int) * N * N,
- L"FileMapping"
- );
- mas = (int*)MapViewOfFile(
- File_Map,
- FILE_MAP_ALL_ACCESS,
- 0,
- 0,
- sizeof(int) * N * N
- );
- w.lpszClassName = L"MyAppClass"; //имя программы
- w.hInstance = hInstance; //идентификатор текущего приложения
- w.lpfnWndProc = WndProc; //указатель на функцию окна
- w.hCursor = LoadCursor(NULL, IDC_ARROW); //загружаем курсор
- w.hIcon = 0;
- w.lpszMenuName = 0;
- hBrush = CreateSolidBrush(lineColor);
- w.hbrBackground = hBrush;
- w.style = CS_HREDRAW | CS_VREDRAW;
- w.cbClsExtra = 0;
- w.cbWndExtra = 0;
- //Если не удалось зарегистрировать класс окна - выходим
- if (!RegisterClass(&w))
- return 0;
- //Создадим окно в памяти, заполнив аргументы CreateWindow
- hWnd = CreateWindow(L"MyAppClass", //Имя программы
- L"Грфические возможности Win32 API", //Заголовок окна
- WS_OVERLAPPEDWINDOW, //Стиль окна - перекрывающееся
- CW_USEDEFAULT, //положение окна на экране по х
- CW_USEDEFAULT, //положение по у
- WIDTH_WINDOW, //ширина
- HEIGHT_WINDOW,
- HWND_DESKTOP, //идентификатор родительского окна
- (HMENU)NULL, //идентификатор меню
- (HINSTANCE)hInstance, //идентификатор экземпляра программы
- (HINSTANCE)NULL); //отсутствие дополнительных параметров
- //Выводим окно из памяти на экран
- ShowWindow(hWnd, nCmdShow);
- //Обновим содержимое окна
- UpdateWindow(hWnd);
- //Цикл обработки сообщений
- while (GetMessage(&lpMsg, NULL, 0, 0)) {
- TranslateMessage(&lpMsg);
- DispatchMessage(&lpMsg);
- }
- return(lpMsg.wParam);
- DestroyWindow(hWnd);
- UnregisterClass(L"MyAppClass", hInstance);
- UnmapViewOfFile(mas);
- CloseHandle(File_Map);
- UnregisterHotKey(hWnd, 1);
- UnregisterHotKey(hWnd, 2);
- DeleteObject(hBrush);
- return 0;
- }
- //Функция окна
- LRESULT CALLBACK WndProc(HWND hWnd, UINT messg, WPARAM wParam, LPARAM lParam) {
- HDC hdc; //создаём контекст устройства
- PAINTSTRUCT ps; //создаём экземпляр структуры графического вывода
- (RegisterHotKey(
- hWnd,
- 1,
- MOD_CONTROL,
- 0x51));
- (RegisterHotKey(
- hWnd,
- 2,
- MOD_SHIFT,
- 0x43));
- setIndent();
- int x = 0, y = 0;
- if (messg == MyMSG_O)
- {
- SendMessage(hWnd, WM_PAINT, ' ', 1L);
- }
- //Цикл обработки сообщений
- switch (messg) {
- case WM_LBUTTONDOWN:
- x = LOWORD(lParam);//координаты с которыми нажата мышь - аргументы WndProc
- y = HIWORD(lParam);
- x /= size;
- y /= size;
- if (mas[x * N + y] != idСircleCell)
- {
- mas[x * N + y] = idСircleCell;
- PostMessage(HWND_BROADCAST, MyMSG_O, x,
- y);
- }
- break;
- case WM_RBUTTONDOWN:
- x = LOWORD(lParam);//координаты с которыми нажата мышь - аргументы WndProc
- y = HIWORD(lParam);
- x /= size;
- y /= size;
- if (mas[x * N + y] != idСrossCell)
- {
- mas[x * N + y] = idСrossCell;
- SendMessage(HWND_BROADCAST, MyMSG_O,
- x,
- y);
- }
- break;
- //сообщение рисования
- case WM_PAINT:
- drawField();
- break;
- case WM_KEYDOWN:
- if (wParam == VK_RETURN) {
- PAINTSTRUCT ps;
- colorBackground = RGB(rand() % 255, rand() % 255, rand() % 255);
- HBRUSH hBrush2 = hBrush;
- hBrush = CreateSolidBrush(colorBackground);
- SetClassLongPtr(hWnd, GCL_HBRBACKGROUND, (LONG)hBrush);
- InvalidateRect(hWnd, NULL, TRUE);
- DeleteObject(hBrush2);
- return 0;
- }
- if (wParam == VK_ESCAPE) {
- DestroyWindow(hWnd);
- }
- break;
- case WM_MOUSEWHEEL:
- HLS();
- break;
- case WM_HOTKEY:
- if (wParam == 1)
- {
- DestroyWindow(hWnd);
- }
- if (wParam == 2) {
- RunNotepad();
- }
- break;
- case WM_DESTROY:
- saveConfig();
- PostQuitMessage(0); /* send a WM_QUIT to the message queue */
- break;
- default:
- return(DefWindowProc(hWnd, messg, wParam, lParam)); //освобождаем очередь приложения от нераспознаных
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement