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 once


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

// Attributes
public:
CmyImage008Doc* GetDocument() const;

// Operations
public:

// Overrides
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);

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

protected:

// Generated message map functions
protected:
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.cpp
inline CmyImage008Doc* CmyImage008View::GetDocument() const
{ return reinterpret_cast<CmyImage008Doc*>(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


// CmyImage008View

IMPLEMENT_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