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 <fstream>
- LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
- WNDCLASS w;
- int WIDTH_WINDOW = 320;
- int HEIGHT_WINDOW = 240;
- INT N =10;
- int deltaLineColor[3] = { 10,10,10 };
- COLORREF lineColor = RGB(0, 255, 0);
- COLORREF colorBackground = RGB(0, 0, 255);
- COLORREF colorCircle = RGB(255, 0, 0);
- int grid[105][105];
- const int idVoidCell = 0;
- const int idСrossCell = 1;
- const int idСircleCell = 2;
- int INDENT = 30;
- HBRUSH hBrush;
- HWND hWnd;
- HDC hdc;
- int* koord = NULL;
- 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);
- //std::cout << rect.bottom - rect.top << std::endl;
- //std::cout << rect.right - rect.left << std::endl;
- INDENT = int ( min(0.85*(rect.bottom - rect.top)/ N, 0.85 * (rect.right - rect.left) / N));
- }
- void drawCross( int x = 0, int y = 0) {
- hdc = GetDC(hWnd);
- HPEN hPen;
- hPen = CreatePen(1, 4, RGB(5, 255, 0));
- SelectObject(hdc, hPen);
- MoveToEx(hdc, x * INDENT, y * INDENT, NULL);
- LineTo(hdc, (x * INDENT) + INDENT, (y * INDENT) + INDENT);
- MoveToEx(hdc, x * INDENT + INDENT, y * INDENT, NULL);
- LineTo(hdc, x * INDENT, y * INDENT + INDENT);
- DeleteObject(hPen);
- ReleaseDC(hWnd, hdc);
- }
- void drawCircle( int x = 0, int y = 0) {
- hdc = GetDC(hWnd);
- HBRUSH hBrush2 = hBrush;
- hBrush = CreateSolidBrush(colorCircle); //задаём сплошную кисть, закрашенную цветом RGB
- SelectObject(hdc, hBrush); //делаем кисть активной
- Ellipse(hdc, x * INDENT, y * INDENT, x * INDENT + INDENT, y * INDENT + INDENT);
- DeleteObject(hBrush2);
- ReleaseDC(hWnd, hdc);
- }
- 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 * INDENT, NULL);
- SelectObject(hdc, hPen);
- LineTo(hdc, N * INDENT, i * INDENT);
- MoveToEx(hdc, i * INDENT, 0, NULL);
- SelectObject(hdc, hPen);
- LineTo(hdc, i * INDENT, N * INDENT);
- }
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- switch (koord[i * N + j])
- {
- case idСrossCell:
- drawCross(i, j);
- break;
- case idСircleCell:
- drawCircle(i, j);
- break;
- default:
- break;
- }
- DeleteObject(hBrush2);
- EndPaint(hWnd, &ps);
- DeleteObject(hPen);
- }
- 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;
- std::cout << 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 main(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpszCmdLine, int nCmdShow) {
- loadConfig();
- MSG lpMsg;
- //получение данных из аргументов
- LPWSTR* szArglist;
- int nArgs;
- szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
- if (nArgs > 1)
- N = _wtoi(szArglist[1]);
- LocalFree(szArglist);
- if (N > 100)N = 100;
- for (int i = 0; i < N; ++i)
- for (int j = 0; j < N; ++j)
- grid[i][j]= idVoidCell;
- HANDLE File_Map = CreateFileMapping(
- INVALID_HANDLE_VALUE,
- NULL,
- PAGE_READWRITE,
- 0,
- sizeof(int) * N * N,
- L"FileMapping123456"
- );
- koord = (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;
- std::cout << int(GetRValue(colorBackground)) << " " << int(GetGValue(colorBackground)) << " " << int(GetBValue(colorBackground)) << std::endl;
- hBrush = CreateSolidBrush(colorBackground);
- 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);
- UnmapViewOfFile(koord);
- CloseHandle(File_Map);
- DestroyWindow(hWnd);
- UnregisterClass(L"MyAppClass", hInstance);
- 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 /= INDENT;
- y /= INDENT;
- /*if (x <= N && y <= N && idСircleCell != grid[x][y]) {
- grid[x][y] = idСircleCell;
- SendMessage(hWnd, WM_PAINT, ' ', 1L);
- }*/
- if (koord[x * N + y] != idСircleCell)
- {
- koord[x * N + y] = idСircleCell;
- PostMessage(
- HWND_TOPMOST,
- MyMSG_O,
- x,
- y
- );
- }
- break;
- case WM_RBUTTONDOWN:
- x = LOWORD(lParam);//координаты с которыми нажата мышь - аргументы WndProc
- y = HIWORD(lParam);
- x /= INDENT;
- y /= INDENT;
- /*
- if (x <= N && y <= N && idСrossCell != grid[x][y] ) {
- grid[x][y] = idСrossCell;
- SendMessage(hWnd, WM_PAINT, ' ', 1L);
- }*/
- if (koord[x * N + y] != idСrossCell)
- {
- koord[x * N + y] = idСrossCell;
- SendMessage(
- HWND_TOPMOST,
- MyMSG_O,
- x,
- y
- );
- }
- break;
- //сообщение рисования
- case WM_PAINT:
- drawField();
- break;
- case WM_MOUSEWHEEL:
- int p;
- switch (rand() % 3) {
- case 0:
- p = int(GetRValue(lineColor)) + deltaLineColor[0];
- if (p >= 255 || p <0) deltaLineColor[0]*=-1;
- lineColor = RGB(int(GetRValue(lineColor))+ deltaLineColor[0] , int(GetGValue(lineColor)), int(GetBValue(lineColor)));
- break;
- case 1:
- p = int(GetGValue(lineColor)) + deltaLineColor[1];
- if (p >= 255 || p < 0) deltaLineColor[1] *= -1;
- lineColor = RGB(int(GetRValue(lineColor)) , int(GetGValue(lineColor))+ deltaLineColor[1], int(GetBValue(lineColor)));
- break;
- case 2:
- p = int(GetBValue(lineColor)) + deltaLineColor[2];
- if (p >= 255 || p < 0) deltaLineColor[2] *= -1;
- lineColor = RGB(int(GetRValue(lineColor)), int(GetGValue(lineColor)) , int(GetBValue(lineColor))+ deltaLineColor[2]);
- break;
- }
- SendMessage(hWnd, WM_PAINT, ' ', 1L);
- break;
- case WM_KEYDOWN:
- if (wParam == VK_RETURN) {
- PAINTSTRUCT ps;
- HBRUSH hBrush2 = hBrush ;
- colorBackground = RGB( rand() % 255 , rand() % 255, rand() % 255);
- 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_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