mfc单文档程序模拟红绿灯 我设置了一个开始的按钮当按下按钮时,出现红绿灯的效果,
在start中使用了一个faketime的变量,当除3余2的时候刷新绿灯位置,在红灯位置绘制红灯,当除3余1的时候刷新红灯位置,在黄灯位置绘制黄灯,当整除3时,刷新黄灯位...
在start中使用了一个faketime的变量,当除3余2的时候刷新绿灯位置,在红灯位置绘制红灯,当除3余1的时候刷新红灯位置,在黄灯位置绘制黄灯,当整除3时,刷新黄灯位置,在绿灯位置绘制绿灯,可是出来的结果只有不间断闪烁的绿灯
void CCornerView::OnStart()
{
// TODO: Add your command handler code here
SetTimer(1,2000,NULL);
faketime=0;}
void CCornerView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
faketime=faketime+2;
CView::OnTimer(nIDEvent);
}
void CCornerView::OnDraw(CDC* pDC)
{
CCornerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC=GetDC();
if(faketime%3==2)
{ CRect rect;
rect.left=750;
rect.right=780;
rect.top=230;
rect.bottom=270;
InvalidateRect(&rect);
CBrush brushRed(RGB(255,0,0));
CBrush* pOldBrush = pDC->SelectObject(&brushRed);
// draw a thick black rectangle filled with blue
pDC->Ellipse(750,130,780,170);
// put back the old objects
pDC->SelectObject(pOldBrush);
}
else if(faketime%3==1)
{
CRect rect;
rect.left=750;
rect.right=780;
rect.top=130;
rect.bottom=170;
InvalidateRect(&rect);
CBrush brushYellow(RGB(255,215,0 ));
CBrush* pOldBrush = pDC->SelectObject(&brushYellow);
// draw a thick black rectangle filled with blue
pDC->Ellipse(750,180,780,220);
// put back the old objects
pDC->SelectObject(pOldBrush);
}
else if(faketime%3==0)
{
CRect rect;
rect.left=750;
rect.right=780;
rect.top=180;
rect.bottom=220;
InvalidateRect(&rect);
CBrush brushGreen(RGB(0,255,0));
CBrush* pOldBrush = pDC->SelectObject(&brushGreen);
// draw a thick black rectangle filled with blue
pDC->Ellipse(750,230,780,270);
// put back the old objects
pDC->SelectObject(pOldBrush);
}
// TODO: add draw code for native data here
} 展开
void CCornerView::OnStart()
{
// TODO: Add your command handler code here
SetTimer(1,2000,NULL);
faketime=0;}
void CCornerView::OnTimer(UINT nIDEvent)
{
// TODO: Add your message handler code here and/or call default
faketime=faketime+2;
CView::OnTimer(nIDEvent);
}
void CCornerView::OnDraw(CDC* pDC)
{
CCornerDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
pDC=GetDC();
if(faketime%3==2)
{ CRect rect;
rect.left=750;
rect.right=780;
rect.top=230;
rect.bottom=270;
InvalidateRect(&rect);
CBrush brushRed(RGB(255,0,0));
CBrush* pOldBrush = pDC->SelectObject(&brushRed);
// draw a thick black rectangle filled with blue
pDC->Ellipse(750,130,780,170);
// put back the old objects
pDC->SelectObject(pOldBrush);
}
else if(faketime%3==1)
{
CRect rect;
rect.left=750;
rect.right=780;
rect.top=130;
rect.bottom=170;
InvalidateRect(&rect);
CBrush brushYellow(RGB(255,215,0 ));
CBrush* pOldBrush = pDC->SelectObject(&brushYellow);
// draw a thick black rectangle filled with blue
pDC->Ellipse(750,180,780,220);
// put back the old objects
pDC->SelectObject(pOldBrush);
}
else if(faketime%3==0)
{
CRect rect;
rect.left=750;
rect.right=780;
rect.top=180;
rect.bottom=220;
InvalidateRect(&rect);
CBrush brushGreen(RGB(0,255,0));
CBrush* pOldBrush = pDC->SelectObject(&brushGreen);
// draw a thick black rectangle filled with blue
pDC->Ellipse(750,230,780,270);
// put back the old objects
pDC->SelectObject(pOldBrush);
}
// TODO: add draw code for native data here
} 展开
1个回答
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询