Advertisement
plasticbag

cross board

Dec 12th, 2019
435
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 6.03 KB | None | 0 0
  1. #include <windows.h>
  2. #include <tchar.h>
  3. #include <stdio.h>
  4. #include <windowsx.h>
  5. #include <fstream>
  6. #include <iostream>
  7. #include <stdlib.h>
  8. using namespace std;
  9. #define UNUSED(expr) (void)(expr)
  10.  
  11. //simply loading bitmap picture
  12.  
  13. LRESULT CALLBACK WindProc(HWND, UINT, UINT, LONG);
  14.  
  15. const TCHAR szWinClass[] = _T("Win32SampleApp");
  16. const TCHAR szWinName[] = _T("Win32SampleWindow");
  17. const char path[] = "C:\\Users\\я\\source\\repos\\TASUKETE\\cross.bmp";
  18. const int N = 3;
  19. int i = 0;
  20. int board[N][N];
  21.  
  22. RECT rect;
  23. HPEN redPen = CreatePen(PS_SOLID, 1, RGB(255, 0, 0));
  24.  
  25. struct Image
  26. {
  27. HBITMAP hBitmap;
  28. LONG width, height;
  29. };
  30.  
  31. Image img;
  32.  
  33. static HDC hdcMemSurface;
  34.  
  35.  
  36. static struct Image loadImage(LPCTSTR path, HINSTANCE hInstance)
  37. {
  38. struct Image image;
  39.  
  40. image.hBitmap = (HBITMAP)LoadImage(
  41. hInstance,
  42. "C:\\Users\\я\\source\\repos\\TASUKETE\\newCross.png",
  43. IMAGE_BITMAP,
  44. 0, 0,
  45. LR_LOADFROMFILE | LR_CREATEDIBSECTION
  46. );
  47.  
  48. BITMAP bitmapInfo;
  49. GetObject(image.hBitmap, sizeof(bitmapInfo), &bitmapInfo);
  50. image.width = bitmapInfo.bmWidth;
  51. image.height = bitmapInfo.bmHeight;
  52.  
  53. return image;
  54. }
  55. BOOL OnClicked(HWND hwnd, LPARAM lParam)
  56. {
  57. HDC hdc, hmdc;
  58. PAINTSTRUCT ps;
  59. HANDLE hOldBitmap, hBitmap;
  60. BITMAP Bitmap;
  61.  
  62. hBitmap = LoadImage(NULL, L"C:\\Users\\я\\source\\repos\\TASUKETE\\newCross.png", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  63. if (!hBitmap) {
  64. MessageBox(NULL, "File not found!", "Error", MB_OK);
  65. return FALSE;
  66. }
  67. GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
  68.  
  69. int xSize = rect.right / N;
  70. int ySize = rect.bottom / N;
  71. int xPos = GET_X_LPARAM(lParam)/xSize;
  72. int yPos = GET_Y_LPARAM(lParam)/ySize;
  73.  
  74. //координаты клетки
  75. int xTop = xPos * xSize;
  76. int yTop = yPos * ySize;
  77. int xBottom = xTop + xSize;
  78. int yBottom = yTop + ySize;
  79.  
  80. hdc = GetDC(hwnd);
  81. hmdc = CreateCompatibleDC(hdc);
  82. hOldBitmap = SelectObject(hmdc, hBitmap);
  83. GetClientRect(hwnd, &rect);
  84. //copy bitmap from compatible context to main context with scaling
  85. StretchBlt(hdc, xTop+1, yTop+1, xSize-1, ySize-1, hmdc, 0, 0, Bitmap.bmWidth,
  86. Bitmap.bmHeight, SRCCOPY);
  87. //select old bitmap as current
  88. SelectObject(hmdc, hOldBitmap);
  89. //delete loaded bitmap
  90. DeleteObject(hBitmap);
  91. //delete compatible context
  92. DeleteDC(hmdc);
  93. //release context and end painting*/
  94. EndPaint(hwnd, &ps);
  95. ReleaseDC(hwnd, hmdc);
  96. ReleaseDC(hwnd, hdc);
  97. board[xPos][yPos] = 1;
  98. return TRUE;
  99. }
  100.  
  101. VOID Redraw(HWND hwnd)
  102. {
  103. HDC hdc, hmdc;
  104. PAINTSTRUCT ps;
  105. BITMAP Bitmap;
  106. HANDLE hBitmap, hOldBitmap;
  107. int xSize = rect.right / N;
  108. int ySize = rect.bottom / N;
  109. for (int i = 0; i < N; i++)
  110. {
  111. for (int j = 0; j < N;j++)
  112. {
  113. if (board[i][j] == 1)
  114. {
  115. hBitmap = LoadImage(NULL, "C:\\Users\\я\\source\\repos\\TASUKETE\\newCross.png", IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE);
  116. if (!hBitmap) {
  117. MessageBox(NULL, "File not found!", "Error", MB_OK);
  118. return;
  119. }
  120. GetObject(hBitmap, sizeof(BITMAP), &Bitmap);
  121. hdc = GetDC(hwnd);
  122. hmdc = CreateCompatibleDC(hdc);
  123. hOldBitmap = SelectObject(hmdc, hBitmap);
  124. GetClientRect(hwnd, &rect);
  125. //copy bitmap from compatible context to main context with scaling
  126. StretchBlt(hdc, i*xSize+1, j*ySize+1, xSize - 1, ySize - 1, hmdc, 0, 0, Bitmap.bmWidth,
  127. Bitmap.bmHeight, SRCCOPY);
  128. //select old bitmap as current
  129. SelectObject(hmdc, hOldBitmap);
  130. //delete loaded bitmap
  131. DeleteObject(hBitmap);
  132. //delete compatible context
  133. DeleteDC(hmdc);
  134. //release context and end painting*/
  135. EndPaint(hwnd, &ps);
  136. ReleaseDC(hwnd, hmdc);
  137. ReleaseDC(hwnd, hdc);
  138. }
  139. }
  140. }
  141. }
  142.  
  143. static LRESULT CALLBACK WindProc
  144. (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  145. {
  146. GetClientRect(hWnd, &rect);
  147. switch (uMsg)
  148. {
  149. case WM_CREATE:
  150. {
  151. const HINSTANCE hInstance = (HINSTANCE)GetWindowLong(hWnd, GWL_HINSTANCE);
  152.  
  153. img = loadImage(TEXT("1.bmp"), hInstance);
  154.  
  155. const HDC hdcWin = GetDC(hWnd);
  156. hdcMemSurface = CreateCompatibleDC(hdcWin);
  157. ReleaseDC(hWnd, hdcWin);
  158.  
  159. return 0;
  160. }
  161.  
  162. case WM_PAINT:
  163. {
  164. PAINTSTRUCT ps;
  165. HDC hdc = GetDC(hWnd);
  166.  
  167. int xCur = 0, yCur = 0;//левый верхний угол
  168. hdc = BeginPaint(hWnd, &ps);
  169. SelectObject(hdc, redPen);
  170. for (int i = 0; i < N - 1; i++)
  171. {
  172. MoveToEx(hdc, xCur + rect.right / N, 0, NULL);//обновляет текущую позицию точки
  173. LineTo(hdc, xCur + rect.right / N, rect.bottom);//рисует горизонтальную линию
  174. MoveToEx(hdc, 0, yCur + rect.bottom / N, NULL);
  175. LineTo(hdc, rect.right, yCur + rect.bottom / N);//вертикальная линия
  176. xCur += rect.right / N;
  177. yCur += rect.bottom / N;
  178. }
  179. ReleaseDC(hWnd, hdc);
  180. EndPaint(hWnd, &ps);
  181. Redraw(hWnd);
  182. return 0;
  183. }
  184.  
  185. case WM_LBUTTONDOWN:
  186. {
  187. if (OnClicked(hWnd, lParam))
  188. return 0;
  189. else cout << "blyat";
  190. break;
  191. }
  192.  
  193. case WM_SIZE:
  194. {
  195. InvalidateRect(hWnd, NULL, TRUE);
  196. return 0;
  197. }
  198. case WM_DESTROY:
  199. {
  200. PostQuitMessage(0);
  201. return 0;
  202. }
  203.  
  204. default:
  205. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  206. }
  207. }
  208.  
  209. int main(int argc, char** argv) {
  210. HWND hWnd;
  211. WNDCLASS wincl = { 0 };
  212. MSG message;
  213. int nCmdShow = SW_SHOW;
  214. HINSTANCE hThisInstance = GetModuleHandle(NULL);
  215. wincl.style = CS_HREDRAW | CS_VREDRAW;
  216. wincl.lpfnWndProc = WindProc;
  217. wincl.hInstance = hThisInstance;
  218. wincl.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  219. wincl.lpszClassName = szWinClass;
  220.  
  221. if (!RegisterClass(&wincl)) {
  222. MessageBox(NULL, "Can't register class!", "ERROR!", MB_OK);
  223. return 0;
  224. }
  225.  
  226. hWnd = CreateWindowEx
  227. (
  228. 0,
  229. szWinClass, szWinName,
  230. WS_OVERLAPPEDWINDOW,
  231. CW_USEDEFAULT,
  232. CW_USEDEFAULT,
  233. 320, 240,
  234. HWND_DESKTOP, NULL,
  235. hThisInstance,
  236. NULL);
  237.  
  238. if (!hWnd) {
  239. MessageBox(NULL, "Can't create window!", "ERROR!", MB_OK);
  240. return 0;
  241. }
  242.  
  243. ShowWindow(hWnd, nCmdShow);
  244. UpdateWindow(hWnd);
  245.  
  246. //process message
  247. while (GetMessage(&message, NULL, 0, 0)) {
  248. TranslateMessage(&message);
  249. DispatchMessage(&message);
  250. }
  251.  
  252. DeleteObject(redPen);
  253. return 0;
  254. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement