MFC基于单文档制作吹彩色泡泡程序
生活随笔
收集整理的這篇文章主要介紹了
MFC基于单文档制作吹彩色泡泡程序
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
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 onlyCMyDoc();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 _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const; #endifprotected:// 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_MSGDECLARE_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 > 1000class CMyView : public CView { protected: // create from serialization onlyCMyView();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 viewvirtual 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 _DEBUGvirtual void AssertValid() const;virtual void Dump(CDumpContext& dc) const; #endifprotected:// 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_MSGDECLARE_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/ // CMyDocIMPLEMENT_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/destructionCMyDoc::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 serializationvoid 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<afxwin.h>#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/ // CMyViewIMPLEMENT_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 commandsON_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/destructionCMyView::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 csreturn CView::PreCreateWindow(cs); }/ // CMyView drawingvoid CMyView::OnDraw(CDC* pDC) {CMyDoc* pDoc = GetDocument();ASSERT_VALID(pDoc);// TODO: add draw code for native data herefor(int i=0;i<pDoc->m_nBubbleCount;i++) pDC->Ellipse(pDoc->m_rectBubble[i]);}/ // CMyView printingBOOL CMyView::OnPreparePrinting(CPrintInfo* pInfo) {// default preparationreturn 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 handlersvoid CMyView::OnLButtonDown(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultCMyDoc* pDoc = GetDocument();ASSERT_VALID(pDoc); if(pDoc->m_nBubbleCount<MAX_BUBBLE){int r=rand()%50+10;int j;j=pDoc->m_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 defaultSetTimer(1,200,NULL);CView::OnLButtonDblClk(nFlags, point); }void CMyView::OnRButtonDblClk(UINT nFlags, CPoint point) {// TODO: Add your message handler code here and/or call defaultKillTimer(1);CView::OnRButtonDblClk(nFlags, point); }void CMyView::OnTimer(UINT nIDEvent) {// TODO: Add your message handler code here and/or call defaultInvalidate();CView::OnTimer(nIDEvent); }void CMyView::OnPaint() {CMyDoc*pDoc=GetDocument();ASSERT_VALID(pDoc); CPaintDC dc(this); // device context for paintingfor(int i=0;i<pDoc->m_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、運行效果如下:
左鍵單擊畫圓,左鍵雙擊畫動圓,右鍵雙擊定圓。
總結(jié)
以上是生活随笔為你收集整理的MFC基于单文档制作吹彩色泡泡程序的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: knn用于水果数据集分类
- 下一篇: 怎样清空输入缓冲区里的内容