Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "MsgHandlers.h"
- #include "Utils.h"
- #include <time.h>
- #include <math.h>
- class CEllipse
- {
- private:
- RECT mBoungingRect;
- int mLineWidth;
- COLORREF mLineColor;
- COLORREF mFillColor;
- HPEN mPen;
- HBRUSH mBrush;
- public:
- //Atrybuty publiczne
- void setBoungingRect(const RECT &aBoungingRect);
- RECT getBoungingRect();
- // RECT getBoungingRect(RECT* aBoungingRect); //druga opcja
- void setLineWidth(int aLineWidth);
- int setLineWidth();
- void setLineColor(COLORREF aLineColor);
- COLORREF getLineColor();
- void setFillColor(COLORREF aFillColor);
- COLORREF getFillColor();
- //Metody publiczne
- void Paint(HDC hdc);
- void Tick();
- //Konstruktor
- CEllipse();
- ~CEllipse(); //destruktor
- // zaimplementoiwac klase
- };
- void CEllipse::setBoungingRect(const RECT &aBoungingRect)
- {
- mBoungingRect=aBoungingRect;
- }
- RECT CEllipse::getBoungingRect()
- {
- return mBoungingRect;
- }
- void CEllipse::setLineWidth(int aLineWidth)
- {
- mLineWidth=aLineWidth;
- }
- int CEllipse::setLineWidth()
- {
- return mLineWidth;
- }
- void CEllipse::setLineColor(COLORREF aLineColor)
- {
- mLineColor=aLineColor;
- }
- COLORREF CEllipse::getLineColor()
- {
- return mLineColor;
- }
- void CEllipse::setFillColor(COLORREF aFillColor)
- {
- mFillColor=aFillColor;
- }
- COLORREF CEllipse::getFillColor()
- {
- return mFillColor;
- }
- void CEllipse::Paint(HDC hdc)
- {
- mPen = (HPEN)GetCurrentObject(hdc, OBJ_PEN);
- mBrush = (HBRUSH)GetCurrentObject(hdc, OBJ_BRUSH);
- Rectangle(hdc,mBoungingRect.left,mBoungingRect.top,mBoungingRect.right,mBoungingRect.bottom);
- }
- void CEllipse::Tick()
- {
- mBoungingRect.left += RandRange(-5,5);
- mBoungingRect.top += RandRange(-5,5);
- mBoungingRect.right += RandRange(-5,5);
- mBoungingRect.bottom += RandRange(-5,5);
- }
- CEllipse::CEllipse()
- {
- mBrush = CreateSolidBrush(mFillColor);
- mPen = CreatePen(PS_SOLID, mLineWidth, mLineColor);
- }
- CEllipse::~CEllipse()
- {
- DeleteObject(mPen);
- DeleteObject(mBrush);
- }
- //m=MyRect.setLinewith(3); wywyołanie lub int w= MyRect.getLineWith(); set przypisanie, get wywolanie
- /************************ Zmienne globalne ************************/
- int ClientWidth, ClientHeight;
- BOOL EraseBkgnd = TRUE;
- RECT MouseRect;
- bool InDrawState = false;
- const int MyShapesCount = 3;
- RECT MyShapes[MyShapesCount];
- int CurrDrawnShapeIdx = 0;
- HPEN PenRect[MyShapesCount], PenEllps[MyShapesCount];
- HBRUSH BrushRect[MyShapesCount], BrushEllps[MyShapesCount];
- /******************** Procedury obsługi zdarzeń ********************/
- //Procedura wywoływana przed pokazaniem okna (na początku działania programu).
- //Tutaj można przeprowadzić początkową inicjalizację zmiennych, obiektów itp.
- void OnCreate(HWND hwnd)
- {
- /* srand(time(NULL));
- for (int i=0;i<MyShapesCount; i++)
- {
- PenRect[i] = CreatePen(PS_SOLID, 3, RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255)));
- PenEllps[i] = CreatePen(PS_SOLID, 3, RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255)));
- BrushRect[i] = CreateSolidBrush(RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255))));
- BrushEllps[i] = CreateSolidBrush(RGB(RandRange(0,255),RandRange(0,255),RandRange(0,255))));
- }
- SetTimer(hwnd, 1, 25, NULL);
- */
- }
- //Procedura wywoływana po każdej zmianie rozmiaru okna
- void OnSize(HWND hwnd, int width, int height, WPARAM wParam)
- {
- ClientWidth = width;
- ClientHeight = height;
- InvalidateRect(hwnd, NULL, EraseBkgnd);
- }
- //Procedura wywoływana co 25 ms (czas ustawia się trzecim argumentem
- // funkcji SetTimer() wywoływanej w OnCreate).
- void OnTimer(HWND hwnd, WPARAM timerID)
- {
- for(int i = 0; i < MyShapesCount; i++)
- {
- MyShapes[i].left += RandRange(-5,5);
- MyShapes[i].top += RandRange(-5,5);
- MyShapes[i].right += RandRange(-5,5);
- MyShapes[i].bottom += RandRange(-5,5);
- }
- InvalidateRect(hwnd, NULL, EraseBkgnd);
- }
- //Procedura wywoływana w celu odświeżenia zawartości okna.
- //Wyłącznie tutaj powinno się przeprowadzać rysowanie.
- void OnPaint(HWND hwnd, HDC hdc)
- {
- /*HPEN oldPen;
- HBRUSH oldBrush;
- oldPen = (HPEN)GetCurrentObject(hdc, OBJ_PEN);
- oldBrush = (HBRUSH)GetCurrentObject(hdc, OBJ_BRUSH);
- for(int i = 0; i < MyShapesCount; i++)
- {
- SelectObject(hdc, PenRect[i]);
- SelectObject(hdc, BrushRect[i]);
- Rectangle(hdc,
- MyShapes[i].left,
- MyShapes[i].top,
- MyShapes[i].right,
- MyShapes[i].bottom);
- SelectObject(hdc, PenEllps[i]);
- SelectObject(hdc, BrushEllps[i]);
- Ellipse(hdc,
- MyShapes[i].left,
- MyShapes[i].top,
- MyShapes[i].right,
- MyShapes[i].bottom);
- }
- SelectObject(hdc, oldPen);
- SelectObject(hdc, oldBrush);*/
- }
- //Procedura wywoływana po naciśnięciu klawisza klawiatury.
- void OnKeyDown(HWND hwnd, WPARAM keyCode)
- {
- switch (keyCode)
- {
- case VK_LEFT:
- break;
- case VK_UP:
- break;
- case VK_RIGHT:
- break;
- case VK_DOWN:
- break;
- case 0x43: //klawisz C
- break;
- case 0x4F: //klawisz O
- EraseBkgnd ^= 0x00000001;
- break;
- case 0x52: //klawisz R
- break;
- case 0x53: //klawisz S
- break;
- }
- //InvalidateRect(hwnd, NULL, EraseBkgnd);
- }
- //Procedura wywoływana po poruszeniu wskaźnika myszy.
- void OnMouseMove(HWND hwnd, int x, int y, WPARAM wParam)
- {
- if(InDrawState)
- {
- MouseRect.right = x;
- MouseRect.bottom = y;
- MyShapes[CurrDrawnShapeIdx] = MouseRect;
- CorrectRect(&MyShapes[CurrDrawnShapeIdx]);
- InvalidateRect(hwnd, NULL, EraseBkgnd);
- }
- }
- //Procedura wywoływana po naciśnięciu lewego przycisku myszy.
- void OnLButtonDown(HWND hwnd, int x, int y, WPARAM wParam)
- {
- if(!InDrawState)
- {
- MouseRect.left = x;
- MouseRect.top = y;
- MouseRect.right = x;
- MouseRect.bottom = y;
- MyShapes[CurrDrawnShapeIdx] = MouseRect;
- CorrectRect(&MyShapes[CurrDrawnShapeIdx]);
- InDrawState = true;
- SetCapture(hwnd);
- }
- }
- //Procedura wywoływana po zwolnieniu lewego przycisku myszy.
- void OnLButtonUp(HWND hwnd, int x, int y, WPARAM wParam)
- {
- if(InDrawState)
- {
- MouseRect.right = x;
- MouseRect.bottom = y;
- MyShapes[CurrDrawnShapeIdx] = MouseRect;
- CorrectRect(&MyShapes[CurrDrawnShapeIdx]);
- CurrDrawnShapeIdx = (CurrDrawnShapeIdx+1) % MyShapesCount;
- InDrawState = false;
- ReleaseCapture();
- InvalidateRect(hwnd, NULL, EraseBkgnd);
- }
- }
- //Procedura wywoływana przed zniszczeniem okna (przed końcem działania programu).
- //Tutaj można zwolnić zaalokowane zasoby.
- void OnDestroy(HWND hwnd)
- {
- KillTimer(hwnd, 1);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement