Advertisement
Guest User

Untitled

a guest
Oct 13th, 2019
157
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.97 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. // Function prototypes.
  15. int WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int);
  16. LRESULT CALLBACK MainWndProc(HWND, UINT, WPARAM, LPARAM);
  17. void paintObject(HWND hWnd, HDC hDC, PAINTSTRUCT ps, int posX, int posY, POINT cursorPosition);
  18. void paintPosition(HWND hWnd, HDC hDC, PAINTSTRUCT ps, POINT cursorPosition);
  19. void drawLine(HDC hDC, HWND hWnd, PAINTSTRUCT ps, int posX, int posY);
  20.  
  21. HWND TextBox;
  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 und = GetStockObject(WHITE_BRUSH); white background brush
  41. wcx.hbrBackground = (HBRUSH) COLOR_BACKGROUND;
  42. wcx.lpszMenuName = (LPCSTR)"MainMenu"; // name of menu resource
  43. wcx.lpszClassName = (LPCSTR)"MainWClass"; // name of window class
  44.  
  45. // Register the window class.
  46.  
  47. if (!RegisterClass(&wcx)) return FALSE;
  48.  
  49. // create window of registered class
  50.  
  51. hWnd = CreateWindow(
  52. "MainWClass", // name of window class
  53. "ITU", // title-bar string
  54. WS_OVERLAPPEDWINDOW, // top-level window
  55. 200, // default horizontal position
  56. 25, // default vertical positionv
  57. 1000, // default width
  58. 700, // default height
  59. (HWND)NULL, // no owner window
  60. (HMENU)NULL, // use class menu
  61. hInstance, // handle to application instance
  62. (LPVOID)NULL); // no window-creation data
  63. if (!hWnd) return FALSE;
  64.  
  65. // Show the window and send a WM_PAINT message to the window procedure.
  66. // Record the current cursor position.
  67.  
  68. ShowWindow(hWnd, nCmdShow);
  69. UpdateWindow(hWnd);
  70.  
  71. // loop of message processing
  72. while ((bRet = GetMessage(&msg, NULL, 0, 0)) != 0)
  73. {
  74. if (bRet == -1)
  75. {
  76. // handle the error and possibly exit
  77. }
  78. else
  79. {
  80. TranslateMessage(&msg);
  81. DispatchMessage(&msg);
  82. }
  83. }
  84. return (int)msg.wParam;
  85. }
  86.  
  87.  
  88. LRESULT CALLBACK MainWndProc(
  89. HWND hWnd, // handle to window
  90. UINT uMsg, // message identifier
  91. WPARAM wParam, // first message parameter
  92. LPARAM lParam) // second message parameter
  93. {
  94. HDC hDC;
  95. PAINTSTRUCT ps;
  96. POINT cursorPosition;
  97.  
  98.  
  99. // init cursor position
  100. GetCursorPos(&cursorPosition);
  101. ScreenToClient(hWnd, &cursorPosition);
  102. switch (uMsg)
  103. {
  104. case WM_CREATE:
  105.  
  106.  
  107.  
  108. /*CreateWindow("BUTTON","Go", WS_VISIBLE | WS_CHILD | WS_BORDER,
  109. 420,10,70, 20,
  110. hWnd, (HMENU) 1, NULL, NULL);*/
  111. break;
  112.  
  113. // character input
  114. case WM_CHAR:
  115. switch (wParam) {
  116. case 0x08: // backspace
  117. case 0x0A: // linefeed
  118. case 0x1B: // escape
  119. TextBox = CreateWindow("EDIT",
  120. "U pressed escape button",
  121. WS_BORDER | WS_CHILD | WS_VISIBLE,
  122. 10, 10, 400, 20,
  123. hWnd, NULL, NULL, NULL);
  124. break;
  125.  
  126. case 0x09: // tab
  127. if (GetCursorPos(&cursorPosition))
  128. {
  129. //cursor position now in p.x and p.y
  130. if (ScreenToClient(hWnd, &cursorPosition))
  131. {
  132. ::MessageBeep(1);
  133. //p.x and p.y are now relative to hwnd's client area
  134. paintPosition(hWnd, hDC, ps, cursorPosition);
  135. ::MessageBox(hWnd, "Button was clicked", "Button was clicked", MB_OK);
  136. }
  137. }
  138. break;
  139.  
  140. default:
  141. break;
  142. }
  143. break;
  144.  
  145. // key input
  146. case WM_KEYDOWN:
  147. switch (wParam) {
  148. // update posX and posY in order to move object
  149. case VK_LEFT: // left arrow
  150. break;
  151. case VK_RIGHT: // right arrow
  152. break;
  153. case VK_UP: // up arrow
  154. break;
  155. case VK_DOWN: // down arrow
  156. break;
  157.  
  158. // react on the other pressed keys
  159. case VK_SPACE: // space
  160. break;
  161. case VK_BACK: // backspace
  162. break;
  163. case VK_TAB: // tab
  164. break;
  165. // more virtual codes can be found here: https://docs.microsoft.com/en-us/windows/win32/inputdev/virtual-key-codes
  166. }
  167. break;
  168.  
  169. // get cursor position
  170. case WM_MOUSEMOVE:
  171. break;
  172.  
  173. // react on mouse clicks
  174. case WM_LBUTTONDOWN:
  175. //hDC = BeginPaint(hWnd, &ps);
  176. //paintObject(hWnd, hDC, ps, 100, 600, cursorPosition);
  177.  
  178. paintPosition(hWnd, hDC, ps, cursorPosition);
  179. //paint other objects
  180. //paintObject2(hWnd, hDC, ps, 150, 250, cursorPosition);
  181. //paintObject3(hWnd, hDC, ps, 200, 300, cursorPosition);
  182. //drawLine(hDC, hWnd, ps, 100, 100);
  183. //EndPaint(hWnd, &ps);
  184. //DeleteDC(hDC);
  185. break;
  186. case WM_LBUTTONUP:
  187. break;
  188.  
  189. // paint objects
  190. case WM_PAINT:
  191. hDC = BeginPaint(hWnd, &ps);
  192. paintPosition(hWnd, hDC, ps, cursorPosition);
  193. paintObject(hWnd, hDC, ps, 100, 100, cursorPosition);
  194. //paintObject(hWnd, hDC, ps, 100, 600, cursorPosition);
  195. //paintPosition(hWnd, hDC, ps);
  196. //paint other objects
  197. //paintObject2(hWnd, hDC, ps, 150, 250, cursorPosition);
  198. //paintObject3(hWnd, hDC, ps, 200, 300, cursorPosition);
  199. drawLine(hDC, hWnd, ps, 500, 500);
  200. paintPosition(hWnd, hDC, ps, cursorPosition);
  201. EndPaint(hWnd, &ps);
  202. DeleteDC(hDC);
  203.  
  204.  
  205. break;
  206.  
  207. //
  208. // Process other messages.
  209. //
  210.  
  211. default:
  212. return DefWindowProc(hWnd, uMsg, wParam, lParam);
  213. }
  214.  
  215. }
  216.  
  217. void drawLine(HDC hDC, HWND hWnd, PAINTSTRUCT ps, int posX, int posY){
  218. HPEN hPenOld;
  219. HPEN hLinePen;
  220. COLORREF qLineColor;
  221.  
  222. qLineColor = RGB (255, 0, 0);
  223. hLinePen = CreatePen(PS_SOLID, 7, qLineColor);
  224. hPenOld = (HPEN)SelectObject(hDC, hLinePen);
  225.  
  226. MoveToEx(hDC, posX, posX, NULL);
  227. LineTo(hDC, posY, posY);
  228. return;
  229. }
  230.  
  231.  
  232. void paintObject(HWND hWnd, HDC hDC, PAINTSTRUCT ps, int posX, int posY, POINT cursorPosition)
  233. {
  234. // Paint rectangles, ellipses, polygons, lines etc.
  235. HPEN hPenOld;
  236. HPEN hLinePen;
  237. COLORREF qLineColor;
  238.  
  239. qLineColor = RGB (200, 0, 0);
  240. hLinePen = CreatePen(PS_SOLID, 7, qLineColor);
  241. hPenOld = (HPEN)SelectObject(hDC, hLinePen);
  242.  
  243. MoveToEx(hDC, posX, posY, NULL);
  244. //LineTo(hDC, cursorPosition.x, cursorPosition.y);
  245. LineTo(hDC, 100, 110);
  246. LineTo(hDC, 110, 110);
  247. LineTo(hDC, 110, 100);
  248. LineTo(hDC, 100, 100);
  249. LineTo(hDC, cursorPosition.x, cursorPosition.y);
  250. //SelectObject(hDC, hPenOld);
  251. //DeleteObject(hLinePen);
  252. //EndPaint(hWnd, &ps);
  253. return;
  254. }
  255.  
  256. void paintPosition(HWND hWnd, HDC hDC, PAINTSTRUCT ps, POINT cursorPosition)
  257. {
  258. char text[40]; // buffer to store an output text
  259. HFONT font; // new large font
  260. HFONT oldFont; // saves the previous font
  261.  
  262. font = CreateFont(25, 0, 0, 0, 0, FALSE, FALSE, FALSE, ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, FF_DONTCARE, 0);
  263. oldFont = (HFONT)SelectObject(hDC, font);
  264. sprintf(text, "Position -- x:%d, y:%d", cursorPosition.x, cursorPosition.y);
  265. TextOut(hDC, 50, 600, text, (int)strlen(text));
  266. SelectObject(hDC, oldFont);
  267. DeleteObject(font);
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement