Guest User

Untitled

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