在VS2010内使用MFC建立动画

网友投稿 330 2022-11-04


在VS2010内使用MFC建立动画

1.导入4个位图图像到当前工程内,建立四个基础变量,

private: int m_moveY; int m_moveX; //移动距离 CBitmap m_bitmap[4]; //存储图像 int m_nCurBitmap; //索引

分别用于存储移动距离,存储图像,当前图像索引位置。

2.构造函数初始化:

CmyImage008View::CmyImage008View() { // TODO: add construction code here static int bitmap[]={IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3,IDB_BITMAP4}; m_moveX=20; m_moveY=10; m_nCurBitmap=0; for(int i=0;i<4;i++) { m_bitmap[i].LoadBitmapW(bitmap[i]); } }

3.完成OnDraw()函数

void CmyImage008View::OnDraw(CDC *pDC ) { CmyImage008Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); /* if (!pDoc) return; */ CDC MDC; MDC.CreateCompatibleDC(pDC); CBitmap *pOldBitmap; BITMAP bm; pOldBitmap=(CBitmap*)MDC.SelectObject(&m_bitmap[m_nCurBitmap]); m_bitmap[m_nCurBitmap].GetBitmap(&bm); pDC->BitBlt(m_moveX,m_moveY,bm.bmWidth,bm.bmHeight,&MDC,0,0,SRCCOPY); MDC.SelectObject(pOldBitmap); MDC.DeleteDC(); // TODO: add draw code for native data here }

4.完成OnCreat函数

int CmyImage008View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here //return 0; SetTimer(1,1000,NULL); }

5.完成OnTimer函数

void CmyImage008View::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); m_nCurBitmap^=1; if(m_nCurBitmap>=2) { m_moveX+=10; if(m_moveX>300) m_nCurBitmap-=2; } else { m_moveX-=10; if(m_moveX<=10) m_nCurBitmap+=2; } OnDraw(&dc); InvalidateRect(NULL,TRUE); CView::OnTimer(nIDEvent); }

6.完成OnDestroy函数

void CmyImage008View::OnDestroy() { CView::OnDestroy(); KillTimer(1); // TODO: Add your message handler code here }

完成后的头文件为:

// myImage008View.h : interface of the CmyImage008View class//#pragma onceclass CmyImage008View : public CView{protected: // create from serialization only CmyImage008View(); DECLARE_DYNCREATE(CmyImage008View)// Attributespublic: CmyImage008Doc* GetDocument() const;// Operationspublic:// Overridespublic: 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);// Implementationpublic: virtual ~CmyImage008View();#ifdef _DEBUG virtual void AssertValid() const; virtual void Dump(CDumpContext& dc) const;#endifprotected:// Generated message map functionsprotected: afx_msg void OnFilePrintPreview(); afx_msg void OnRButtonUp(UINT nFlags, CPoint point); afx_msg void OnContextMenu(CWnd* pWnd, CPoint point); DECLARE_MESSAGE_MAP()private: int m_moveY; int m_moveX; //移动距离 CBitmap m_bitmap[4]; //存储图像 int m_nCurBitmap; //索引public: afx_msg int OnCreate(LPCREATESTRUCT lpCreateStruct); afx_msg void OnTimer(UINT_PTR nIDEvent); afx_msg void OnDestroy();};#ifndef _DEBUG // debug version in myImage008View.cppinline CmyImage008Doc* CmyImage008View::GetDocument() const { return reinterpret_cast(m_pDocument); }#endif

cpp文件为:

// myImage008View.cpp : implementation of the CmyImage008View class//#include "stdafx.h"// SHARED_HANDLERS can be defined in an ATL project implementing preview, thumbnail// and search filter handlers and allows sharing of document code with that project.#ifndef SHARED_HANDLERS#include "myImage008.h"#endif#include "myImage008Doc.h"#include "myImage008View.h"#ifdef _DEBUG#define new DEBUG_NEW#endif// CmyImage008ViewIMPLEMENT_DYNCREATE(CmyImage008View, CView) BEGIN_MESSAGE_MAP(CmyImage008View, CView) // Standard printing commands ON_COMMAND(ID_FILE_PRINT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_DIRECT, &CView::OnFilePrint) ON_COMMAND(ID_FILE_PRINT_PREVIEW, &CmyImage008View::OnFilePrintPreview) ON_WM_CONTEXTMENU() ON_WM_RBUTTONUP() ON_WM_CREATE() ON_WM_TIMER() ON_WM_DESTROY() END_MESSAGE_MAP() // CmyImage008View construction/destruction CmyImage008View::CmyImage008View() { // TODO: add construction code here static int bitmap[]={IDB_BITMAP1,IDB_BITMAP2,IDB_BITMAP3,IDB_BITMAP4}; m_moveX=20; m_moveY=10; m_nCurBitmap=0; for(int i=0;i<4;i++) { m_bitmap[i].LoadBitmapW(bitmap[i]); } } CmyImage008View::~CmyImage008View() { } BOOL CmyImage008View::PreCreateWindow(CREATESTRUCT& cs) { // TODO: Modify the Window class or styles here by modifying // the CREATESTRUCT cs return CView::PreCreateWindow(cs); } // CmyImage008View drawing void CmyImage008View::OnDraw(CDC *pDC ) { CmyImage008Doc* pDoc = GetDocument(); ASSERT_VALID(pDoc); /* if (!pDoc) return; */ CDC MDC; MDC.CreateCompatibleDC(pDC); CBitmap *pOldBitmap; BITMAP bm; pOldBitmap=(CBitmap*)MDC.SelectObject(&m_bitmap[m_nCurBitmap]); m_bitmap[m_nCurBitmap].GetBitmap(&bm); pDC->BitBlt(m_moveX,m_moveY,bm.bmWidth,bm.bmHeight,&MDC,0,0,SRCCOPY); MDC.SelectObject(pOldBitmap); MDC.DeleteDC(); // TODO: add draw code for native data here } // CmyImage008View printing void CmyImage008View::OnFilePrintPreview() {#ifndef SHARED_HANDLERS AFXPrintPreview(this);#endif } BOOL CmyImage008View::OnPreparePrinting(CPrintInfo* pInfo) { // default preparation return DoPreparePrinting(pInfo); } void CmyImage008View::OnBeginPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add extra initialization before printing } void CmyImage008View::OnEndPrinting(CDC* /*pDC*/, CPrintInfo* /*pInfo*/) { // TODO: add cleanup after printing } void CmyImage008View::OnRButtonUp(UINT /* nFlags */, CPoint point) { ClientToScreen(&point); OnContextMenu(this, point); } void CmyImage008View::OnContextMenu(CWnd* /* pWnd */, CPoint point) {#ifndef SHARED_HANDLERS theApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);#endif } // CmyImage008View diagnostics#ifdef _DEBUG void CmyImage008View::AssertValid() const { CView::AssertValid(); } void CmyImage008View::Dump(CDumpContext& dc) const { CView::Dump(dc); } CmyImage008Doc* CmyImage008View::GetDocument() const // non-debug version is inline { ASSERT(m_pDocument->IsKindOf(RUNTIME_CLASS(CmyImage008Doc))); return (CmyImage008Doc*)m_pDocument; }#endif //_DEBUG // CmyImage008View message handlers int CmyImage008View::OnCreate(LPCREATESTRUCT lpCreateStruct) { if (CView::OnCreate(lpCreateStruct) == -1) return -1; // TODO: Add your specialized creation code here //return 0; SetTimer(1,1000,NULL); } void CmyImage008View::OnTimer(UINT_PTR nIDEvent) { // TODO: Add your message handler code here and/or call default CClientDC dc(this); m_nCurBitmap^=1; if(m_nCurBitmap>=2) { m_moveX+=10; if(m_moveX>300) m_nCurBitmap-=2; } else { m_moveX-=10; if(m_moveX<=10) m_nCurBitmap+=2; } OnDraw(&dc); InvalidateRect(NULL,TRUE); CView::OnTimer(nIDEvent); } void CmyImage008View::OnDestroy() { CView::OnDestroy(); KillTimer(1); // TODO: Add your message handler code here }

参考文件:

Visual C++从初学到精通 ,吕乐,电子工业出版社,第九章,pp.186-190


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:在VS2010内配置OPENCV241
下一篇:天干地支查询API(天干地支查询软件)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~