问题描述
- 做一个圆在按键按下后向右移动,放开后停止。每秒刷新一次。功能不对求教
- 代码如下。 void CHomeWorkView::OnTimer(UINT_PTR nIDEvent)
{
if (nIDEvent == 102)
{
Invalidate();
}
CView::OnTimer(nIDEvent);
}void CHomeWorkView::OnKeyDown(UINT nChar UINT nRepCnt UINT nFlags)
{
CDC* pDC=GetDC();
CPoint m_lStartPoint(10 10) m_rStartPoint(100 100);
pDC->Ellipse(10 10 100 100);
m_KeyDown = false;
SetTimer(102 1000 NULL);
if (nChar == 39)
{
m_KeyDown = true;
while(m_KeyDown)
{
pDC->Ellipse(m_lStartPoint.x += 10 m_lStartPoint.y m_rStartPoint.x += 10 m_rStartPoint.y);
}
}CView::OnKeyDown(nChar nRepCnt nFlags);
}
void CHomeWorkView::OnKeyUp(UINT nChar UINT nRepCnt UINT nFlags)
{
if (nChar==39)
{
m_KeyDown = false;
}CView::OnKeyUp(nChar nRepCnt nFlags);
}
解决方案
定义一个全局变量offset
在按键函数中调用offset++;
在定时器中调用OnDraw
在OnDraw里Ellipse,x+offset
时间: 2024-12-02 08:14:39