VC mfc单文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;i<points.size();i++){ color=RGB(rand()%256,rand()%256,rand()%256); r=rand()%46+5; br.CreateSolidBrush(color); pDC->SelectObject(&br); pDC->Ellipse(points[i].x-r,points[i].y-r,points[i].x+r,points[i].y+r); br.DeleteObject(); } // TODO: add draw code for native data here } ///////////////////////////////////////////////////////////////////////////// // CMyView printing BOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CMyView::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CMyView::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } ///////////////////////////////////////////////////////////////////////////// // CMyView diagnostics #ifdef _DEBUG void CMyView::AssertValid() const { CView::AssertValid(); } void CMyView::Dump(CDumpContext& dc) const { CView::Dump(dc); } CMyDoc* CMyView::GetDocument() // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc))); return (CMyDoc*)m_pDocument; } #endif //_DEBUG ///////////////////////////////////////////////////////////////////////////// // CMyView message handlers void CMyView::OnLButtonDown(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default center=point; r=rand()%46+5;//r=5~50 color=RGB(rand()%256,rand()%256,rand()%256); points.push_back(center); SetTimer(1,200,NULL); CView::OnLButtonDown(nFlags, point); } void CMyView::OnLButtonUp(UINT nFlags, CPoint point) { // TODO: Add your message handler code here and/or call default CView::OnLButtonUp(nFlags, point); } void CMyView::rise() { for(int i=0;i<points.size();i++) { points[i].y-=5; if(points[i].y<-r) { points.erase(points.begin()+i); i--; } } } void CMyView::OnTimer(UINT nIDEvent) { // TODO: Add your message handler code here and/or call default if(nIDEvent==1){ RedrawWindow(); rise(); } CView::OnTimer(nIDEvent); },运行效果中圆在上升过程中颜色和大小不停的变换,应怎么修改此代码使得圆在上升过程中的大小和颜色不会变换,完整步骤及代码
时间: 2023-06-30 13:14:15 浏览: 174
要使圆在上升过程中大小和颜色不会变换,可以将颜色和大小的变化单独提取出来,放到循环外面进行定义,然后再在循环内部进行圆的上升操作。具体实现方式如下:
1. 在头文件中定义变量:
```cpp
// 头文件中定义变量
COLORREF color; // 圆的颜色
int r; // 圆的半径
```
2. 在`OnDraw`函数中修改绘制圆的代码:
```cpp
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// 绘制圆
for (int i = 0;i < points.size();i++)
{
// 保持颜色和大小不变
color = RGB(0, 0, 255); // 蓝色
r = 20;
// 绘制圆
CPen pen(PS_SOLID, 1, color);
CBrush brush(color);
pDC->SelectObject(&pen);
pDC->SelectObject(&brush);
pDC->Ellipse(points[i].x - r, points[i].y - r, points[i].x + r, points[i].y + r);
}
}
```
在这个代码中,我们将颜色和大小的变化都删除了,直接将颜色设置为蓝色,大小设置为20。这样就可以让圆在上升过程中保持颜色和大小不变了。
完整修改后的代码如下:
MyView.h文件:
```cpp
#pragma once
#include "MyDoc.h"
#include <vector>
class CMyView : public CView
{
protected: // create from serialization only
CMyView() noexcept;
DECLARE_DYNCREATE(CMyView)
// Attributes
public:
CMyDoc* GetDocument() const;
// Operations
public:
// Overrides
public:
virtual void OnDraw(CDC* pDC) override;
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
protected:
virtual void OnInitialUpdate() override; // called first time after construct
virtual void OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/) override;
// Implementation
public:
virtual ~CMyView();
#ifdef _DEBUG
virtual void AssertValid() const;
virtual void Dump(CDumpContext& dc) const;
#endif
protected:
// Generated message map functions
protected:
DECLARE_MESSAGE_MAP()
public:
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonUp(UINT nFlags, CPoint point);
afx_msg void OnTimer(UINT_PTR nIDEvent);
void rise();
};
#ifndef _DEBUG // debug version in MyView.cpp
inline CMyDoc* CMyView::GetDocument() const
{ return reinterpret_cast<CMyDoc*>(m_pDocument); }
#endif
```
MyView.cpp文件:
```cpp
#include "pch.h"
#include "framework.h"
#include "My.h"
#include "MyDoc.h"
#include "MyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
IMPLEMENT_DYNCREATE(CMyView, CView)
BEGIN_MESSAGE_MAP(CMyView, CView)
ON_WM_LBUTTONDOWN()
ON_WM_LBUTTONUP()
ON_WM_TIMER()
END_MESSAGE_MAP()
// CMyView construction/destruction
CMyView::CMyView() noexcept
{
// TODO: add construction code here
}
CMyView::~CMyView()
{
}
BOOL CMyView::PreCreateWindow(CREATESTRUCT& cs)
{
// TODO: Modify the Window class or styles here by modifying
// the CREATESTRUCT cs
return CView::PreCreateWindow(cs);
}
// CMyView drawing
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// 绘制圆
for (int i = 0;i < points.size();i++)
{
// 保持颜色和大小不变
color = RGB(0, 0, 255); // 蓝色
r = 20;
// 绘制圆
CPen pen(PS_SOLID, 1, color);
CBrush brush(color);
pDC->SelectObject(&pen);
pDC->SelectObject(&brush);
pDC->Ellipse(points[i].x - r, points[i].y - r, points[i].x + r, points[i].y + r);
}
}
void CMyView::OnInitialUpdate()
{
CView::OnInitialUpdate();
// TODO: Add your specialized code here and/or call the base class
}
void CMyView::OnUpdate(CView* /*pSender*/, LPARAM /*lHint*/, CObject* /*pHint*/)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
if (!pDoc)
return;
// TODO: Add your specialized code here and/or call the base class
}
// CMyView diagnostics
#ifdef _DEBUG
void CMyView::AssertValid() const
{
CView::AssertValid();
}
void CMyView::Dump(CDumpContext& dc) const
{
CView::Dump(dc);
}
CMyDoc* CMyView::GetDocument() const // non-debug version is inline
{
ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CMyDoc)));
return (CMyDoc*)m_pDocument;
}
#endif //_DEBUG
// CMyView message handlers
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
center = point;
r = rand() % 46 + 5;
color = RGB(rand() % 256, rand() % 256, rand() % 256);
points.push_back(center);
SetTimer(1, 200, NULL);
CView::OnLButtonDown(nFlags, point);
}
void CMyView::OnLButtonUp(UINT nFlags, CPoint point)
{
// TODO: Add your message handler code here and/or call default
CView::OnLButtonUp(nFlags, point);
}
void CMyView::rise()
{
for (int i = 0;i < points.size();i++)
{
points[i].y -= 5;
if (points[i].y < -r)
{
points.erase(points.begin() + i);
i--;
}
}
}
void CMyView::OnTimer(UINT_PTR nIDEvent)
{
// TODO: Add your message handler code here and/or call default
if (nIDEvent == 1) {
RedrawWindow();
rise();
}
CView::OnTimer(nIDEvent);
}
```
这样修改后,圆在上升过程中的大小和颜色就不会变换了。
阅读全文