Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <windows.h>
- #include <tchar.h>
- #include <stdio.h>
- #include <windowsx.h>
- #include <fstream>
- #include <iostream>
- #include <stdlib.h>
- using namespace std;
- #define UNUSED(expr) (void)(expr)
- //simply loading bitmap picture
- LRESULT CALLBACK WindProc(HWND, UINT, UINT, LONG);
- const TCHAR szWinClass[] = _T("Win32SampleApp");
- const TCHAR szWinName[] = _T("Win32SampleWindow");
- const char path[] = "C:\\Users\\я\\source\\repos\\TASUKETE\\cross.bmp";
- const int N = 3;
- int i = 0;
- int board[N][N];
- RECT rect;
- HPEN redPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
- struct Image
- {
- HBITMAP hBitmap;
- LONG width, height;
- };
- Image img;
- static HDC hdcMemSurface;
- static struct Image loadImage(LPCTSTR path, HINSTANCE hInstance)
- {
- struct Image image;
- image.hBitmap = (HBITMAP)LoadImage(
- hInstance,
- "C:\\Users\\я\\source\\repos\\TASUKETE\\newCross.png",
- IMAGE_BITMAP,
- 0, 0,
- LR_LOADFROMFILE | LR_CREATEDIBSECTION
- );
- BITMAP bitmapInfo;
- GetObject(image.hBitmap, sizeof(bitmapInfo), &bitmapInfo);
- image.width = bitmapInfo.bmWidth;
- image.height = bitmapInfo.bmHeight;
- return image;
- }
- BOOL OnClicked(HWND hwnd, LPARAM lParam)
- {
- HDC hdc, hmdc;
- PAINTSTRUCT ps;
- HANDLE hOldBitmap, hBitmap;
- BITMAP Bitmap;
- hBitmap = LoadImage(NULL, L"C:\\Users\\я\\source\\repos\\TASUKETE\\newCross.png", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- if (!hBitmap) {
- MessageBox(NULL, "File not found!", "Error", MB_OK);
- return FALSE;
- }
- GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
- int xSize = rect.right / N;
- int ySize = rect.bottom / N;
- int xPos = GET_X_LPARAM(lParam)/xSize;
- int yPos = GET_Y_LPARAM(lParam)/ySize;
- //координаты клетки
- int xTop = xPos * xSize;
- int yTop = yPos * ySize;
- int xBottom = xTop + xSize;
- int yBottom = yTop + ySize;
- hdc = GetDC(hwnd);
- hmdc = CreateCompatibleDC(hdc);
- hOldBitmap = SelectObject(hmdc, hBitmap);
- GetClientRect(hwnd, &rect);
- //copy bitmap from compatible context to main context with scaling
- StretchBlt(hdc, xTop+1, yTop+1, xSize-1, ySize-1, hmdc, 0, 0, Bitmap.bmWidth,
- Bitmap.bmHeight, SRCCOPY);
- //select old bitmap as current
- SelectObject(hmdc, hOldBitmap);
- //delete loaded bitmap
- DeleteObject(hBitmap);
- //delete compatible context
- DeleteDC(hmdc);
- //release context and end painting*/
- EndPaint(hwnd, &ps);
- ReleaseDC(hwnd, hmdc);
- ReleaseDC(hwnd, hdc);
- board[xPos][yPos] = 1;
- return TRUE;
- }
- VOID Redraw(HWND hwnd)
- {
- HDC hdc, hmdc;
- PAINTSTRUCT ps;
- BITMAP Bitmap;
- HANDLE hBitmap, hOldBitmap;
- int xSize = rect.right / N;
- int ySize = rect.bottom / N;
- for (int i = 0; i < N; i++)
- {
- for (int j = 0; j < N;j++)
- {
- if (board[i][j] == 1)
- {
- hBitmap = LoadImage(NULL, "C:\\Users\\я\\source\\repos\\TASUKETE\\newCross.png", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
- if (!hBitmap) {
- MessageBox(NULL, "File not found!", "Error", MB_OK);
- return;
- }
- GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
- hdc = GetDC(hwnd);
- hmdc = CreateCompatibleDC(hdc);
- hOldBitmap = SelectObject(hmdc, hBitmap);
- GetClientRect(hwnd, &rect);
- //copy bitmap from compatible context to main context with scaling
- StretchBlt(hdc, i*xSize+1, j*ySize+1, xSize - 1, ySize - 1, hmdc, 0, 0, Bitmap.bmWidth,
- Bitmap.bmHeight, SRCCOPY);
- //select old bitmap as current
- SelectObject(hmdc, hOldBitmap);
- //delete loaded bitmap
- DeleteObject(hBitmap);
- //delete compatible context
- DeleteDC(hmdc);
- //release context and end painting*/
- EndPaint(hwnd, &ps);
- ReleaseDC(hwnd, hmdc);
- ReleaseDC(hwnd, hdc);
- }
- }
- }
- }
- static LRESULT CALLBACK WindProc
- (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
- {
- GetClientRect(hWnd, &rect);
- switch (uMsg)
- {
- case WM_CREATE:
- {
- const HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
- img = loadImage(TEXT("1.bmp"), hInstance);
- const HDC hdcWin = GetDC(hWnd);
- hdcMemSurface = CreateCompatibleDC(hdcWin);
- ReleaseDC(hWnd, hdcWin);
- return 0;
- }
- case WM_PAINT:
- {
- PAINTSTRUCT ps;
- HDC hdc = GetDC(hWnd);
- int xCur = 0, yCur = 0;//левый верхний угол
- hdc = BeginPaint(hWnd, &ps);
- SelectObject(hdc, redPen);
- for (int i = 0; i < N - 1; i++)
- {
- MoveToEx(hdc, xCur + rect.right / N, 0, NULL);//обновляет текущую позицию точки
- LineTo(hdc, xCur + rect.right / N, rect.bottom);//рисует горизонтальную линию
- MoveToEx(hdc, 0, yCur + rect.bottom / N, NULL);
- LineTo(hdc, rect.right, yCur + rect.bottom / N);//вертикальная линия
- xCur += rect.right / N;
- yCur += rect.bottom / N;
- }
- ReleaseDC(hWnd, hdc);
- EndPaint(hWnd, &ps);
- Redraw(hWnd);
- return 0;
- }
- case WM_LBUTTONDOWN:
- {
- if (OnClicked(hWnd, lParam))
- return 0;
- else cout << "blyat";
- break;
- }
- case WM_SIZE:
- {
- InvalidateRect(hWnd, NULL, TRUE);
- return 0;
- }
- case WM_DESTROY:
- {
- PostQuitMessage(0);
- return 0;
- }
- default:
- return DefWindowProc(hWnd, uMsg, wParam, lParam);
- }
- }
- int main(int argc, char** argv) {
- HWND hWnd;
- WNDCLASS wincl = { 0 };
- MSG message;
- int nCmdShow = SW_SHOW;
- HINSTANCE hThisInstance = GetModuleHandle(NULL);
- wincl.style = CS_HREDRAW | CS_VREDRAW;
- wincl.lpfnWndProc = WindProc;
- wincl.hInstance = hThisInstance;
- wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
- wincl.lpszClassName = szWinClass;
- if (!RegisterClass(&wincl)) {
- MessageBox(NULL, "Can't register class!", "ERROR!", MB_OK);
- return 0;
- }
- hWnd = CreateWindowEx
- (
- 0,
- szWinClass, szWinName,
- WS_OVERLAPPEDWINDOW,
- CW_USEDEFAULT,
- CW_USEDEFAULT,
- 320, 240,
- HWND_DESKTOP, NULL,
- hThisInstance,
- NULL);
- if (!hWnd) {
- MessageBox(NULL, "Can't create window!", "ERROR!", MB_OK);
- return 0;
- }
- ShowWindow(hWnd, nCmdShow);
- UpdateWindow(hWnd);
- //process message
- while (GetMessage(&message, NULL, 0, 0)) {
- TranslateMessage(&message);
- DispatchMessage(&message);
- }
- DeleteObject(redPen);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement