怎样实现动画背景旗帜
【打印文章】
作者:贾旭滨(译)
//原著:Juan Carlos Urbina
//译者:重庆大学光电工程学院 贾旭滨
//欢迎大家批评指教,谢谢!
以下代码是关于用类CmdiMainClient来显示动画背景旗帜的一个例子。原理是很简单的,我们只需要一系列连续的位图,并且让它连续显示。在这个例子中你可以用鼠标的右键来开始/停止这个动画,如果点左键的话,就可以显示一个子窗口,来保持动画是继续。为了你的方便,你可以把代码直接从浏览器中拷贝下去,并且插到你的代码中就行了。以下是代码:
#include "stdafx.h"
#include "banner.h"
#include "mainfrm.h"
#include "resource.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMdiMainClient
CMdiMainClient::CMdiMainClient()
: m_sizeClient(0, 0)
{
VERIFY (m_bmpBackGround.LoadBitmap(IDB_BCKGRD));
ASSERT (m_bmpBackGround.GetSafeHandle());
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP0));
ASSERT (m_bmpBackGround2.GetSafeHandle());
BmpLoaded = 1;
StopRotation = FALSE;
}
CMdiMainClient::~CMdiMainClient()
{
if(m_bmpBackGround.GetSafeHandle())
m_bmpBackGround.DeleteObject();
if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
}
BEGIN_MESSAGE_MAP(CMdiMainClient, CWnd)
//{{AFX_MSG_MAP(CMdiMainClient)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMdiMainClient message handlers
BOOL CMdiMainClient::OnEraseBkgnd(CDC* pDC)
{
// Erase only the area needed
CRect rect;
GetClientRect(&rect) ;
SetTimer(0,300,NULL);
// Place the bitmap
BITMAP bm ;
CDC dcMem ;
BITMAP bm2 ;
CDC dcMem2;
VERIFY(m_bmpBackGround.GetObject(sizeof(bm), (LPVOID)&bm));
dcMem.CreateCompatibleDC(NULL);
CBitmap* pbmpOld = (CBitmap*)dcMem.SelectObject(&m_bmpBackGround);
ASSERT(pbmpOld) ;
// Display red background
pDC->StretchBlt(0, 0, rect.right, rect.bottom, &dcMem, 0,
0, bm.bmWidth, bm.bmHeight,SRCCOPY);
VERIFY(m_bmpBackGround2.GetObject(sizeof(bm2), (LPVOID)&bm2));
dcMem2.CreateCompatibleDC(NULL);
dcMem2.SelectObject(&m_bmpBackGround2);
// Display picture background
pDC->StretchBlt(200,100,400, 200, &dcMem2, 0,0, bm2.bmWidth, bm2.bmHeight,
SRCCOPY);
// Display Instructions
pDC->SetBkColor(RGB(255,0,0));
pDC->SetTextColor(RGB(192,192,192));
pDC->TextOut(480, 500, "Mouse Right Click to Stop/Start Special Effects");
pDC->TextOut(480, 515, "Mouse Left-Click to Show Child Window");
// CleanUp
dcMem.SelectObject(pbmpOld) ;
return TRUE;
}
////////////////////////////////////////////////////////////////////
//
void CMdiMainClient::OnSize(UINT nType, int cx, int cy)
{
Default() ;
// if the app is just starting up, save the window
// dimensions and get out
if ((m_sizeClient.cx == 0) && (m_sizeClient.cy==0))
{
m_sizeClient.cx = cx;
m_sizeClient.cy = cy;
return ;
}
// if the size hasn't changed, break and pass to default
if ((m_sizeClient.cx == cx) && ( m_sizeClient.cy == cy))
{
return ;
}
// window size has changed so save new dimensions and force
// entire background to redraw, including icon backgrounds
m_sizeClient.cx = cx;
m_sizeClient.cy = cy;
RedrawWindow(NULL, NULL,
RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW|RDW_ALLCHILDREN);
return ;
}
void CMdiMainClient::OnTimer(UINT nIDEvent)
{
DisplayNextLogoFrame();
CWnd::OnTimer(nIDEvent);
}
void CMdiMainClient::DisplayNextLogoFrame()
{
CRect rect;
GetClientRect(&rect) ;
BITMAP bm2 ;
CDC dcMem2;
CDC *pDC = GetDC();
if (!StopRotation)
{
// Select next logo frame to be displayed
switch (BmpLoaded)
{
case 1: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP1));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 2: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP2));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 3: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP3));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 4: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP4));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 5: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP5));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 6: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP6));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 7: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP7));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 8: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP8));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 9: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP9));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
}
// Change Bitmap index
if (BmpLoaded == 9)
BmpLoaded = 0;
else
BmpLoaded++;
}
VERIFY(m_bmpBackGround2.GetObject(sizeof(bm2), (LPVOID)&bm2));
dcMem2.CreateCompatibleDC(NULL);
dcMem2.SelectObject(&m_bmpBackGround2);
pDC->StretchBlt(200,100,400, 200, &dcMem2, 0,0, bm2.bmWidth, bm2.bmHeight,
SRCCOPY);
ReleaseDC(pDC);
}
void CMdiMainClient::OnRButtonDown(UINT nFlags, CPoint point)
{
if (StopRotation)
StopRotation = FALSE;
else
StopRotation = TRUE;
CWnd::OnRButtonDown(nFlags, point);
}
void CMdiMainClient::OnLButtonDown(UINT nFlags, CPoint point)
{
m_Hide_Logo = TRUE;
KillTimer(0);
// Display Application
((CBannerApp*)AfxGetApp())->pDocTemplate->OpenDocumentFile(NULL);
CWnd::OnLButtonDown(nFlags, point);
}
// MdiMainClient.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CMdiMainClient window
extern BOOL m_Hide_Logo;
class CMdiMainClient : public CWnd
{
// Construction
public:
CMdiMainClient();
// Attributes
protected:
CBitmap m_bmpBackGround2;
CBitmap m_bmpBackGround;
int BmpLoaded;
public:
BOOL StopRotation;
CSize m_sizeClient;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMdiMainClient)
//}}AFX_VIRTUAL
// Implementation
public:
void Display_Blank_Bitmap();
void DisplayNextLogoFrame();
virtual ~CMdiMainClient();
// Generated message map functions
protected:
//{{AFX_MSG(CMdiMainClient)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
//原著:Juan Carlos Urbina
//译者:重庆大学光电工程学院 贾旭滨
//欢迎大家批评指教,谢谢!
以下代码是关于用类CmdiMainClient来显示动画背景旗帜的一个例子。原理是很简单的,我们只需要一系列连续的位图,并且让它连续显示。在这个例子中你可以用鼠标的右键来开始/停止这个动画,如果点左键的话,就可以显示一个子窗口,来保持动画是继续。为了你的方便,你可以把代码直接从浏览器中拷贝下去,并且插到你的代码中就行了。以下是代码:
#include "stdafx.h"
#include "banner.h"
#include "mainfrm.h"
#include "resource.h"
#ifdef _DEBUG
#undef THIS_FILE
static char BASED_CODE THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CMdiMainClient
CMdiMainClient::CMdiMainClient()
: m_sizeClient(0, 0)
{
VERIFY (m_bmpBackGround.LoadBitmap(IDB_BCKGRD));
ASSERT (m_bmpBackGround.GetSafeHandle());
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP0));
ASSERT (m_bmpBackGround2.GetSafeHandle());
BmpLoaded = 1;
StopRotation = FALSE;
}
CMdiMainClient::~CMdiMainClient()
{
if(m_bmpBackGround.GetSafeHandle())
m_bmpBackGround.DeleteObject();
if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
}
BEGIN_MESSAGE_MAP(CMdiMainClient, CWnd)
//{{AFX_MSG_MAP(CMdiMainClient)
// NOTE - the ClassWizard will add and remove mapping macros here.
ON_WM_TIMER()
ON_WM_ERASEBKGND()
ON_WM_RBUTTONDOWN()
ON_WM_LBUTTONDOWN()
ON_WM_SIZE()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CMdiMainClient message handlers
BOOL CMdiMainClient::OnEraseBkgnd(CDC* pDC)
{
// Erase only the area needed
CRect rect;
GetClientRect(&rect) ;
SetTimer(0,300,NULL);
// Place the bitmap
BITMAP bm ;
CDC dcMem ;
BITMAP bm2 ;
CDC dcMem2;
VERIFY(m_bmpBackGround.GetObject(sizeof(bm), (LPVOID)&bm));
dcMem.CreateCompatibleDC(NULL);
CBitmap* pbmpOld = (CBitmap*)dcMem.SelectObject(&m_bmpBackGround);
ASSERT(pbmpOld) ;
// Display red background
pDC->StretchBlt(0, 0, rect.right, rect.bottom, &dcMem, 0,
0, bm.bmWidth, bm.bmHeight,SRCCOPY);
VERIFY(m_bmpBackGround2.GetObject(sizeof(bm2), (LPVOID)&bm2));
dcMem2.CreateCompatibleDC(NULL);
dcMem2.SelectObject(&m_bmpBackGround2);
// Display picture background
pDC->StretchBlt(200,100,400, 200, &dcMem2, 0,0, bm2.bmWidth, bm2.bmHeight,
SRCCOPY);
// Display Instructions
pDC->SetBkColor(RGB(255,0,0));
pDC->SetTextColor(RGB(192,192,192));
pDC->TextOut(480, 500, "Mouse Right Click to Stop/Start Special Effects");
pDC->TextOut(480, 515, "Mouse Left-Click to Show Child Window");
// CleanUp
dcMem.SelectObject(pbmpOld) ;
return TRUE;
}
////////////////////////////////////////////////////////////////////
//
void CMdiMainClient::OnSize(UINT nType, int cx, int cy)
{
Default() ;
// if the app is just starting up, save the window
// dimensions and get out
if ((m_sizeClient.cx == 0) && (m_sizeClient.cy==0))
{
m_sizeClient.cx = cx;
m_sizeClient.cy = cy;
return ;
}
// if the size hasn't changed, break and pass to default
if ((m_sizeClient.cx == cx) && ( m_sizeClient.cy == cy))
{
return ;
}
// window size has changed so save new dimensions and force
// entire background to redraw, including icon backgrounds
m_sizeClient.cx = cx;
m_sizeClient.cy = cy;
RedrawWindow(NULL, NULL,
RDW_INVALIDATE|RDW_ERASE|RDW_ERASENOW|RDW_ALLCHILDREN);
return ;
}
void CMdiMainClient::OnTimer(UINT nIDEvent)
{
DisplayNextLogoFrame();
CWnd::OnTimer(nIDEvent);
}
void CMdiMainClient::DisplayNextLogoFrame()
{
CRect rect;
GetClientRect(&rect) ;
BITMAP bm2 ;
CDC dcMem2;
CDC *pDC = GetDC();
if (!StopRotation)
{
// Select next logo frame to be displayed
switch (BmpLoaded)
{
case 1: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP1));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 2: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP2));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 3: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP3));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 4: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP4));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 5: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP5));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 6: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP6));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 7: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP7));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 8: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP8));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
case 9: if(m_bmpBackGround2.GetSafeHandle())
m_bmpBackGround2.DeleteObject();
VERIFY (m_bmpBackGround2.LoadBitmap(IDB_BITMAP9));
ASSERT (m_bmpBackGround2.GetSafeHandle());
break;
}
// Change Bitmap index
if (BmpLoaded == 9)
BmpLoaded = 0;
else
BmpLoaded++;
}
VERIFY(m_bmpBackGround2.GetObject(sizeof(bm2), (LPVOID)&bm2));
dcMem2.CreateCompatibleDC(NULL);
dcMem2.SelectObject(&m_bmpBackGround2);
pDC->StretchBlt(200,100,400, 200, &dcMem2, 0,0, bm2.bmWidth, bm2.bmHeight,
SRCCOPY);
ReleaseDC(pDC);
}
void CMdiMainClient::OnRButtonDown(UINT nFlags, CPoint point)
{
if (StopRotation)
StopRotation = FALSE;
else
StopRotation = TRUE;
CWnd::OnRButtonDown(nFlags, point);
}
void CMdiMainClient::OnLButtonDown(UINT nFlags, CPoint point)
{
m_Hide_Logo = TRUE;
KillTimer(0);
// Display Application
((CBannerApp*)AfxGetApp())->pDocTemplate->OpenDocumentFile(NULL);
CWnd::OnLButtonDown(nFlags, point);
}
// MdiMainClient.h : header file
//
/////////////////////////////////////////////////////////////////////////////
// CMdiMainClient window
extern BOOL m_Hide_Logo;
class CMdiMainClient : public CWnd
{
// Construction
public:
CMdiMainClient();
// Attributes
protected:
CBitmap m_bmpBackGround2;
CBitmap m_bmpBackGround;
int BmpLoaded;
public:
BOOL StopRotation;
CSize m_sizeClient;
// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CMdiMainClient)
//}}AFX_VIRTUAL
// Implementation
public:
void Display_Blank_Bitmap();
void DisplayNextLogoFrame();
virtual ~CMdiMainClient();
// Generated message map functions
protected:
//{{AFX_MSG(CMdiMainClient)
// NOTE - the ClassWizard will add and remove member functions here.
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
afx_msg void OnSize(UINT nType, int cx, int cy);
afx_msg void OnTimer(UINT nIDEvent);
afx_msg void OnRButtonDown(UINT nFlags, CPoint point);
afx_msg void OnLButtonDown(UINT nFlags, CPoint point);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
本栏文章均来自于互联网,版权归原作者和各发布网站所有,本站收集这些文章仅供学习参考之用。任何人都不能将这些文章用于商业或者其他目的。( Pfan.cn )
【编程爱好者论坛】