Advertisement
YauhenMardan

WinAPI_5_2_circles

Mar 24th, 2018
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.11 KB | None | 0 0
  1. #pragma warning(disable:4996)
  2. #undef UNICODE
  3. #include <windows.h>
  4. #include <string>
  5. #include "KWnd.h"
  6.  
  7.  
  8. using namespace std;
  9.  
  10. LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam);
  11.  
  12. const int INTERVAL = 110;
  13.  
  14. bool isOdd(int i);
  15.  
  16. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  17. {
  18. MSG msg;
  19. KWnd mainWnd("MyWin", hInstance, nCmdShow, WndProc);
  20. while (GetMessage(&msg, NULL, 0, 0))
  21. {
  22. TranslateMessage(&msg);
  23. DispatchMessage(&msg);
  24. }
  25. return (msg.wParam);
  26. }
  27.  
  28. LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
  29. {
  30. PAINTSTRUCT ps;
  31. HDC hDC;
  32. int userReply;
  33. //timer pause
  34. static bool paused = false;
  35. static bool isEpeleptic1, isEpeleptic2, isEpeleptic3;
  36. //sx,sy
  37. static int sx, sy;
  38. //point for mouse
  39. static POINT mousePoint;
  40. //timer t
  41. static int t = 0;
  42. //pen
  43. static HPEN hPenCommon;
  44. //brushed
  45. static HBRUSH hBrushBlack, hBrushWhite, hBrushRed, hBrushYellow, hBrushGreen;
  46. //regions
  47. static HRGN ellipse1, ellipse2, ellipse3;
  48.  
  49. switch (message)
  50. {
  51. case WM_CREATE:
  52. //init values
  53. paused = false;
  54. isEpeleptic1 = false;
  55. isEpeleptic2 = false;
  56. isEpeleptic3 = false;
  57. //create regions
  58. ellipse1 = CreateEllipticRgn(sx / 10, 2 * sy / 10, 3 * sx / 10, 4 * sy / 10);
  59. ellipse2 = CreateEllipticRgn(4 * sx / 10, 2 * sy / 10, 6 * sx / 10, 4 * sy / 10);
  60. ellipse3 = CreateEllipticRgn(7 * sx / 10, 2 * sy / 10, 9 * sx / 10, 4 * sy / 10);
  61. //create pen
  62. hPenCommon = (HPEN)GetStockObject(1);
  63. //create brushes
  64. hBrushBlack = CreateSolidBrush(RGB(0, 0, 0));
  65. hBrushWhite = CreateSolidBrush(RGB(255, 255, 255));
  66. hBrushRed = CreateSolidBrush(RGB(255, 0, 0));
  67. hBrushYellow = CreateSolidBrush(RGB(255, 160, 0));
  68. hBrushGreen = CreateSolidBrush(RGB(0, 255, 0));
  69. //create timer
  70. SetTimer(hWnd, 1, INTERVAL, NULL);
  71. break;
  72. case WM_SIZE:
  73. sx = LOWORD(lParam);
  74. sy = HIWORD(lParam);
  75. SetRectRgn(ellipse1, sx / 10, 2 * sy / 10, 3 * sx / 10, 4 * sy / 10);
  76. SetRectRgn(ellipse2, 4 * sx / 10, 2 * sy / 10, 6 * sx / 10, 4 * sy / 10);
  77. SetRectRgn(ellipse3, 7 * sx / 10, 2 * sy / 10, 9 * sx / 10, 4 * sy / 10);
  78. break;
  79. case WM_LBUTTONDOWN:
  80. mousePoint.x = LOWORD(lParam);
  81. mousePoint.y = HIWORD(lParam);
  82. //check for point in region
  83. if (PtInRegion(ellipse1, mousePoint.x, mousePoint.y)) {
  84. isEpeleptic1 = true;
  85. }
  86. if (PtInRegion(ellipse2, mousePoint.x, mousePoint.y)) {
  87. isEpeleptic2 = true;
  88. }
  89. if (PtInRegion(ellipse3, mousePoint.x, mousePoint.y)) {
  90. isEpeleptic3 = true;
  91. }
  92. break;
  93. case WM_RBUTTONDOWN:
  94. mousePoint.x = LOWORD(lParam);
  95. mousePoint.y = HIWORD(lParam);
  96. //check for point in region
  97. if (PtInRegion(ellipse1, mousePoint.x, mousePoint.y)) {
  98. isEpeleptic1 = false;
  99. }
  100. if (PtInRegion(ellipse2, mousePoint.x, mousePoint.y)) {
  101. isEpeleptic2 = false;
  102. }
  103. if (PtInRegion(ellipse3, mousePoint.x, mousePoint.y)) {
  104. isEpeleptic3 = false;
  105. }
  106. break;
  107. case WM_PAINT:
  108. //begin paint
  109. hDC = BeginPaint(hWnd, &ps);
  110. //draw circles
  111. SelectObject(hDC, hPenCommon);
  112. //draw base
  113. SelectObject(hDC, hBrushRed);
  114. if (isOdd(t) && isEpeleptic1) {
  115. Ellipse(hDC, sx / 10 + sx / 20, 2 * sy / 10 + sy / 20, 3 * sx / 10 - sx / 20, 4 * sy / 10 - sy / 20);
  116. }
  117. else {
  118. Ellipse(hDC, sx / 10, 2 * sy / 10, 3 * sx / 10, 4 * sy / 10);
  119. }
  120. SelectObject(hDC, hBrushYellow);
  121. if (isOdd(t) && isEpeleptic2) {
  122. Ellipse(hDC, 4 * sx / 10 + sx / 20, 2 * sy / 10 + sy / 20, 6 * sx / 10 - sx / 20, 4 * sy / 10 - sy / 20);
  123. }
  124. else {
  125. Ellipse(hDC, 4 * sx / 10, 2 * sy / 10, 6 * sx / 10, 4 * sy / 10);
  126. }
  127. SelectObject(hDC, hBrushGreen);
  128. if (isOdd(t) && isEpeleptic3) {
  129. Ellipse(hDC, 7 * sx / 10 + sx / 20, 2 * sy / 10 + sy / 20, 9 * sx / 10 - sx / 20, 4 * sy / 10 - sy / 20);
  130. }
  131. else {
  132. Ellipse(hDC, 7 * sx / 10, 2 * sy / 10, 9 * sx / 10, 4 * sy / 10);
  133. }
  134. //end paint
  135. EndPaint(hWnd, &ps);
  136. break;
  137. case WM_TIMER:
  138. if (t >= INTERVAL)
  139. t = 0;
  140. if (isEpeleptic1) {
  141. InvalidateRgn(hWnd, ellipse1, true);
  142. }
  143. if (isEpeleptic2) {
  144. InvalidateRgn(hWnd, ellipse2, true);
  145. }
  146. if (isEpeleptic3) {
  147. InvalidateRgn(hWnd, ellipse3, true);
  148. }
  149. t++;
  150. break;
  151. case WM_KEYDOWN:
  152. {
  153. switch (wParam)
  154. {
  155. case VK_SPACE:
  156. {
  157. if (!paused) {
  158. KillTimer(hWnd, 1);
  159. paused = true;
  160. }
  161. else if (paused) {
  162. SetTimer(hWnd, 1, INTERVAL, NULL);
  163. paused = false;
  164. }
  165. break;
  166. }
  167. }
  168. break;
  169. }
  170. case WM_CLOSE:
  171. userReply = MessageBox(hWnd, "Are you sure?", "", MB_YESNO | MB_ICONQUESTION);
  172. if (IDYES == userReply)
  173. {
  174. DestroyWindow(hWnd);
  175. }
  176. break;
  177. case WM_DESTROY:
  178. //pen
  179. DeleteObject(hPenCommon);
  180. //brushes
  181. DeleteObject(hBrushBlack);
  182. DeleteObject(hBrushWhite);
  183. DeleteObject(hBrushRed);
  184. DeleteObject(hBrushYellow);
  185. DeleteObject(hBrushGreen);
  186. //delete regions
  187. DeleteObject(ellipse1);
  188. DeleteObject(ellipse2);
  189. DeleteObject(ellipse3);
  190. //delete timer
  191. KillTimer(hWnd, 1);
  192. PostQuitMessage(0);
  193. break;
  194. default:
  195. return DefWindowProc(hWnd, message, wParam, lParam);
  196. }
  197. return 0;
  198. }
  199.  
  200. bool isOdd(int i)
  201. {
  202. return i & 1;
  203. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement