VC++ 6.0单文档 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-12 07:06:27 浏览: 24
根据您的代码,您可以按照以下步骤来修改代码:
1. 首先,您需要将存储圆形的变量从函数中移到类中。在 CMyView 类中添加以下变量:
```
CArray<Circle, Circle&> m_circles; // 用于存储所有的圆形
```
2. 然后,在 OnLButtonDown 函数中,您需要将生成圆形的代码改为创建 Circle 对象并将其添加到数组中的代码。同时,您需要在 OnLButtonUp 函数中启动一个定时器,以便定期更新窗口并使圆形向上移动。修改后的代码如下:
```
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// 生成随机颜色和大小
int size = rand() % 100 + 20;
int colorIndex = rand() % 6;
COLORREF color = m_colors[colorIndex];
// 创建圆形对象
Circle circle(point, size, color);
// 将圆形对象添加到数组中
m_circles.Add(circle);
// 重绘视图
Invalidate();
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// 启动定时器
SetTimer(1, 200, NULL);
CView::OnLButtonUp(nFlags, point);
}
```
3. 在 OnTimer 函数中,您需要遍历所有的圆形,并使它们向上移动。移动完成后,您需要重新绘制窗口。修改后的代码如下:
```
void CMyView::OnTimer(UINT nIDEvent)
{
// 遍历所有的圆形,使其向上移动
for (int i = 0; i < m_circles.GetSize(); i++)
{
Circle circle = m_circles.GetAt(i);
circle.MoveUp();
m_circles.SetAt(i, circle);
}
// 重绘视图
RedrawWindow();
CView::OnTimer(nIDEvent);
}
```
4. 最后,在 OnDraw 函数中,您需要遍历所有的圆形,并将它们绘制到设备上下文中。修改后的代码如下:
```
void CMyView::OnDraw(CDC* pDC)
{
// 绘制所有圆形
for (int i = 0; i < m_circles.GetSize(); i++)
{
Circle circle = m_circles.GetAt(i);
circle.Draw(pDC);
}
}
```
这样,您就可以实现左击鼠标出现的圆不会因为后面再左击鼠标所出现的圆而消失,而是继续保持原来向上的运动。