VC++ mfc中怎么修改此代码void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; pDC=GetDC(); color=RGB(rand()%6,rand()%6,rand()%6); r=rand()F+5; br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(center.x-r,center.y-r,center.x+r,center.y+r); br.DeleteObject(); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default SetTimer(1,200,NULL); CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { center.y-=5; pDC=GetDC(); br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(center.x-r,center.y-r,center.x+r,center.y+r); br.DeleteObject(); } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default RedrawWindow(); rise(); 能实现前面左击鼠标出现的圆不会因为后面再左击鼠标所出现的圆而消失,而是继续保持原来向上的运动
时间: 2024-02-09 18:11:45 浏览: 160
这段代码实现了鼠标左击后在视图中心出现一个随机颜色的圆,并在鼠标松开后开始一个定时器,每隔一段时间就向上移动一定的距离。
要实现前面左击鼠标出现的圆不会因为后面再左击鼠标所出现的圆而消失,可以将将绘制圆的代码从 `OnLButtonDown` 中移动到 `OnTimer` 中,这样每个定时器周期都会重新绘制圆,并向上移动。同时,为了避免绘制出现的圆重叠在一起,可以将圆的半径 `r` 固定为一个常量值,例如 `r=20`。
修改后的代码如下:
```
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
SetTimer(1, 200, NULL);
CView::OnLButtonUp(nFlags, point);
}
void CMyView::OnTimer(UINT nIDEvent)
{
center.y -= 5;
pDC = GetDC();
color = RGB(rand() % 6, rand() % 6, rand() % 6);
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - 20, center.y - 20, center.x + 20, center.y + 20);
br.DeleteObject();
CView::OnTimer(nIDEvent);
}
```
阅读全文