Witam wszystkich, napisałem w Visual Studio mały programik zawierający klasę która przechowuje obiekt, jego właściwości i go wyrysowuje na ekran. oto kod z głównego pliku:
#include "stdafx.h"
#include "MyModule.h"
#include "CCircle.h"
CCircle gCircle;
//Property Access Methods
POINT CCircle::GetCenterPos()
{
POINT centerPos;
centerPos.x = (mBoundingRect.left + mBoundingRect.right) / 2;
centerPos.y = (mBoundingRect.bottom + mBoundingRect.top) / 2;
return centerPos;
}
int CCircle::GetRadius()
{
return (mBoundingRect.right - mBoundingRect.left) / 2;
}
int CCircle::GetPenWidth()
{
return mPenWidth;
}
COLORREF CCircle::GetPenColor()
{
return mPenColor;
}
COLORREF CCircle::GetFillColor()
{
return mFillColor;
}
RECT CCircle::GetClientR()
{
return mClientRect;
}
void CCircle::SetCenterPos(POINT aCenterPos)
{
int radius;
radius = GetRadius();
mBoundingRect.left = aCenterPos.x - radius;
mBoundingRect.right = aCenterPos.x + radius;
mBoundingRect.top = aCenterPos.y - radius;
mBoundingRect.bottom = aCenterPos.y + radius;
}
void CCircle::SetRadius(int aRadius)
{
POINT centerPos = GetCenterPos();
mBoundingRect.left = centerPos.x - aRadius;
mBoundingRect.right = centerPos.x + aRadius;
mBoundingRect.top = centerPos.y - aRadius;
mBoundingRect.bottom = centerPos.y + aRadius;
}
void CCircle::SetPenWidth(int aPenWidth)
{
mPenWidth = aPenWidth;
delete mPen;
mPen = new HPEN;
*mPen=CreatePen(PS_SOLID,mPenWidth,mPenColor);
}
void CCircle::SetPenColor(COLORREF aPenColor)
{
mPenColor = aPenColor;
delete mPen;
mPen = new HPEN;
*mPen=CreatePen(PS_SOLID,mPenWidth,mPenColor);
}
void CCircle::SetFillColor(COLORREF aFillColor)
{
mFillColor = aFillColor;
delete mBrush;
mBrush = new HBRUSH;
*mBrush = CreateSolidBrush(mFillColor);
}
void CCircle::SetClientR(HWND &ahwnd) //pobiera aktualny rozmiar okna
{
GetClientRect(ahwnd, &mClientRect);
}
//Public Methods
void CCircle::Paint(HDC ahdc)
{
HPEN oldPen;
HBRUSH oldBrush;
oldPen = (HPEN)SelectObject(ahdc,*mPen);
oldBrush = (HBRUSH)SelectObject(ahdc, *mBrush);
Rectangle(ahdc, mBoundingRect.left + 50, mBoundingRect.top + 50, mBoundingRect.right + 50, mBoundingRect.bottom + 50);
Ellipse(ahdc, mBoundingRect.left, mBoundingRect.top, mBoundingRect.right, mBoundingRect.bottom);
SelectObject(ahdc,oldPen);
SelectObject(ahdc,oldBrush);
}
void CCircle::Move(int aXoffset, int aYoffset)
{
POINT centerPos;
centerPos = GetCenterPos();
centerPos.x = centerPos.x + aXoffset;
centerPos.y = centerPos.y + aYoffset;
SetCenterPos(centerPos);
}
void CCircle::Collision(int aSpeed)
{
}
//Constructors and Destructors
CCircle::CCircle()
{
mBoundingRect.bottom = 0;
mBoundingRect.right = 50;
mBoundingRect.left = 0;
mBoundingRect.top = 50;
mPenWidth = 10;
mPenColor = RGB(226,192,114);
mFillColor = RGB(181,121,217);
mPen = new HPEN;
mBrush = new HBRUSH;
*mPen = CreatePen(PS_SOLID,mPenWidth,mPenColor);
*mBrush = CreateSolidBrush(mFillColor);
}
CCircle::~CCircle()
{
delete mPen;
delete mBrush;
}
void OnPaint(HDC &hdc, HWND &hWnd) //rysowanie na ekranie
{
gCircle.Paint(hdc);
/*HPEN myPen,oldPen,InnyPen;
HBRUSH myBrush,oldBrush,InnyBrush;
RECT textRect,textShadowRect;
InnyPen = CreatePen(PS_SOLID,10,RGB(23,200,23));
InnyBrush = CreateSolidBrush(RGB(67,43,23));
myPen = CreatePen(PS_SOLID,5,RGB(255,0,0));
oldPen = (HPEN)SelectObject(hdc,myPen);
myBrush = CreateSolidBrush(RGB(0,0,255));
oldBrush = (HBRUSH)SelectObject(hdc,myBrush);
SetRect(&textRect,60,20,10,10);
DrawText(hdc,TEXT("Programowanie Obiektowe, Śr/TP 9.15"),-1,&textRect, DT_TOP | DT_NOCLIP);
SetRect(&textShadowRect,62,22,12,12);
DrawShadowText(hdc,TEXT("Programowanie"),-1,&textShadowRect,123124 ,RGB(250,0,0),RGB(0,0,255),10,10);
SelectObject(hdc,myPen);
SelectObject(hdc,myBrush);
Rectangle(hdc,50,100,150,200);
SelectObject(hdc,InnyPen);
SelectObject(hdc,InnyBrush);
Ellipse(hdc,300,250,500,50);
SelectObject(hdc,oldPen);
SelectObject(hdc,oldBrush);
Rectangle(hdc,40,90,140,190);
DeleteObject(myPen);
DeleteObject(myBrush);*/
}
void OnKeydown(HWND &hWnd, WPARAM &wParam) //poruszanie przedmiotem
{
switch(wParam)
{
case VK_RIGHT:
gCircle.Move(10,0);
break;
case VK_LEFT:
gCircle.Move(-10,0);
break;
case VK_UP:
gCircle.Move(0,-10);
break;
case VK_DOWN:
gCircle.Move(0,10);
break;
}
InvalidateRect(hWnd,0,false); //gdy damy true bedzie normalnie sie przemeszczac
UpdateWindow(hWnd);
}
void OnTime(HWND &hWnd)
{
gCircle.Move(1,5);
Sleep(44);
InvalidateRect(hWnd,0,true); //gdy damy false bedzie zamalowywac
}
jak zauważycie na ekranie wyrysowują się dwa elementy elipsa i kwadrat oraz że spadają, poruszają się one automatycznie w pewnym kierunki. Problem mam taki, że chciałbym zrobić by obiekt po dotknięciu ramki nie wyszedł za nią tylko się od niej odbił oraz chciałbym zrobić żeby każdy element żył swoim życiem tzn. by leciał w inną stronę niż drugi.Mogę liczyć na pomoc?
Pozdrawiam,WIzard