VC mfc单文档中代码如下void CMyView::OnDraw(CDC* pDC) { CMyDoc* pDoc = GetDocument(); ASSERT_VALID(pDoc); for (int i=0;i<points.size();i++){ color=RGB(rand()%6,rand()%6,rand()%6); r=rand()F+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()F+5;//r=5~50 color=RGB(rand()%6,rand()%6,rand()%6); 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-29 15:18:01 浏览: 101
要实现每次单击鼠标出现的圆大小和颜色随机,但在圆的上升过程中大小和颜色都为刚开始单击鼠标时的大小和颜色,不会不停的变换,可以在OnLButtonDown()函数中随机生成圆的颜色和大小,并保存在成员变量中,然后在OnDraw()函数中使用这些保存的颜色和大小来画圆。同时,要避免前面单击鼠标出现的圆的颜色和大小随着后面单击鼠标出现的圆的颜色和大小改变而改变,可以将每个圆的颜色和大小保存在一个points数组中,每个元素对应一个圆的颜色和大小。具体步骤如下:
1. 在CMyView类中添加成员变量:
```cpp
COLORREF color; // 当前圆的颜色
int r; // 当前圆的半径
vector<pair<COLORREF, int>> points; // 保存每个圆的颜色和大小
```
2. 修改OnLButtonDown()函数,随机生成圆的颜色和大小,并保存在成员变量中:
```cpp
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
center = point;
r = rand() % 46 + 5; // 生成5~50之间的随机数
color = RGB(rand() % 256, rand() % 256, rand() % 256); // 生成随机颜色
points.push_back(make_pair(color, r)); // 将当前圆的颜色和大小保存到points数组中
SetTimer(1, 200, NULL);
CView::OnLButtonDown(nFlags, point);
}
```
3. 修改OnDraw()函数,使用points数组中保存的颜色和大小来画圆:
```cpp
void CMyView::OnDraw(CDC* pDC)
{
CMyDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
for (int i = 0; i < points.size(); i++) {
color = points[i].first; // 使用保存的颜色来画圆
r = points[i].second; // 使用保存的大小来画圆
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
br.DeleteObject();
}
// TODO: add draw code for native data here
}
```
4. 修改rise()函数,不再改变圆的大小和颜色:
```cpp
void CMyView::rise()
{
for (int i = 0; i < points.size(); i++) {
points[i].first = points[i].first; // 不再改变颜色
points[i].second = points[i].second; // 不再改变大小
points[i].first -= 5;
if (points[i].first < -points[i].second) {
points.erase(points.begin() + i);
i--;
}
}
}
```
完整代码如下:
```cpp
// MyView.cpp : implementation of the CMyView class
//
#include "stdafx.h"
#include "My.h"
#include "MyDoc.h"
#include "MyView.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#endif
// CMyView
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()
{
// 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 = points[i].first;
r = points[i].second;
br.CreateSolidBrush(color);
pDC->SelectObject(&br);
pDC->Ellipse(center.x - r, center.y - r, center.x + r, center.y + r);
br.DeleteObject();
}
// TODO: add draw code for native data here
}
void CMyView::OnLButtonDown(UINT nFlags, CPoint point)
{
center = point;
r = rand() % 46 + 5; // 生成5~50之间的随机数
color = RGB(rand() % 256, rand() % 256, rand() % 256); // 生成随机颜色
points.push_back(make_pair(color, r)); // 将当前圆的颜色和大小保存到points数组中
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].first = points[i].first; // 不再改变颜色
points[i].second = points[i].second; // 不再改变大小
points[i].first -= 5;
if (points[i].first < -points[i].second) {
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);
}
// 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
```
阅读全文