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 10:12:00 浏览: 119
VC++在对话框中加载视图源码实例
在 `CMyView` 类中添加一个成员变量 `std::vector<CPoint> centers`,用来存储所有圆的中心点。每次左击鼠标时,将新的圆心点加入 `centers` 中。然后在 `rise()` 函数中,对于每个圆心点,都让其向上移动并重新绘制圆。修改 `OnLButtonUp()` 函数,在设置定时器后不再调用 `CView::OnLButtonUp(nFlags, point)`,因为这会清空 `centers`,而是在定时器响应函数中调用 `CView::OnLButtonDown(nFlags, point)`,因为这个函数不会清空 `centers`。代码如下:
```
// MyView.h
#pragma once
#include <vector>
class CMyView : public CView
{
protected:
DECLARE_DYNCREATE(CMyView)
CPoint center;
int r;
COLORREF color;
CBrush br;
CDC* pDC;
std::vector<CPoint> centers; // 存储所有圆的中心点
protected:
CMyView() noexcept;
virtual ~CMyView();
public:
virtual void OnDraw(CDC* pDC) override;
virtual void OnInitialUpdate() override;
virtual BOOL PreCreateWindow(CREATESTRUCT& cs) override;
protected:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT nIDEvent);
DECLARE_MESSAGE_MAP()
private:
void rise();
};
// MyView.cpp
#include "pch.h"
#include "framework.h"
#include "MFCApplication1.h"
#include "MyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CMyView, CView)
CMyView::CMyView() noexcept
{
}
CMyView::~CMyView()
{
}
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
END_MESSAGE_MAP()
void CMyView::OnDraw(CDC* /*pDC*/)
{
CMyDocument* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
}
void CMyView::OnInitialUpdate()
{
CView::OnInitialUpdate();
SetTimer(1, 200, NULL);
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
return CView::PreCreateWindow(cs);
}
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
center = point;
pDC = GetDC();
color = RGB(rand() % 6, rand() % 6, rand() % 6);
r = rand() % 50 + 5;
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
br.DeleteObject();
// 将新的圆心点加入 centers 中
centers.push_back(center);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
SetTimer(1, 200, NULL);
// 不再调用 CView::OnLButtonUp,而是在定时器响应函数中调用 CView::OnLButtonDown
}
void CMyView::rise()
{
for (auto& c : centers) // 对于每个圆心点,都让其向上移动并重新绘制圆
{
c.y -= 5;
pDC = GetDC();
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(c.x - r, c.y - r, c.x + r, c.y + r);
br.DeleteObject();
}
}
void CMyView::OnTimer(UINT nIDEvent)
{
RedrawWindow();
rise();
CView::OnLButtonDown(0, center); // 调用 CView::OnLButtonDown,保持 centers 不被清空
}
```
阅读全文