怎么用多个定时器模拟交通灯(红,黄,绿),大侠们看下我的问题
step 1:
先在view里面定义了一个变量int iEntity;并在构造函数初始化为0
step 2:
int CMyGame4View::OnCreate(LPCREATESTRUCT lpCreateStruct)
{
if (CView::OnCreate(lpCreateStruct) == -1)
return -1;
// TODO: Add your specialized creation code here
SetTimer(11,25000,NULL);
SetTimer(22,1000,NULL);
SetTimer(33,25000,NULL);
return 0;
}
step 3:
void CMyGame4View::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
switch (nIDEvent)
{
case 11:
iEntity=1;
break;
case 22:
iEntity=2;
break;
case 33:
iEntity=3;
break;
default:
iEntity=0;
break;
}
Invalidate();
CView::OnTimer(nIDEvent);
}
step 4:
void CMyGame4View::OnDraw(CDC* pDC)
{
CMyGame4Doc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// TODO: add draw code for native data here
CBrush myBrush;
myBrush.CreateSolidBrush(RGB(255,255,255));
CBrush *pOldBrush=pDC->SelectObject(&myBrush);
CRect rect1;
GetClientRect(&rect1);
pDC->FillRect(rect1,&myBrush);
pDC->SelectObject(pOldBrush);
pDC->Rectangle(30,30,90,210);
pDC->Ellipse(40,40,80,80);
pDC->Ellipse(40,100,80,140);
pDC->Ellipse(40,160,80,200);
if (1 == iEntity)
{
CBrush myBrushRed;
myBrushRed.CreateSolidBrush(RGB(255,0,0));
CBrush *pOldBrushRed=pDC->SelectObject(&myBrushRed);
pDC->Ellipse(40,40,80,80);
pDC->SelectObject(pOldBrushRed);
}
else if (2 == iEntity)
{
CBrush myBrushYellow;
myBrushYellow.CreateSolidBrush(RGB(255,255,0));
CBrush *pOldBrushYellow=pDC->SelectObject(&myBrushYellow);
pDC->Ellipse(40,100,80,140);
pDC->SelectObject(pOldBrushYellow);
}
else if (3 == iEntity || 0 == iEntity)
{
CBrush myBrushGreen;
myBrushGreen.CreateSolidBrush(RGB(0,255,0));
CBrush *pOldBrushGreen=pDC->SelectObject(&myBrushGreen);
pDC->Ellipse(40,160,80,200);
pDC->SelectObject(pOldBrushGreen);
}
else
{
}
}
请大侠们帮忙看下,最主要不太懂在oncreate里面的settime是在窗口创建的时候就开始设置,都去响应ontimer,那在ondraw到底怎么画啊.如果多个定时器不能都在oncreate里面触发,应该怎么办? 说的比较乱,如果没听懂我的意思,只要能给出实现交通灯控制的就行。