Advertisement
Guest User

Untitled

a guest
Oct 15th, 2019
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 8.09 KB | None | 0 0
  1. #include <windows.h>
  2. #include <string.h>
  3. #include <stdio.h>
  4. #include <math.h>
  5.  
  6.  
  7. // Global variable
  8. HINSTANCE hInst;
  9. UINT MessageCount = 0;
  10. UINT Count = 0;
  11. int posX = 0;
  12. int posY = 0;
  13.  
  14. int pom = 0;
  15.  
  16. // Function prototypes.
  17. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  18. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  19. void paintObject(HWND hWnd, HDC hDC, PAINTSTRUCT ps, int posX, int posY, POINT cursorPosition);
  20. void paintPosition(HWND hWnd, HDC hDC, PAINTSTRUCT ps);
  21.  
  22. // Application entry point. This is the same as main() in standart C.
  23. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  24. {
  25. MSG msg;
  26. BOOL bRet;
  27. WNDCLASS wcx; // register class
  28. HWND hWnd;
  29.  
  30. hInst = hInstance; // Save the application-instance handle.
  31. // Fill in the window class structure with parameters that describe the main window.
  32.  
  33. wcx.style = CS_HREDRAW | CS_VREDRAW; // redraw if size changes
  34. wcx.lpfnWndProc = (WNDPROC)MainWndProc; // points to window procedure
  35. wcx.cbClsExtra = 0; // no extra class memory
  36. wcx.cbWndExtra = 0; // no extra window memory
  37. wcx.hInstance = hInstance; // handle to instance
  38. wcx.hIcon = LoadIcon(NULL, IDI_APPLICATION); // predefined app. icon
  39. wcx.hCursor = LoadCursor(NULL, IDC_ARROW); // predefined arrow
  40. wcx.hbrBackground = GetStockObject(WHITE_BRUSH); // white background brush
  41. wcx.lpszMenuName = (LPCSTR)"MainMenu"; // name of menu resource
  42. wcx.lpszClassName = (LPCSTR)"MainWClass"; // name of window class
  43.  
  44. // Register the window class.
  45.  
  46. if (!RegisterClass(&wcx)) return FALSE;
  47.  
  48. // create window of registered class
  49.  
  50. hWnd = CreateWindow(
  51. "MainWClass", // name of window class
  52. "ITU", // title-bar string
  53. WS_OVERLAPPEDWINDOW, // top-level window
  54. 200, // default horizontal position
  55. 25, // default vertical position
  56. 1000, // default width
  57. 700, // default height
  58. (HWND)NULL, // no owner window
  59. (HMENU)NULL, // use class menu
  60. hInstance, // handle to application instance
  61. (LPVOID)NULL); // no window-creation data
  62. if (!hWnd) return FALSE;
  63.  
  64. // Show the window and send a WM_PAINT message to the window procedure.
  65. // Record the current cursor position.
  66.  
  67. ShowWindow(hWnd, nCmdShow);
  68. UpdateWindow(hWnd);
  69.  
  70. // loop of message processing
  71. while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
  72. {
  73. if (bRet == -1)
  74. {
  75. // handle the error and possibly exit
  76. }
  77. else
  78. {
  79. TranslateMessage(&msg);
  80. DispatchMessage(&msg);
  81. }
  82. }
  83. return (int)msg.wParam;
  84. }
  85.  
  86.  
  87. LRESULT CALLBACK MainWndProc(
  88. HWND hWnd, // handle to window
  89. UINT uMsg, // message identifier
  90. WPARAM wParam, // first message parameter
  91. LPARAM lParam) // second message parameter
  92. {
  93. HDC hDC;
  94. PAINTSTRUCT ps;
  95. POINT cursorPosition;
  96. HWND hwndTmp; // window handle where mouse button was released
  97.  
  98. // init cursor position
  99. GetCursorPos(&cursorPosition);
  100. ScreenToClient(hWnd, &cursorPosition);
  101.  
  102. switch (uMsg)
  103. {
  104. case WM_CREATE:
  105. break;
  106.  
  107. // character input
  108. case WM_CHAR:
  109. switch (wParam) {
  110. case 0x08: // backspace
  111. case 0x0A: // linefeed
  112. case 0x1B: // escape
  113. break;
  114.  
  115. case 0x09: // tab
  116. break;
  117.  
  118. default:
  119. break;
  120. }
  121. break;
  122.  
  123. // key input
  124. case WM_KEYDOWN:
  125. switch (wParam) {
  126. // update posX and posY in order to move object
  127. case VK_LEFT: // left arrow
  128. posX -= 10;
  129. InvalidateRect(hWnd, NULL, TRUE);
  130. break;
  131. case VK_RIGHT: // right arrow
  132. posX += 10;
  133. InvalidateRect(hWnd, NULL, TRUE);
  134. break;
  135. case VK_UP: // up arrow
  136. posY -= 10;
  137. InvalidateRect(hWnd, NULL, TRUE);
  138. break;
  139. case VK_DOWN: // down arrow
  140. posY += 10;
  141. InvalidateRect(hWnd, NULL, TRUE);
  142. break;
  143.  
  144. // react on the other pressed keys
  145. case VK_SPACE: // space
  146. pom = 1;
  147. InvalidateRect(hWnd, NULL, TRUE);
  148. break;
  149. case VK_BACK: // backspace
  150. break;
  151. case VK_TAB: // tab
  152. break;
  153. // more virtual codes can be found here: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
  154. }
  155. break;
  156.  
  157. // get cursor position
  158. case WM_MOUSEMOVE:
  159. GetCursorPos(&cursorPosition);
  160. ScreenToClient(hWnd, &cursorPosition);
  161. InvalidateRect(hWnd, NULL, TRUE);
  162. break;
  163.  
  164. // react on mouse clicks
  165. case WM_LBUTTONDOWN:
  166. SetCapture(hWnd);
  167. InvalidateRect(hWnd, NULL, TRUE);
  168. break;
  169. case WM_LBUTTONUP:
  170. cursorPosition.x = (int)LOWORD(lParam);
  171. cursorPosition.y = (int)HIWORD(lParam);
  172.  
  173. ClientToScreen(hWnd, &cursorPosition);
  174.  
  175. hwndTmp = WindowFromPoint(cursorPosition);
  176.  
  177. ReleaseCapture();
  178. break;
  179.  
  180. // paint objects
  181. case WM_PAINT:
  182. hDC = BeginPaint(hWnd, &ps);
  183. paintObject(hWnd, hDC, ps, posX, posY, cursorPosition);
  184. paintPosition(hWnd, hDC, ps);
  185. // paint other objects
  186. // paintObject2(hWnd, hDC, ps, posX, posY, cursorPosition);
  187. // paintObject3(hWnd, hDC, ps, posX, posY, cursorPosition);
  188. EndPaint(hWnd, &ps);
  189. DeleteDC(hDC);
  190. break;
  191.  
  192. //
  193. // Process other messages.
  194. //
  195.  
  196. default:
  197. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  198. }
  199. }
  200.  
  201.  
  202. void paintObject(HWND hWnd, HDC hDC, PAINTSTRUCT ps, int posX, int posY, POINT cursorPosition)
  203. {
  204. GetCursorPos(&cursorPosition);
  205.  
  206. HBRUSH brush, brush2,brush3, oldbrush, oldbrush2, oldbrush3;
  207. brush = CreateSolidBrush(RGB(0, 100, 255));
  208. brush2 = CreateSolidBrush(RGB(0, 255, 0));
  209. brush3 = CreateSolidBrush(RGB(0, 0, 0));
  210. oldbrush = SelectObject(hDC, brush);
  211. oldbrush2 = SelectObject(hDC, brush2);
  212. oldbrush3 = SelectObject(hDC, brush3);
  213.  
  214. if (pom) {
  215. brush2 = CreateSolidBrush(RGB(
  216. (BYTE)(rand() % 255), // red component of color
  217. (BYTE)(rand() % 255), // green component of color
  218. (BYTE)(rand() % 255) // blue component of color
  219. ));
  220. oldbrush2 = SelectObject(hDC, brush2);
  221. SelectObject(hDC, oldbrush2);
  222. pom = 0;
  223. }
  224.  
  225. Ellipse(hDC, posX + 330, posY + 120, posX + 270, posY + 150);
  226. Ellipse(hDC, posX + 310, posY + 150, posX + 290, posY + 290);
  227. MoveToEx(hDC, posX + 300, posY + 200, NULL);
  228. LineTo(hDC, posX + 400, posY + 150);
  229. MoveToEx(hDC, posX + 300, posY + 200, NULL);
  230. LineTo(hDC, posX + 200, posY + 150);
  231. MoveToEx(hDC, posX + 300, posY + 290, NULL);
  232. LineTo(hDC, posX + 280, posY + 400);
  233. MoveToEx(hDC, posX + 300, posY + 290, NULL);
  234. LineTo(hDC, posX + 320, posY + 400);
  235.  
  236.  
  237.  
  238.  
  239.  
  240.  
  241. SelectObject(hDC, oldbrush2);
  242. Ellipse(hDC, cursorPosition.x + 100, cursorPosition.y + 100, cursorPosition.x + 200, cursorPosition.y + 200);
  243. Ellipse(hDC, cursorPosition.x + 400, cursorPosition.y + 100, cursorPosition.x + 500, cursorPosition.y + 200);
  244. SelectObject(hDC, oldbrush3);
  245. Ellipse(hDC, cursorPosition.x + 50, cursorPosition.y + 0, cursorPosition.x + 550, cursorPosition.y + 150);
  246. MoveToEx(hDC, cursorPosition.x + 250, cursorPosition.y + 20, NULL);
  247. LineTo(hDC, cursorPosition.x + 280, cursorPosition.y - 50);
  248. MoveToEx(hDC, cursorPosition.x + 310, cursorPosition.y + 20, NULL);
  249. LineTo(hDC, cursorPosition.x + 280, cursorPosition.y - 50);
  250. MoveToEx(hDC, cursorPosition.x + 280, cursorPosition.y - 50, NULL);
  251. LineTo(hDC, cursorPosition.x + 280, cursorPosition.y - 150);
  252. MoveToEx(hDC, cursorPosition.x + 250, cursorPosition.y -100, NULL);
  253. LineTo(hDC, cursorPosition.x + 310, cursorPosition.y - 100);
  254. Ellipse(hDC, cursorPosition.x + 270, cursorPosition.y -150, cursorPosition.x + 290, cursorPosition.y - 120);
  255.  
  256.  
  257.  
  258.  
  259.  
  260. return;
  261. }
  262.  
  263. void paintPosition(HWND hWnd, HDC hDC, PAINTSTRUCT ps)
  264. {
  265. char text[40]; // buffer to store an output text
  266. HFONT font; // new large font
  267. HFONT oldFont; // saves the previous font
  268.  
  269. font = CreateFont(25, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, 0);
  270. oldFont = (HFONT)SelectObject(hDC, font);
  271. sprintf(text, "Position -- x:%d, y:%d", posX, posY);
  272. TextOut(hDC, 50, 600, text, (int)strlen(text));
  273. SelectObject(hDC, oldFont);
  274. DeleteObject(font);
  275. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement