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 = 5;
- 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;
- #define STACK_SIZE (64*1024)
- HANDLE paint;
- LPCWSTR WM_COORDINATE_O = L"WM_COORDINATE_O";
- UINT MyMSG_O = RegisterWindowMessage(WM_COORDINATE_O);
- LPCWSTR WM_YOU_LOSE_MSG = L"WM_YOU_LOSE_MSG_CROSS_AND_CIRCLE";
- UINT YOU_LOSE_MSG = RegisterWindowMessage(WM_YOU_LOSE_MSG);
- DWORD dwSuspendCount = 1;
- HANDLE pnt;
- HANDLE wndw;
- int MY_SYMBOL;
- 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 drawField();
- DWORD WINAPI HLS(LPVOID) {
- while (true) {
- WaitForSingleObject(pnt, INFINITE);
- int R = (int(GetRValue(colorBackground)));
- int G = (int(GetGValue(colorBackground)));
- int B = (int(GetBValue(colorBackground)));
- 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);
- colorBackground = ColorHLSToRGB(Hue + 10, Luminance, Saturation);
- drawField();
- Sleep(100);
- PostMessage(HWND_BROADCAST, MyMSG_O, 1,
- 1);
- }
- return 0;
- }
- 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 check_map() {
- for (int i = 0; i < N; ++i) {
- int count_cross = 0;
- int count_circl = 0;
- for (int j = 0; j < N; ++j) {
- switch (mas[i * N + j]){
- case idСrossCell:
- count_cross += 1;
- break;
- case idСircleCell:
- count_circl += 1;
- break;
- default:
- break;
- }
- }
- if (count_cross == N) {
- return idСrossCell;
- }
- if (count_circl == N) {
- return idСircleCell;
- }
- }
- for (int j = 0; j < N; ++j) {
- int count_cross = 0;
- int count_circl = 0;
- for (int i = 0; i < N; ++i) {
- switch (mas[i * N + j]) {
- case idСrossCell:
- count_cross += 1;
- break;
- case idСircleCell:
- count_circl += 1;
- break;
- default:
- break;
- }
- }
- if (count_cross == N) {
- return idСrossCell;
- }
- if (count_circl == N) {
- return idСircleCell;
- }
- }
- return 0;
- }
- int CALLBACK WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR pCmdLine, int nCmdShow) {
- MSG lpMsg;
- loadConfig();
- LPWSTR* szArglist;
- int nArgs;
- N = 5;
- szArglist = CommandLineToArgvW(GetCommandLineW(), &nArgs);
- if (nArgs > 1)
- N = _wtoi(szArglist[1]);
- LocalFree(szArglist);
- pnt = CreateSemaphore(NULL, 1, 1, NULL);
- wndw = CreateSemaphore(NULL, 2, 2, L"MyAppClass");
- paint = CreateThread(NULL, STACK_SIZE, HLS, NULL, 0, NULL);
- HANDLE File_Map = CreateFileMapping(
- INVALID_HANDLE_VALUE,
- NULL,
- PAGE_READWRITE,
- 0,
- sizeof(int) * N * N + 2* sizeof(int),
- L"FileMapping"
- );
- mas = (int*)MapViewOfFile(
- File_Map,
- FILE_MAP_ALL_ACCESS,
- 0,
- 0,
- sizeof(int) * N * N + 2 * sizeof(int)
- );
- if (WaitForSingleObject(wndw, 0) == WAIT_TIMEOUT) {
- MessageBox(NULL, _T("Два окна уже запущено"), _T("Ошибка"),
- MB_OK | MB_SETFOREGROUND);
- ExitProcess(0);
- }
- if (mas[N * N] == 1) {
- MY_SYMBOL = idСircleCell;
- }
- else {
- mas[N * N] = 1;
- mas[N * N + 1] = idСircleCell;
- MY_SYMBOL = idСrossCell;
- }
- 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);
- CloseHandle(paint);
- CloseHandle(pnt);
- CloseHandle(wndw);
- 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);
- }
- if (messg == YOU_LOSE_MSG)
- {
- if (wParam == idСircleCell) {
- MessageBox(NULL, _T("Выиграли нолики!"), _T("Конец игры"),
- MB_OK | MB_SETFOREGROUND);
- }
- else {
- MessageBox(NULL, _T("Выйграли крестики!"), _T("Конец игры"),
- MB_OK | MB_SETFOREGROUND);
- }
- ExitProcess(0);
- }
- //Цикл обработки сообщений
- 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СrossCell ) {
- break;
- }
- if (mas[N * N + 1] != MY_SYMBOL)
- {
- mas[x * N + y] = MY_SYMBOL;
- mas[N * N + 1] = MY_SYMBOL;
- if (check_map() == MY_SYMBOL) {
- PostMessage(HWND_BROADCAST, YOU_LOSE_MSG, MY_SYMBOL,
- MY_SYMBOL);
- // MessageBox(NULL, _T("Вы выйграли!"), _T("Конец игры"),
- // MB_OK | MB_SETFOREGROUND);
- // ExitProcess(0);
- }
- }
- else {
- MessageBox(NULL, _T("Не ваш ход!"), _T("Ошибка"),
- MB_OK | MB_SETFOREGROUND);
- }
- break;
- //сообщение рисования
- case WM_PAINT:
- ReleaseSemaphore(pnt, 1, NULL);
- break;
- case WM_KEYUP:
- 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);
- }
- if (wParam == VK_SPACE) {
- if (dwSuspendCount == 0)
- dwSuspendCount = ResumeThread(paint);
- else dwSuspendCount = SuspendThread(paint);
- }
- if (wParam == VK_NUMPAD1) {
- SetThreadPriority(paint, THREAD_PRIORITY_IDLE);
- return 0;
- }
- if (wParam == VK_NUMPAD2) {
- SetThreadPriority(paint, THREAD_PRIORITY_LOWEST);
- return 0;
- }
- if (wParam == VK_NUMPAD3) {
- SetThreadPriority(paint, THREAD_PRIORITY_BELOW_NORMAL);
- return 0;
- }
- if (wParam == VK_NUMPAD4) {
- SetThreadPriority(paint, THREAD_PRIORITY_NORMAL);
- return 0;
- }
- if (wParam == VK_NUMPAD5) {
- SetThreadPriority(paint, THREAD_PRIORITY_ABOVE_NORMAL);
- return 0;
- }
- if (wParam == VK_NUMPAD6) {
- SetThreadPriority(paint, THREAD_PRIORITY_HIGHEST);
- return 0;
- }
- if (wParam == VK_NUMPAD7) {
- SetThreadPriority(paint, THREAD_PRIORITY_TIME_CRITICAL);
- return 0;
- }
- 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 */
- ReleaseSemaphore(wndw, 1, NULL);
- break;
- default:
- return(DefWindowProc(hWnd, messg, wParam, lParam)); //освобождаем очередь приложения от нераспознаных
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement