Advertisement
Guest User

Untitled

a guest
Oct 4th, 2015
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.33 KB | None | 0 0
  1. #include <Windows.h>
  2.  
  3. #define MAX_LENGTH 255
  4. #define BTN_WIDTH 60
  5. #define BTN_HEIGHT 60
  6. #define ID_BTN_DRAW 1
  7. #define ID_BTN_CLEAR 2
  8.  
  9.  
  10.  
  11. LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
  12.  
  13.  
  14. HDC hDC;
  15. PAINTSTRUCT ps;
  16. RECT rectWindow, rectText;
  17. HINSTANCE hInst;
  18. bool startDraw = false, clear = false;
  19.  
  20. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nCmdShow)
  21. {
  22. TCHAR szClassName[] = L"Мой класс";
  23. MSG msg;
  24. HWND hWnd;
  25. WNDCLASSEX wc;
  26.  
  27. hInst = hInstance;
  28. wc.cbSize = sizeof(wc);
  29. wc.style = CS_HREDRAW | CS_VREDRAW;
  30. wc.lpfnWndProc = WndProc;
  31. wc.lpszMenuName = NULL;
  32. wc.lpszClassName = szClassName;
  33. wc.cbWndExtra = NULL;
  34. wc.cbClsExtra = NULL;
  35. wc.hIcon = LoadIcon(NULL, IDC_ICON);
  36. wc.hIconSm = LoadIcon(NULL, IDC_ICON);
  37. wc.hCursor = LoadCursor(NULL, IDC_ARROW);
  38. wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
  39. wc.hInstance = hInstance;
  40. if (!RegisterClassEx(&wc))
  41. {
  42. MessageBox(NULL, L"Не получилось зарегистрировать класс!", L"Ошибка", MB_OK);
  43. return NULL;
  44. }
  45.  
  46.  
  47. hWnd = CreateWindow(
  48. szClassName,
  49. L"Полноценная оконная процедура",
  50. WS_OVERLAPPEDWINDOW,
  51. 600,
  52. 150,
  53. 505,
  54. 460,
  55. (HWND)NULL,
  56. NULL,
  57. hInstance,
  58. NULL);
  59. if (!hWnd)
  60. {
  61. MessageBox(NULL, L"Не получилось создать окно!", L"Ошибка", MB_OK);
  62. return NULL;
  63. }
  64.  
  65. ShowWindow(hWnd, nCmdShow);
  66. UpdateWindow(hWnd);
  67.  
  68.  
  69. while (GetMessage(&msg, NULL, NULL, NULL))
  70. {
  71. TranslateMessage(&msg);
  72. DispatchMessage(&msg);
  73. }
  74. return msg.wParam;
  75. }
  76.  
  77. LRESULT CALLBACK WndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
  78. {
  79. static HWND hBtnDraw, hBtnClear;
  80. static HPEN hPenRed, hPenGreen;
  81. static int cxClient, cyClient;
  82.  
  83. LPDRAWITEMSTRUCT pDIS;
  84. POINT pt5[5];
  85. PAINTSTRUCT ps;
  86. HDC hdc;
  87. RECT rect;
  88.  
  89.  
  90. switch (uMsg)
  91. {
  92. case WM_CREATE:
  93. hBtnDraw = CreateWindow(
  94. L"button",
  95. L"",
  96. WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
  97. 10,
  98. 10,
  99. BTN_WIDTH,
  100. BTN_HEIGHT,
  101. hWnd,
  102. (HMENU)ID_BTN_DRAW,
  103. hInst,
  104. NULL);
  105.  
  106. hBtnClear = CreateWindow(
  107. L"button",
  108. L"",
  109. WS_CHILD | WS_VISIBLE | BS_OWNERDRAW,
  110. 10,
  111. 80,
  112. BTN_WIDTH,
  113. BTN_HEIGHT,
  114. hWnd,
  115. (HMENU)ID_BTN_CLEAR,
  116. hInst,
  117. NULL);
  118. startDraw = false;
  119. hPenRed = CreatePen(PS_SOLID, 2, RGB(255, 0, 0));
  120. break;
  121. case WM_SIZE:
  122. cxClient = LOWORD(lParam);
  123. cyClient = HIWORD(lParam);
  124. return 0;
  125.  
  126. case WM_PAINT:
  127. hdc = BeginPaint(hWnd, &ps);
  128. if (startDraw)
  129. {
  130. SetViewportOrgEx(hdc, 80, 10, NULL);
  131. Rectangle(hdc, 0, 0, 400, 400); //frame
  132. Rectangle(hdc, 100, 220, 220, 280); //engine
  133. Ellipse(hdc, 80, 280, 120, 320); //whell
  134. Ellipse(hdc, 260, 280, 300, 320); //whell
  135. Rectangle(hdc, 220, 140, 300, 280); //cabine
  136. pt5[0].x = 240; pt5[0].y = 160;
  137. pt5[1].x = 280; pt5[1].y = 160;
  138. pt5[2].x = 280; pt5[2].y = 220;
  139. pt5[3].x = 240; pt5[3].y = 220;
  140. pt5[4].x = 240; pt5[4].y = 160;
  141. Polyline(hdc, pt5, 5); //window
  142. pt5[0].x = 120; pt5[0].y = 230;
  143. pt5[1].x = 200; pt5[1].y = 230;
  144. pt5[2].x = 200; pt5[2].y = 250;
  145. pt5[3].x = 120; pt5[3].y = 250;
  146. pt5[4].x = 120; pt5[4].y = 230;
  147. Polyline(hdc, pt5, 5); //engine
  148. MoveToEx(hdc, 140, 250, NULL);
  149. LineTo(hdc, 140, 230);
  150. MoveToEx(hdc, 160, 250, NULL);
  151. LineTo(hdc, 160, 230);
  152. MoveToEx(hdc, 180, 250, NULL);
  153. LineTo(hdc, 180, 230);
  154. //антены
  155. Arc(hdc, 240, 100, 360, 180, 320, 100, 260, 140);
  156. Arc(hdc, 260, 100, 380, 180, 340, 100, 280, 140);
  157. //литье
  158. MoveToEx(hdc, 260, 300, NULL);
  159. LineTo(hdc, 300, 300);
  160.  
  161. MoveToEx(hdc, 80, 300, NULL);
  162. LineTo(hdc, 120, 300);
  163.  
  164. MoveToEx(hdc, 100, 280, NULL);
  165. LineTo(hdc, 100, 320);
  166.  
  167. MoveToEx(hdc, 280, 280, NULL);
  168. LineTo(hdc, 280, 320);
  169. startDraw = false;
  170. }
  171. if (clear)
  172. {
  173. SelectObject(hdc, CreatePen(BS_SOLID, 0, RGB(255, 255, 255)));
  174. Rectangle(hdc, 75, 5, 600, 600);
  175. clear = false;
  176. }
  177. EndPaint(hWnd, &ps);
  178. break;
  179. case WM_DESTROY:
  180. PostQuitMessage(NULL);
  181. break;
  182. case WM_COMMAND:
  183. switch (LOWORD(wParam))
  184. {
  185. case ID_BTN_DRAW:
  186. startDraw = true;
  187. InvalidateRect(hWnd, NULL, true);
  188. break;
  189. case ID_BTN_CLEAR:
  190. clear = true;
  191. InvalidateRect(hWnd, NULL, true);
  192. break;
  193. }
  194. break;
  195. case WM_DRAWITEM:
  196. pDIS = (LPDRAWITEMSTRUCT)lParam;
  197.  
  198. switch (pDIS->CtlID)
  199. {
  200. case ID_BTN_DRAW:
  201. FillRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(150, 150, 150)));
  202. FrameRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(0, 0, 0)));
  203.  
  204. SelectObject(pDIS->hDC, CreatePen(PS_SOLID, 1, RGB(0, 255, 0)));
  205. SelectObject(pDIS->hDC, CreateSolidBrush(RGB(255, 0, 0)));
  206. Ellipse(pDIS->hDC, 20, 20, 40, 40);
  207. break;
  208. case ID_BTN_CLEAR:
  209. FillRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(255, 255, 255)));
  210. FrameRect(pDIS->hDC, &pDIS->rcItem, CreateSolidBrush(RGB(0, 0, 0)));
  211.  
  212. SelectObject(pDIS->hDC, CreatePen(PS_SOLID, 1, RGB(0, 0, 0)));
  213. SelectObject(pDIS->hDC, CreateSolidBrush(RGB(150, 150, 150)));
  214. pt5[0].x = 10; pt5[0].y = 10;
  215. pt5[1].x = 20; pt5[1].y = 50;
  216. pt5[2].x = 40; pt5[2].y = 50;
  217. pt5[3].x = 50; pt5[3].y = 10;
  218. Polygon(pDIS->hDC, pt5, 4);
  219. break;
  220. }
  221. break;
  222.  
  223. default:
  224. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  225. }
  226. return NULL;
  227. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement