Skip to content

Instantly share code, notes, and snippets.

@Devhobby
Created December 7, 2018 17:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Devhobby/6c627c2de196b4c3c7237481e39ae91b to your computer and use it in GitHub Desktop.
Save Devhobby/6c627c2de196b4c3c7237481e39ae91b to your computer and use it in GitHub Desktop.
ox
// Okienka pierwsze starcie.cpp : definiuje punkt wejścia dla aplikacji
//
#include "stdafx.h"
#include "Okienka pierwsze starcie.h"
#include <windowsx.h>
#define MAX_LOADSTRING 100
// Zmienne globalne:
HINSTANCE hInst; // bieżące wystąpienie
WCHAR szTitle[MAX_LOADSTRING]; // Tekst paska tytułu
WCHAR szWindowClass[MAX_LOADSTRING]; // nazwa klasy okna głównego
const int CELL_SIZE = 100; // Wielkość pola gry
HBRUSH hbr1, hbr2;
int playerTurn = 1;
// Przekaż dalej deklaracje funkcji dołączonych w tym module kodu:
ATOM MyRegisterClass(HINSTANCE hInstance);
BOOL InitInstance(HINSTANCE, int);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK About(HWND, UINT, WPARAM, LPARAM);
int APIENTRY wWinMain(_In_ HINSTANCE hInstance,
_In_opt_ HINSTANCE hPrevInstance,
_In_ LPWSTR lpCmdLine,
_In_ int nCmdShow)
{
UNREFERENCED_PARAMETER(hPrevInstance);
UNREFERENCED_PARAMETER(lpCmdLine);
// TODO: W tym miejscu umieść kod.
// Zainicjuj ciągi globalne
LoadStringW(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
LoadStringW(hInstance, IDC_OKIENKAPIERWSZESTARCIE, szWindowClass, MAX_LOADSTRING);
MyRegisterClass(hInstance);
// Wykonaj inicjacje aplikacji:
if (!InitInstance(hInstance, nCmdShow))
{
return FALSE;
}
HACCEL hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_OKIENKAPIERWSZESTARCIE));
MSG msg;
// Główna pętla wiadomości:
while (GetMessage(&msg, nullptr, 0, 0))
{
if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return (int)msg.wParam;
}
//
// FUNKCJA: MyRegisterClass()
//
// CEL: Rejestruje klasę okna.
//
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_OKIENKAPIERWSZESTARCIE));
wcex.hCursor = LoadCursor(nullptr, IDC_ARROW);
//wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wcex.hbrBackground = static_cast<HBRUSH>(GetStockObject(GRAY_BRUSH)); //ustawianie koloru tła
wcex.lpszMenuName = MAKEINTRESOURCEW(IDC_OKIENKAPIERWSZESTARCIE);
wcex.lpszClassName = szWindowClass;
wcex.hIconSm = LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));
return RegisterClassExW(&wcex);
}
//
// FUNKCJA: InitInstance(HINSTANCE, int)
//
// CEL: Zapisuje dojście wystąpienia i tworzy okno główne
//
// KOMENTARZE:
//
// W tej funkcji dojście wystąpienia jest zapisywane w zmiennej globalnej i
// jest tworzone i wyświetlane okno główne programu.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
hInst = hInstance; // Przechowuj dojście wystąpienia w zmiennej globalnej
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;
}
//
// FUNKCJA: WndProc(HWND, UINT, WPARAM, LPARAM)
//
// CEL: Przetwarza wiadomości dla okna głównego.
//
// WM_COMMAND — przetwarzaj menu aplikacji
// WM_PAINT — pomaluj okno główne
// WM_DESTROY — opublikuj komunikat o rezygnacji i wróć
//
//
BOOL GetGameBoardRect(HWND hwnd, RECT* pRect)
{
RECT rc; //rysujemy kwadrat w samym środku (bez względu na wielkość okna)
if (GetClientRect(hwnd, &rc))
{
int width = rc.right - rc.left; //środek szerokości
int height = rc.bottom - rc.top; //środek wysokości
pRect->left = (width - CELL_SIZE * 3) / 2; //przeliczamy boki ramki w stosunku do wielkości okna
pRect->top = (height - CELL_SIZE * 3) / 2;
pRect->right = pRect->left + CELL_SIZE * 3;
pRect->bottom = pRect->top + CELL_SIZE * 3;
return TRUE;
}
SetRectEmpty(pRect);
return FALSE;
}
void DrawLine(HDC hdc, int x1, int y1, int x2, int y2) //rysowanie linii
{
MoveToEx(hdc, x1, y1, NULL);
LineTo(hdc, x2, y2);
}
int GetCellNumberFromPoint(HWND hWnd, int x, int y)
{
POINT pt{ x, y };
RECT rc;
if (GetGameBoardRect(hWnd, &rc))
{
if (PtInRect(&rc, pt))
{
// kliknięcie na obszarze tablicy
x = pt.x - rc.left;
y = pt.y - rc.top;
int column = x / CELL_SIZE;
int row = y / CELL_SIZE;
//Przeliczamy do indexu
return column + row * 3;
}
}
return -1; //poza tablicą gry
}
BOOL GetCellRect(HWND hWnd, int index, RECT * pRect)
{
RECT rcBoard;
SetRectEmpty(pRect);
if (index < 0 || index > 8) return FALSE;
if (GetGameBoardRect(hWnd, &rcBoard))
{
//zmiana indexu z 0-8 na x/y
int y = index / 3; //numer rzędu
int x = index % 3; //numer kolumny
pRect -> left = rcBoard.left + x * CELL_SIZE +1;
pRect->top = rcBoard.top + y * CELL_SIZE +1;
pRect->right = pRect->left + CELL_SIZE -1;
pRect->bottom = pRect->top + CELL_SIZE -1;
return TRUE;
}
return FALSE;
}
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
switch (message)
{
case WM_CREATE:
{
hbr1 = CreateSolidBrush(RGB(255,0,0));
hbr2 = CreateSolidBrush(RGB(0,0,255));
}
break;
case WM_COMMAND:
{
int wmId = LOWORD(wParam);
// Analizuj zaznaczenia menu:
switch (wmId)
{
case IDM_ABOUT:
DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
break;
case IDM_EXIT:
DestroyWindow(hWnd);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
}
break;
case WM_LBUTTONDOWN:
{
int xPos = GET_X_LPARAM(lParam); //odczyt współrzędnych kliknięcia myszką.
int yPos = GET_Y_LPARAM(lParam);
int index = GetCellNumberFromPoint(hWnd, xPos, yPos);
HDC hdc = GetDC(hWnd);
if (NULL != hdc)
{
WCHAR temp[100];
//wsprintf(temp, L"Index = %d", index);
//TextOut(hdc, xPos, yPos, temp, lstrlen(temp));
//pobieranie rozmiaru pola z indeksu
if(index != -1)
{
RECT rcCell;
if (GetCellRect(hWnd, index, &rcCell))
{
FillRect(hdc, &rcCell,(playerTurn ==2) ? hbr2 : hbr1 );
}
playerTurn = (playerTurn == 1) ? 2 : 1;
}
ReleaseDC(hWnd, hdc);
}
}
break;
case WM_GETMINMAXINFO: //określanie wielkości okna (min/max)
{
MINMAXINFO * ptMinMax = (MINMAXINFO*)lParam;
//ptMinMax->ptMaxTrackSize.x = CELL_SIZE * 5; //wielkość max
//ptMinMax->ptMaxTrackSize.y = CELL_SIZE * 5;
ptMinMax->ptMinTrackSize.x = CELL_SIZE * 5; //wielkość min
ptMinMax->ptMinTrackSize.y = CELL_SIZE * 5;
}
break;
case WM_PAINT:
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hWnd, &ps);
// TODO: tutaj dodaj kod rysowania używający elementu hdc...
RECT rc; //rysujemy kwadrat w samym środku (bez względu na wielkość okna)
if (GetGameBoardRect(hWnd, &rc))
{
FillRect(hdc, &rc, (HBRUSH)GetStockObject(WHITE_BRUSH)); //wypełnienie na biało pola gry
//Rectangle(hdc, rc.left, rc.top, rc.right, rc.bottom); //rysowanie właściwe
}
for (int i = 0; i < 4; i++)
{
//rysowanie linii pionowych
DrawLine(hdc, rc.left + CELL_SIZE * i, rc.top, rc.left + CELL_SIZE * i, rc.bottom);
//rysowanie linii poziomych
DrawLine(hdc, rc.left, rc.top + CELL_SIZE * i, rc.right, rc.top + CELL_SIZE * i);
}
EndPaint(hWnd, &ps);
}
break;
case WM_DESTROY:
DeleteObject(hbr1);
DeleteObject(hbr2);
PostQuitMessage(0);
break;
default:
return DefWindowProc(hWnd, message, wParam, lParam);
}
return 0;
}
//Procedura obsługi wiadomości dla okna informacji o programie.
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;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment