在VS2010内使用MFC建立动画
2012-07-03 09:02
218 查看
1.导入4个位图图像到当前工程内,建立四个基础变量,
分别用于存储移动距离,存储图像,当前图像索引位置。
2.构造函数初始化:
3.完成OnDraw()函数
5.完成OnTimer函数
完成后的头文件为:
参考文件:
Visual C++从初学到精通 ,吕乐,电子工业出版社,第九章,pp.186-190
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 classcpp文件为:
//
#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
// 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
相关文章推荐
- Opencv 2.4.6 +VS2010 MFC + StaticLib使用问题
- VS2010 mfc 使用 tab control
- VS2010使用.net 4.0中的ASP.NET MVC 2 模板建立工程后无法提交HTML代码解决方案一则
- 脑上用VS2010编写的VC++程序(使用MFC库
- VS2010下建立MFC程序
- 使用VS2010开发ActiveX(MFC)控件(3)——添加接口及WEB调用
- 使用VS2010开发ActiveX(MFC)控件(3)——添加接口及WEB调用
- 在VS2010里MFC对话框中使用OpenGL绘图,添加的控件不显示的一种解决方法
- 2.使用MFC的CSocket类建立简单的UDP通信
- MFC中使用CUDA5.0的方法(VS2010环境)
- VS2010 MFC使用TeeChart绘图控件之一:控件和类的导入
- 在VS2010进行MFC编程设置字符集的问题(使用多字节字符集引起错误)
- VS2010修改C++项目为“静态库中使用MFC”编译问题
- 使用VS2010开发的MFC Ribbon应用程序运行后关闭了属性界面、输出界面、类视图、通知栏等再次恢复方法
- 使用VS2010开发ActiveX(MFC)控件(1)——创建
- (转载)VS2010/MFC编程入门之三十五(菜单:菜单及CMenu类的使用)
- vs2010下MFC中使用CMFCToolBar在图标下显示文本方法
- 使用vs2010创建MFC工程后,vs2010的bug,不知道你们有没有
- [转]使用CPPUNIT如何建立一个基于MFC的GUI测试框架
- 使用VS2010开发Qt程序的4点经验(QT4到QT5的升级,更改sln文件,切换工程使用的Qt库,在VS的Solution Explorer视图中建立文件夹)