在VS2010内使用MFC建立动画
1.导入4个位图图像到当前工程内,建立四个基础变量,
// 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 commandsON_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/destructionCmyImage008View::CmyImage008View(){// TODO: add construction code herestatic 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 csreturn CView::PreCreateWindow(cs);}// CmyImage008View drawingvoid 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 printingvoid CmyImage008View::OnFilePrintPreview(){#ifndef SHARED_HANDLERSAFXPrintPreview(this);#endif}BOOL CmyImage008View::OnPreparePrinting(CPrintInfo* pInfo){// default preparationreturn 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_HANDLERStheApp.GetContextMenuManager()->ShowPopupMenu(IDR_POPUP_EDIT, point.x, point.y, this, TRUE);#endif}// CmyImage008View diagnostics#ifdef _DEBUGvoid 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 handlersint 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 defaultCClientDC 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