Animated Background Banner | CodeGuru

Animated Background Banner

The following code is used to display an Animated Banner using the the CmdiMainClient class. You will need to get a set of sequenced bitmaps to show the animation. In my example you can Right-Click to Start/Stop the animation and Left-Click to display a child window, keeping the background animation alive. For your convenience, the […]

Written By
CodeGuru Staff
CodeGuru Staff
Jan 15, 1999
1 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

The following code is used to display an Animated Banner
using the the CmdiMainClient class. You will need to get
a set of sequenced bitmaps to show the animation. In my
example you can Right-Click to Start/Stop the animation
and Left-Click to display a child window, keeping the
background animation alive.


For your convenience, the code can be copied and pasted from
your browser or
downloaded (.zip format).

#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()
};

Download demo project – 86 KB

Download source – 3 KB

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.