Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.02 KB | None | 0 0
  1. #include <windows.h>
  2. #include <stdio.h>
  3.  
  4. const char* NazwaKlasy = TEXT("Klasa Okienka");
  5. MSG Komunikat;
  6.  
  7. enum Figure {
  8. LINE, RECTANGLE, TRIANGLE, ELLIPSE
  9. };
  10. Figure figure[4] = { LINE,RECTANGLE,TRIANGLE,ELLIPSE };
  11.  
  12. int colors[16] = {
  13. 0x000FFFF,
  14. 0x0808080,
  15. 0x0000080,
  16. 0x0C0C0C0,
  17. 0x0000000,
  18. 0x0008000,
  19. 0x0808000,
  20. 0x0008080,
  21. 0x00000FF,
  22. 0x000FF00,
  23. 0x0800080,
  24. 0x0FFFFFF,
  25. 0x0FF00FF,
  26. 0x0800000,
  27. 0x0FF0000,
  28. 0x0FFFF00,
  29. };
  30. RECT figures_rect[4];
  31. LPRECT colors_rect[16];
  32. bool draw = false;
  33.  
  34. POINT first_point;
  35. int selected_brush_color = -1;
  36. int selected_pen_color = -1;
  37.  
  38. Figure selected_figure = ELLIPSE;
  39.  
  40. // OBSŁUGA ZDARZEŃ
  41. LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam)
  42. {
  43. HBRUSH brush;
  44. int saved;
  45.  
  46. switch (msg)
  47. {
  48. case WM_DESTROY:
  49. PostQuitMessage(0);
  50. break;
  51.  
  52. case WM_LBUTTONDOWN:
  53. {
  54.  
  55. RECT cr;
  56. POINT mouse_pos;
  57. mouse_pos.x = LOWORD(lParam);
  58. mouse_pos.y = HIWORD(lParam);
  59.  
  60. GetClientRect(hwnd, &cr);
  61.  
  62. if (mouse_pos.x > cr.right - 175 && mouse_pos.y < 600)
  63. // Przybornik
  64. {
  65. for (int i = 0; i < 16; i++)
  66. {
  67. if (PtInRect(colors_rect[i], mouse_pos)) // Sprawdzanie kolizji z kolorowymi kwadratami
  68. {
  69. selected_brush_color = colors[i];
  70. break;
  71. }
  72. }
  73. for (int i = 0; i < 4; i++)
  74. {
  75. if (PtInRect(&figures_rect[i], mouse_pos)) // Sprawdzanie kolizji z kwadratami figur
  76. {
  77. selected_figure = figure[i];
  78. break;
  79. }
  80. }
  81.  
  82. draw = false;
  83. }
  84. else
  85. // Pole rysowania
  86. {
  87. draw = true;
  88. first_point = mouse_pos;
  89. }
  90. break;
  91. }
  92. case WM_RBUTTONDOWN:
  93. {
  94.  
  95. RECT cr;
  96. POINT mouse_pos;
  97. mouse_pos.x = LOWORD(lParam);
  98. mouse_pos.y = HIWORD(lParam);
  99.  
  100. GetClientRect(hwnd, &cr);
  101.  
  102. if (mouse_pos.x > cr.right - 175 && mouse_pos.y < 600)
  103. // INTERFEJS
  104. {
  105. for (int i = 0; i < 16; i++)
  106. {
  107. if (PtInRect(colors_rect[i], mouse_pos))
  108. {
  109. selected_pen_color = colors[i];
  110. break;
  111. }
  112. }
  113. }
  114. break;
  115. }
  116. case WM_LBUTTONUP:
  117. {
  118. RECT cr;
  119. POINT mouse_pos;
  120. mouse_pos.x = LOWORD(lParam);
  121. mouse_pos.y = HIWORD(lParam);
  122. GetClientRect(hwnd, &cr);
  123. if (draw)
  124. {
  125. if (selected_brush_color == -1)
  126. {
  127. MessageBox(hwnd, TEXT("Wybierz kolor pedzla"), TEXT("Błęd"), NULL);
  128. break;
  129. }
  130.  
  131. if (selected_pen_color == -1)
  132. {
  133. MessageBox(hwnd, TEXT("Wybierz kolor piora"), TEXT("Błęd"), NULL);
  134. break;
  135. }
  136. if (mouse_pos.x > cr.right - 175 && mouse_pos.y < 600) break;
  137.  
  138. HDC hdc = GetDC(hwnd);
  139.  
  140. switch (selected_figure)
  141. {
  142. case LINE:
  143. SelectObject(hdc, GetStockObject(DC_BRUSH));
  144. SelectObject(hdc, GetStockObject(DC_PEN));
  145. SetDCBrushColor(hdc, selected_brush_color);
  146. SetDCPenColor(hdc, selected_pen_color);
  147. MoveToEx(hdc, first_point.x, first_point.y, (LPPOINT)NULL);
  148. LineTo(hdc, mouse_pos.x, mouse_pos.y);
  149. break;
  150. case RECTANGLE:
  151. SelectObject(hdc, GetStockObject(DC_BRUSH));
  152. SelectObject(hdc, GetStockObject(DC_PEN));
  153. SetDCBrushColor(hdc, selected_brush_color);
  154. SetDCPenColor(hdc, selected_pen_color);
  155. Rectangle(hdc, first_point.x, first_point.y, mouse_pos.x, mouse_pos.y);
  156. break;
  157. case TRIANGLE:
  158. {
  159. SelectObject(hdc, GetStockObject(DC_BRUSH));
  160. SelectObject(hdc, GetStockObject(DC_PEN));
  161. SetDCBrushColor(hdc, selected_brush_color);
  162. SetDCPenColor(hdc, selected_pen_color);
  163. MoveToEx(hdc, first_point.x,first_point.y,NULL);
  164. POINT point[3];
  165.  
  166. point[0].x = mouse_pos.x;
  167. point[0].y = mouse_pos.y;
  168. point[1].x = first_point.x - (mouse_pos.x - first_point.x);
  169. point[1].y = mouse_pos.y;
  170. point[2].x = first_point.x;
  171. point[2].y = first_point.y;
  172.  
  173. Polygon(hdc, point, 3);
  174.  
  175. break;
  176. }
  177. case ELLIPSE:
  178. SelectObject(hdc, GetStockObject(DC_BRUSH));
  179. SelectObject(hdc, GetStockObject(DC_PEN));
  180. SetDCBrushColor(hdc, selected_brush_color);
  181. SetDCPenColor(hdc, selected_pen_color);
  182. Ellipse(hdc, first_point.x, first_point.y, mouse_pos.x, mouse_pos.y);
  183. break;
  184. }
  185.  
  186. ReleaseDC(hwnd, hdc);
  187. }
  188. break;
  189. }
  190. case WM_PAINT:
  191. {
  192. //InvalidateRect(hwnd, NULL, true);
  193. RedrawWindow(hwnd, NULL, NULL, RDW_ERASE | RDW_INVALIDATE);
  194.  
  195. HDC hdc;
  196. PAINTSTRUCT ps;
  197. RECT cr;
  198. HBRUSH int_brush;
  199. int column;
  200.  
  201. GetClientRect(hwnd, &cr);
  202.  
  203. hdc = BeginPaint(hwnd, &ps);
  204. saved = SaveDC(hdc);
  205.  
  206. int_brush = CreateSolidBrush(0x00FEDCBA);
  207.  
  208. SelectObject(hdc, int_brush);
  209. Rectangle(hdc, cr.right - 175, cr.top, cr.right, 600);
  210.  
  211. column = 0;
  212. for (int i = 0; i < 16; i++)
  213. {
  214. SelectObject(hdc, GetStockObject(DC_BRUSH));
  215. SetDCBrushColor(hdc, colors[i]);
  216.  
  217. RECT *color_rect = new RECT;
  218.  
  219. color_rect->left = cr.right - 150 + 55 * column;
  220. color_rect->top = cr.top + 20 + 55 * (i % 8);
  221. color_rect->right = cr.right - 100 + 55 * column;
  222. color_rect->bottom = cr.top + 70 + 55 * (i % 8);
  223.  
  224. Rectangle(hdc,
  225. color_rect->left,
  226. color_rect->top,
  227. color_rect->right,
  228. color_rect->bottom);
  229.  
  230. colors_rect[i] = color_rect;
  231.  
  232. if (i == 7)
  233. column = 1;
  234. }
  235.  
  236. column = 0;
  237. for (int i = 0; i < 4; i++)
  238. {
  239. SelectObject(hdc, GetStockObject(WHITE_BRUSH));
  240.  
  241. if (i == 2) column = 1;
  242. figures_rect[i].left = cr.right - 150 + 55 * column;
  243. figures_rect[i].top = cr.top + 480 + 55 * (i % 2);
  244. figures_rect[i].right = cr.right - 100 + 55 * column;
  245. figures_rect[i].bottom = cr.top + 530 + 55 * (i % 2);
  246.  
  247. Rectangle(hdc,
  248. figures_rect[i].left,
  249. figures_rect[i].top,
  250. figures_rect[i].right,
  251. figures_rect[i].bottom);
  252. }
  253.  
  254.  
  255. MoveToEx(hdc, figures_rect[0].left+5,figures_rect[0].top+5, NULL); // Linia w kwadracie
  256. LineTo(hdc, figures_rect[0].right - 5, figures_rect[0].bottom - 5);
  257.  
  258. MoveToEx(hdc, figures_rect[1].left + 5, figures_rect[1].top + 5, NULL); //Kwadrat w kwadracie
  259. Rectangle(hdc, figures_rect[1].left+5,figures_rect[1].top+5,figures_rect[1].right - 5, figures_rect[1].bottom - 5);
  260.  
  261. MoveToEx(hdc, figures_rect[2].left + 5, figures_rect[2].bottom - 5, NULL); // Trójkat w kwadracie
  262. LineTo(hdc, figures_rect[2].right - 5, figures_rect[2].bottom - 5);
  263. LineTo(hdc, (figures_rect[2].right+figures_rect[2].left)/2, figures_rect[2].top + 5);
  264. LineTo(hdc, figures_rect[2].left + 5, figures_rect[2].bottom - 5);
  265.  
  266. MoveToEx(hdc, figures_rect[3].left + 5, figures_rect[3].top + 5, NULL); // Kolo w kwadracie
  267. Ellipse(hdc, figures_rect[3].left + 5, figures_rect[3].top + 5, figures_rect[3].right - 5, figures_rect[3].bottom - 5);
  268.  
  269. DeleteObject(int_brush);
  270.  
  271. RestoreDC(hdc, saved);
  272. EndPaint(hwnd, &ps);
  273. }
  274. default:
  275. return DefWindowProc(hwnd, msg, wParam, lParam);
  276. }
  277.  
  278. return 0;
  279. }
  280.  
  281. int WINAPI WinMain(HINSTANCE hInstance,
  282. HINSTANCE hPrevInstance,
  283. LPSTR lpCmdLine,
  284. int nCmdShow)
  285. {
  286.  
  287.  
  288. // WYPEŁNIANIE STRUKTURY
  289. WNDCLASSEX wc;
  290.  
  291. wc.cbSize = sizeof(WNDCLASSEX);
  292. wc.style = CS_VREDRAW | CS_HREDRAW; // Dzięki temu rysuje się jeszcze raz
  293. wc.lpfnWndProc = WndProc;
  294. wc.cbClsExtra = 0;
  295. wc.cbWndExtra = 0;
  296. wc.hInstance = hInstance;
  297. wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
  298. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  299. wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
  300. wc.lpszMenuName = NULL;
  301. wc.lpszClassName = NazwaKlasy;
  302. wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
  303.  
  304.  
  305. // REJESTROWANIE KLASY OKNA
  306. if (!RegisterClassEx(&wc))
  307. {
  308. MessageBox(NULL, TEXT("Nie przeszło rejestracji"), TEXT("..."),
  309. MB_ICONEXCLAMATION | MB_OK);
  310. return 1;
  311. }
  312.  
  313.  
  314. // TWORZENIE OKNA
  315. HWND hwnd;
  316.  
  317. hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, NazwaKlasy, TEXT("Oto okienko"), WS_OVERLAPPEDWINDOW,
  318. CW_USEDEFAULT, CW_USEDEFAULT, 600, 400, NULL, NULL, hInstance, NULL);
  319.  
  320. if (hwnd == NULL)
  321. {
  322. MessageBox(NULL, TEXT("Ni ma okna"), TEXT("..."), MB_ICONEXCLAMATION);
  323. return 1;
  324. }
  325.  
  326.  
  327. ShowWindow(hwnd, nCmdShow); // Pokaż okienko...
  328. UpdateWindow(hwnd);
  329.  
  330.  
  331. // Pętla komunikatów
  332. while (GetMessage(&Komunikat, NULL, 0, 0))
  333. {
  334. TranslateMessage(&Komunikat);
  335. DispatchMessage(&Komunikat);
  336. }
  337.  
  338. return Komunikat.wParam;
  339. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement