MFC基于单文档制作吹彩色泡泡程序

1、代码如下:


// 吹彩色泡泡Doc.h : interface of the CMyDoc class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_DOC_H__559CF0A7_41F7_48A5_A356_774764C0E60F__INCLUDED_)
#define AFX_DOC_H__559CF0A7_41F7_48A5_A356_774764C0E60F__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define MAX_BUBBLE 250 //设置吹泡泡的上限

class CMyDoc : public CDocument
{
protected: // create from serialization only
	CMyDoc();
	DECLARE_DYNCREATE(CMyDoc)

// Attributes
public:
	CRect m_rectBubble[MAX_BUBBLE];
	int x[MAX_BUBBLE],y[MAX_BUBBLE],X[MAX_BUBBLE],Y[MAX_BUBBLE] ;
	int a[MAX_BUBBLE],b[MAX_BUBBLE],c[MAX_BUBBLE];
	int m_nBubbleCount;
// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMyDoc)
	public:
	virtual BOOL OnNewDocument();
	virtual void Serialize(CArchive& ar);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMyDoc();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CMyDoc)
		// NOTE - the ClassWizard will add and remove member functions here.
		//    DO NOT EDIT what you see in these blocks of generated code !
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_DOC_H__559CF0A7_41F7_48A5_A356_774764C0E60F__INCLUDED_)

// 吹彩色泡泡View.h : interface of the CMyView class
//
/////////////////////////////////////////////////////////////////////////////

#if !defined(AFX_VIEW_H__50144892_DB2F_46D1_A755_F4A47754BBA2__INCLUDED_)
#define AFX_VIEW_H__50144892_DB2F_46D1_A755_F4A47754BBA2__INCLUDED_

#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000


class CMyView : public CView
{
protected: // create from serialization only
	CMyView();
	DECLARE_DYNCREATE(CMyView)

// Attributes
public:
	CMyDoc* GetDocument();

// Operations
public:

// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CMyView)
	public:
	virtual void OnDraw(CDC* pDC);  // overridden to draw this view
	virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
	protected:
	virtual BOOL OnPreparePrinting(CPrintInfo* pInfo);
	virtual void OnBeginPrinting(CDC* pDC, CPrintInfo* pInfo);
	virtual void OnEndPrinting(CDC* pDC, CPrintInfo* pInfo);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CMyView();
#ifdef _DEBUG
	virtual void AssertValid() const;
	virtual void Dump(CDumpContext& dc) const;
#endif

protected:

// Generated message map functions
protected:
	//{{AFX_MSG(CMyView)
	afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
	afx_msg void OnLButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnRButtonDblClk(UINT nFlags, CPoint point);
	afx_msg void OnTimer(UINT nIDEvent);
	afx_msg void OnPaint();
	//}}AFX_MSG
	DECLARE_MESSAGE_MAP()
};

#ifndef _DEBUG  // debug version in 吹彩色泡泡View.cpp
inline CMyDoc* CMyView::GetDocument()
   { return (CMyDoc*)m_pDocument; }
#endif

/////////////////////////////////////////////////////////////////////////////

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_VIEW_H__50144892_DB2F_46D1_A755_F4A47754BBA2__INCLUDED_)

// 吹彩色泡泡Doc.cpp : implementation of the CMyDoc class
//

#include "stdafx.h"
#include "吹彩色泡泡.h"

#include "吹彩色泡泡Doc.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyDoc

IMPLEMENT_DYNCREATE(CMyDoc, CDocument)

BEGIN_MESSAGE_MAP(CMyDoc, CDocument)
	//{{AFX_MSG_MAP(CMyDoc)
		// NOTE - the ClassWizard will add and remove mapping macros here.
		//    DO NOT EDIT what you see in these blocks of generated code!
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CMyDoc construction/destruction

CMyDoc::CMyDoc()
{
	// TODO: add one-time construction code here

}

CMyDoc::~CMyDoc()
{
}

BOOL CMyDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	// TODO: add reinitialization code here
	// (SDI documents will reuse this document)
	m_nBubbleCount=0;
	return TRUE;
}



/////////////////////////////////////////////////////////////////////////////
// CMyDoc serialization

void CMyDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}

/////////////////////////////////////////////////////////////////////////////
// CMyDoc diagnostics

#ifdef _DEBUG
void CMyDoc::AssertValid() const
{
	CDocument::AssertValid();
}

void CMyDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif //_DEBUG

/////////////////////////////////////////////////////////////////////////////
// CMyDoc commands

// 吹彩色泡泡View.cpp : implementation of the CMyView class
//
#include

#include "stdafx.h"
#include "吹彩色泡泡.h"

#include "吹彩色泡泡Doc.h"
#include "吹彩色泡泡View.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CMyView

IMPLEMENT_DYNCREATE(CMyView, CView)

BEGIN_MESSAGE_MAP(CMyView, CView)
	//{{AFX_MSG_MAP(CMyView)
	ON_WM_LBUTTONDOWN()
	ON_WM_LBUTTONDBLCLK()
	ON_WM_RBUTTONDBLCLK()
	ON_WM_TIMER()
	ON_WM_PAINT()
	//}}AFX_MSG_MAP
	// Standard printing commands
	ON_COMMAND(ID_FILE_PRINT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_DIRECT, CView::OnFilePrint)
	ON_COMMAND(ID_FILE_PRINT_PREVIEW, CView::OnFilePrintPreview)
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);
	// TODO: add draw code for native data here
	for(int i=0;im_nBubbleCount;i++)         
   pDC->Ellipse(pDoc->m_rectBubble[i]);

}

/////////////////////////////////////////////////////////////////////////////
// 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
	CMyDoc* pDoc = GetDocument();
	ASSERT_VALID(pDoc);
if(pDoc->m_nBubbleCountm_nBubbleCount;
		pDoc->a[j]=rand()%255,pDoc->b[j]=rand()%255,pDoc->c[j]=rand()%255;
		pDoc->x[j]=point.x-r;pDoc->y[j]=point.y-r;pDoc->X[j]=point.x+r;pDoc->Y[j]=point.y+r;
		CRect rect(point.x-r,point.y-r,point.x+r,point.y+r);
		pDoc->m_rectBubble[pDoc->m_nBubbleCount]=rect;
		pDoc->m_nBubbleCount++;
		pDoc->SetModifiedFlag();
		InvalidateRect(rect,FALSE);
	}
	
	CView::OnLButtonDown(nFlags, point);
}


void CMyView::OnLButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	SetTimer(1,200,NULL);
	
	CView::OnLButtonDblClk(nFlags, point);
}

void CMyView::OnRButtonDblClk(UINT nFlags, CPoint point) 
{
	// TODO: Add your message handler code here and/or call default
	KillTimer(1);
	
	CView::OnRButtonDblClk(nFlags, point);
}

void CMyView::OnTimer(UINT nIDEvent) 
{
	// TODO: Add your message handler code here and/or call default
	Invalidate();
	
	CView::OnTimer(nIDEvent);
}

void CMyView::OnPaint() 
{
	
CMyDoc*pDoc=GetDocument();
	ASSERT_VALID(pDoc);
CPaintDC dc(this); // device context for painting
	 for(int i=0;im_nBubbleCount;i++)
	 {
		 CBrush  brushNew;
	   CPen  penNew;	   
	   brushNew.CreateSolidBrush(RGB(pDoc->a[i],pDoc->b[i],pDoc->c[i]));
	   penNew.CreatePen(PS_SOLID,1,RGB(255,0,0));
	   dc.SelectObject(&brushNew);  
		dc.SelectObject(&penNew);
       dc.Ellipse(pDoc->x[i],pDoc->y[i],pDoc->X[i],pDoc->Y[i]);
       pDoc->y[i]=pDoc->y[i]-15-(pDoc->X[i]-pDoc->x[i])/5,pDoc->Y[i]=pDoc->Y[i]-15-(pDoc->X[i]-pDoc->x[i])/5;
		RECT r;
		GetClientRect(&r);
	if(pDoc->Y[i]<0)
		{
			pDoc->y[i]=r.bottom,pDoc->Y[i]=r.bottom-pDoc->X[i]+pDoc->x[i];
		}
	 }	// TODO: Add your message handler code here
	
	// Do not call CView::OnPaint() for painting messages
}

2、运行效果如下:


MFC基于单文档制作吹彩色泡泡程序_第1张图片


左键单击画圆,左键双击画动圆,右键双击定圆。




你可能感兴趣的:(MFC学习笔记,MFC——学习笔记)