Advertisement
Guest User

Untitled

a guest
Mar 27th, 2017
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.02 KB | None | 0 0
  1. #include "MsgHandlers.h"
  2. #include "Utils.h"
  3. #include <time.h>
  4. #include <math.h>
  5.  
  6. class CEllipse
  7. {
  8. private:
  9.  
  10. RECT mBoungingRect;
  11. int mLineWidth;
  12. COLORREF mLineColor;
  13. COLORREF mFillColor;
  14. HPEN mPen;
  15. HBRUSH mBrush;
  16.  
  17. public:
  18. //Atrybuty publiczne
  19. void setBoungingRect(const RECT &aBoungingRect);
  20. RECT getBoungingRect();
  21. // RECT getBoungingRect(RECT* aBoungingRect); //druga opcja
  22.  
  23. void setLineWidth(int aLineWidth);
  24. int setLineWidth();
  25.  
  26. void setLineColor(COLORREF aLineColor);
  27. COLORREF getLineColor();
  28.  
  29. void setFillColor(COLORREF aFillColor);
  30. COLORREF getFillColor();
  31.  
  32. //Metody publiczne
  33.  
  34. void Paint(HDC hdc);
  35.  
  36. void Tick();
  37.  
  38. //Konstruktor
  39. CEllipse();
  40. ~CEllipse(); //destruktor
  41. // zaimplementoiwac klase
  42. };
  43.  
  44. void CEllipse::setBoungingRect(const RECT &aBoungingRect)
  45. {
  46. mBoungingRect=aBoungingRect;
  47. }
  48. RECT CEllipse::getBoungingRect()
  49. {
  50. return mBoungingRect;
  51. }
  52. void CEllipse::setLineWidth(int aLineWidth)
  53. {
  54. mLineWidth=aLineWidth;
  55. }
  56. int CEllipse::setLineWidth()
  57. {
  58. return mLineWidth;
  59. }
  60. void CEllipse::setLineColor(COLORREF aLineColor)
  61. {
  62. mLineColor=aLineColor;
  63. }
  64. COLORREF CEllipse::getLineColor()
  65. {
  66. return mLineColor;
  67. }
  68. void CEllipse::setFillColor(COLORREF aFillColor)
  69. {
  70. mFillColor=aFillColor;
  71. }
  72. COLORREF CEllipse::getFillColor()
  73. {
  74. return mFillColor;
  75. }
  76. void CEllipse::Paint(HDC hdc)
  77. {
  78. mPen = (HPEN)GetCurrentObject(hdc, OBJ_PEN);
  79. mBrush = (HBRUSH)GetCurrentObject(hdc, OBJ_BRUSH);
  80.  
  81. Rectangle(hdc,mBoungingRect.left,mBoungingRect.top,mBoungingRect.right,mBoungingRect.bottom);
  82. }
  83. void CEllipse::Tick()
  84. {
  85. mBoungingRect.left += RandRange(-5,5);
  86. mBoungingRect.top += RandRange(-5,5);
  87. mBoungingRect.right += RandRange(-5,5);
  88. mBoungingRect.bottom += RandRange(-5,5);
  89.  
  90. }
  91.  
  92. CEllipse::CEllipse()
  93. {
  94. mBrush = CreateSolidBrush(mFillColor);
  95. mPen = CreatePen(PS_SOLID, mLineWidth, mLineColor);
  96. }
  97.  
  98. CEllipse::~CEllipse()
  99. {
  100. DeleteObject(mPen);
  101. DeleteObject(mBrush);
  102. }
  103.  
  104.  
  105. //m=MyRect.setLinewith(3); wywyołanie lub int w= MyRect.getLineWith(); set przypisanie, get wywolanie
  106. /************************ Zmienne globalne ************************/
  107.  
  108. int ClientWidth, ClientHeight;
  109. BOOL EraseBkgnd = TRUE;
  110.  
  111. RECT MouseRect;
  112. bool InDrawState = false;
  113.  
  114. const int MyShapesCount = 3;
  115. RECT MyShapes[MyShapesCount];
  116.  
  117. int CurrDrawnShapeIdx = 0;
  118.  
  119. HPEN PenRect[MyShapesCount], PenEllps[MyShapesCount];
  120. HBRUSH BrushRect[MyShapesCount], BrushEllps[MyShapesCount];
  121.  
  122.  
  123. /******************** Procedury obsługi zdarzeń ********************/
  124.  
  125. //Procedura wywoływana przed pokazaniem okna (na początku działania programu).
  126. //Tutaj można przeprowadzić początkową inicjalizację zmiennych, obiektów itp.
  127. void OnCreate(HWND hwnd)
  128. {
  129. /* srand(time(NULL));
  130. for (int i=0;i<MyShapesCount; i++)
  131. {
  132. PenRect[i] = CreatePen(PS_SOLID, 3, RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255)));
  133. PenEllps[i] = CreatePen(PS_SOLID, 3, RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255)));
  134. BrushRect[i] = CreateSolidBrush(RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255))));
  135. BrushEllps[i] = CreateSolidBrush(RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255))));
  136. }
  137. SetTimer(hwnd, 1, 25, NULL);
  138. */
  139.  
  140. }
  141.  
  142.  
  143. //Procedura wywoływana po każdej zmianie rozmiaru okna
  144. void OnSize(HWND hwnd, int width, int height, WPARAM wParam)
  145. {
  146. ClientWidth = width;
  147. ClientHeight = height;
  148. InvalidateRect(hwnd, NULL, EraseBkgnd);
  149. }
  150.  
  151.  
  152. //Procedura wywoływana co 25 ms (czas ustawia się trzecim argumentem
  153. // funkcji SetTimer() wywoływanej w OnCreate).
  154. void OnTimer(HWND hwnd, WPARAM timerID)
  155. {
  156. for(int i = 0; i < MyShapesCount; i++)
  157. {
  158. MyShapes[i].left += RandRange(-5,5);
  159. MyShapes[i].top += RandRange(-5,5);
  160. MyShapes[i].right += RandRange(-5,5);
  161. MyShapes[i].bottom += RandRange(-5,5);
  162. }
  163.  
  164. InvalidateRect(hwnd, NULL, EraseBkgnd);
  165. }
  166.  
  167.  
  168. //Procedura wywoływana w celu odświeżenia zawartości okna.
  169. //Wyłącznie tutaj powinno się przeprowadzać rysowanie.
  170. void OnPaint(HWND hwnd, HDC hdc)
  171. {
  172.  
  173.  
  174. /*HPEN oldPen;
  175. HBRUSH oldBrush;
  176.  
  177. oldPen = (HPEN)GetCurrentObject(hdc, OBJ_PEN);
  178. oldBrush = (HBRUSH)GetCurrentObject(hdc, OBJ_BRUSH);
  179.  
  180. for(int i = 0; i < MyShapesCount; i++)
  181. {
  182. SelectObject(hdc, PenRect[i]);
  183. SelectObject(hdc, BrushRect[i]);
  184.  
  185. Rectangle(hdc,
  186. MyShapes[i].left,
  187. MyShapes[i].top,
  188. MyShapes[i].right,
  189. MyShapes[i].bottom);
  190.  
  191. SelectObject(hdc, PenEllps[i]);
  192. SelectObject(hdc, BrushEllps[i]);
  193.  
  194. Ellipse(hdc,
  195. MyShapes[i].left,
  196. MyShapes[i].top,
  197. MyShapes[i].right,
  198. MyShapes[i].bottom);
  199. }
  200.  
  201. SelectObject(hdc, oldPen);
  202. SelectObject(hdc, oldBrush);*/
  203. }
  204.  
  205.  
  206. //Procedura wywoływana po naciśnięciu klawisza klawiatury.
  207. void OnKeyDown(HWND hwnd, WPARAM keyCode)
  208. {
  209. switch (keyCode)
  210. {
  211. case VK_LEFT:
  212. break;
  213. case VK_UP:
  214. break;
  215. case VK_RIGHT:
  216. break;
  217. case VK_DOWN:
  218. break;
  219. case 0x43: //klawisz C
  220. break;
  221. case 0x4F: //klawisz O
  222. EraseBkgnd ^= 0x00000001;
  223. break;
  224. case 0x52: //klawisz R
  225. break;
  226. case 0x53: //klawisz S
  227. break;
  228. }
  229. //InvalidateRect(hwnd, NULL, EraseBkgnd);
  230. }
  231.  
  232.  
  233. //Procedura wywoływana po poruszeniu wskaźnika myszy.
  234. void OnMouseMove(HWND hwnd, int x, int y, WPARAM wParam)
  235. {
  236. if(InDrawState)
  237. {
  238. MouseRect.right = x;
  239. MouseRect.bottom = y;
  240.  
  241. MyShapes[CurrDrawnShapeIdx] = MouseRect;
  242. CorrectRect(&MyShapes[CurrDrawnShapeIdx]);
  243.  
  244. InvalidateRect(hwnd, NULL, EraseBkgnd);
  245. }
  246. }
  247.  
  248.  
  249. //Procedura wywoływana po naciśnięciu lewego przycisku myszy.
  250. void OnLButtonDown(HWND hwnd, int x, int y, WPARAM wParam)
  251. {
  252. if(!InDrawState)
  253. {
  254. MouseRect.left = x;
  255. MouseRect.top = y;
  256. MouseRect.right = x;
  257. MouseRect.bottom = y;
  258.  
  259. MyShapes[CurrDrawnShapeIdx] = MouseRect;
  260. CorrectRect(&MyShapes[CurrDrawnShapeIdx]);
  261.  
  262. InDrawState = true;
  263. SetCapture(hwnd);
  264. }
  265. }
  266.  
  267.  
  268. //Procedura wywoływana po zwolnieniu lewego przycisku myszy.
  269. void OnLButtonUp(HWND hwnd, int x, int y, WPARAM wParam)
  270. {
  271. if(InDrawState)
  272. {
  273. MouseRect.right = x;
  274. MouseRect.bottom = y;
  275.  
  276. MyShapes[CurrDrawnShapeIdx] = MouseRect;
  277. CorrectRect(&MyShapes[CurrDrawnShapeIdx]);
  278. CurrDrawnShapeIdx = (CurrDrawnShapeIdx+1) % MyShapesCount;
  279.  
  280. InDrawState = false;
  281. ReleaseCapture();
  282. InvalidateRect(hwnd, NULL, EraseBkgnd);
  283. }
  284. }
  285.  
  286.  
  287. //Procedura wywoływana przed zniszczeniem okna (przed końcem działania programu).
  288. //Tutaj można zwolnić zaalokowane zasoby.
  289. void OnDestroy(HWND hwnd)
  290. {
  291. KillTimer(hwnd, 1);
  292.  
  293. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement