Hi,
I have written a code to perform animation using Visual Studio 2008. It has got mountains, a road and a car. The car has to travel on the road. But when i execute the program a grey window slowly captures my screen. Due to large size i am skipping the default code.
The *view.cpp file is:
IMPLEMENT_DYNCREATE(CAnimationCarView, CView)
BEGIN_MESSAGE_MAP(CAnimationCarView, CView)
ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint)
ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CView::OnFilePrintPreview)
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_WM_CREATE()
END_MESSAGE_MAP()
CAnimationCarView::CAnimationCarView()
{
i=150,j=190,k=145,l=140,m=138,n=195,o=197,p=170,q=148,r=185;
penGrey.CreatePen(PS_SOLID, 5, RGB(128,128,128)); penLGrey.CreatePen(PS_SOLID,5, RGB(220,220,220)); penBlack.CreatePen(PS_SOLID, 5, RGB(0,0,0));
pOldPen=NULL;
brGrey.CreateSolidBrush(RGB(128, 128, 128));
brLGrey.CreateSolidBrush(RGB(220,220,220));
brBlack.CreateSolidBrush(RGB(0, 0, 0));
pOldBrush=NULL;
}
void CAnimationCarView::OnDraw(CDC* pDC)
{
CAnimationCarDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
CBitmap XMap,*pOldMap;
CRect rc;
GetClientRect(&rc);
CBrush brBackground( RGB( 255, 255, 255 ) );
XMap.CreateCompatibleBitmap(pDC,rc.Width(), rc.Height());
CDC WorkDC;
CDC *MyDC=&WorkDC;
MyDC->CreateCompatibleDC(pDC);
MyDC->FillRect(rc, &brBackground);
pOldMap=MyDC->SelectObject(&XMap);
road(MyDC);
mountain(MyDC);
stationary_car(MyDC);
pDC->BitBlt(0, 0, rc.Width(), rc.Height(), MyDC, 0, 0, SRCCOPY);
MyDC->SelectObject(pOldMap);
XMap.DeleteObject();
MyDC->DeleteDC();
}
void CAnimationCarView::mountain(CDC* pDC)
{
CPen penBrown(PS_SOLID, 5, RGB(150,75,0));
CPen* pOldPen=NULL;
pOldPen=pDC->SelectObject(&penBrown);
pDC->MoveTo(0,250);
pDC->LineTo(700,250);
pDC->MoveTo(0,250);
pDC->LineTo(0,160);
pDC->MoveTo(0,160);
pDC->LineTo(30,200);
pDC->MoveTo(30,200);
pDC->LineTo(80,120);
pDC->AngleArc(90,124,10,30,150);
pDC->MoveTo(99,119);
pDC->LineTo(150,180);
pDC->MoveTo(150,180);
pDC->LineTo(220,100);
pDC->AngleArc(234,106,15,30,150);
pDC->MoveTo(248,99);
pDC->LineTo(320,200);
pDC->MoveTo(320,200);
pDC->LineTo(370,125);
pDC->AngleArc(380,129,10,30,150);
pDC->MoveTo(389,124);
pDC->LineTo(430,180);
pDC->MoveTo(430,180);
pDC->LineTo(505,90);
pDC->AngleArc(519,96,15,30,150);
pDC->MoveTo(533,89);
pDC->LineTo(605,200);
pDC->MoveTo(605,200);
pDC->LineTo(700,170);
pDC->MoveTo(700,170);
pDC->LineTo(700,250);
CBrush brBrown(RGB(150, 75, 0));
CBrush* pOldBrush=NULL;
pOldBrush=pDC->SelectObject(&brBrown);
pDC->FloodFill(10,245,RGB(150,75,0));
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
void CAnimationCarView::road(CDC* pDC){
CPoint poly6[8]={CPoint(0,400), CPoint(210,400),CPoint(210,440),CPoint(0,440)};
CPen pen(PS_SOLID, 5, RGB(220,220,220));
CPen* pOldPen=NULL;
pOldPen=pDC->SelectObject(&pen);
CBrush br(RGB(220, 220, 220));
CBrush* pOldBrush=NULL;
pOldBrush=pDC->SelectObject(&br);
pDC->Polygon(poly6, 4);
pDC->SelectObject(pOldBrush);
pDC->SelectObject(pOldPen);
}
void CAnimationCarView::stationary_car(CDC* pDC)
{
CBitmap XMap,*pOldMap;
CPen* pOldPen=NULL;
pOldPen=pDC->SelectObject(&penGrey);
SIZE Size;
Size.cx = GetSystemMetrics(SM_CXSCREEN);
Size.cy = GetSystemMetrics(SM_CYSCREEN);
XMap.CreateCompatibleBitmap(pDC,Size.cx,Size.cy);
CDC WorkDC;
CDC *MyDC=&WorkDC;
MyDC->CreateCompatibleDC(pDC);
pOldMap=MyDC->SelectObject(&XMap);
MyDC->MoveTo(i,402);
MyDC->LineTo(j,402);
MyDC->MoveTo(j,415);
MyDC->LineTo(k,415);
MyDC->MoveTo(j,402);
MyDC->LineTo(j,415);
MyDC->MoveTo(k,415);
MyDC->LineTo(i,402);
CBrush* pOldBrush=NULL;
pOldBrush=MyDC->SelectObject(&brGrey);
MyDC->FloodFill(p,408,RGB(128,128,128));
MyDC->MoveTo(n,415);
MyDC->LineTo(o,428);
MyDC->MoveTo(l,415);
MyDC->LineTo(m,428);
MyDC->MoveTo(l,415);
MyDC->LineTo(n,415);
MyDC->MoveTo(o,428);
MyDC->LineTo(m,428);
MyDC->FloodFill(p,422,RGB(128,128,128));
MyDC->SelectObject(pOldBrush);
MyDC->SelectObject(pOldPen);
CBrush brBlack(RGB(0, 0, 0));
pOldBrush=NULL;
pOldBrush=MyDC->SelectObject(&brBlack);
pOldPen=NULL;
pOldPen=MyDC->SelectObject(&penBlack);
MyDC->MoveTo(r,434);
MyDC->AngleArc(q,434,5,0,360);
MyDC->FloodFill(q,434,RGB(0,0,0));
MyDC->AngleArc(r,434,5,0,360);
pDC->BitBlt(j,k,Size.cx,Size.cy,MyDC,0,0,SRCCOPY);
MyDC->SelectObject(pOldPen);
MyDC->SelectObject(pOldBrush);
MyDC->SelectObject(pOldMap);
XMap.DeleteObject();
MyDC->DeleteDC();
}
void CAnimationCarView::OnTimer(UINT_PTR nIDEvent)
{
if(i>0){
i=i-3;
Invalidate();
j=j-3;k=k-3;l=l-3;m=m-3;n=n-3;o=o-3;p=p-3;q=q-3;r=r-3;
}
CView::OnTimer(nIDEvent);
}
BOOL CAnimationCarView::OnEraseBkgnd(CDC* pDC)
{
return TRUE;
return CView::OnEraseBkgnd(pDC);
}
int CAnimationCarView::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
SetTimer(1,200,NULL);
return 0;
}
The header file is:
#pragma once
class CAnimationCarView : public CView
{
protected: CAnimationCarView();
DECLARE_DYNCREATE(CAnimationCarView)
public:
CAnimationCarDoc* GetDocument() const;
int i,j,k,l,m,n,o,p,q,r;
CPen penGrey, penLGrey, penBlack;
CPen *pOldPen;
CBrush brGrey, brLGrey, brBlack;
CBrush* pOldBrush;
public:
void mountain(CDC*);
void road(CDC*);
void stationary_car(CDC*);
public:
virtual void OnDraw(CDC* pDC); virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnTimer(UINT_PTR nIDEvent);
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct);
};
#ifndef _DEBUG // debug version in AnimationCarView.cpp
inline CAnimationCarDoc* CAnimationCarView::GetDocument() const
{ return reinterpret_cast<CAnimationCarDoc*>(m_pDocument); }
#endif
Somebody plz guide me.
Zulf.