Click here to Skip to main content
Click here to Skip to main content

Tile, center and stretch a bitmap as your MFC dialog background

, 6 Jun 2002 CPOL
Rate this:
Please Sign up or sign in to vote.
A small CDialog derived class that helps avoid frequent copy/pasting of the same code, when using a bitmap as dialog background


Adding the class to your project

There are three steps you have to follow before you can use this class in your project.

  1. Add BDialog.cpp and BDialog.h to your project
  2. Replace all instances of CDialog with  CBDialog. Do this to both the cpp and h file of your dialog class. For example if your project is called Test, make the changes to TestDlg.cpp and TestDlg.h. You'll have to use Find and Replace.
  3. #include BDialog.h to your dialog header file

So basically we have derived our dialog class from CBDialog instead of CDialog, but since CBDialog is derived from CDialog, you won't have any problems with your existing code.

Using the class

Basically there are just two methods that you need to call.

void CBDialog::SetBitmapStyle(int style)

style - This is used to set the bitmap background style. You can use one of the following three styles.

  • StyleTile - The background image will be tiled
  • StyleStretch - The background image will be stretched
  • StyleCenter - The background image will be centered, if it is smaller than the client area of the dialog

You can call this method either from OnInitDialog() or from any other place in your dialog class. If you call it outside OnInitDialog() you'll also have to call Invalidate() to force a repaint of the dialog client area.

int CBDialog::SetBitmap(UINT nIDResource)

nIDResource - This specifies the resource ID number of the bitmap resource

Return Value

The return value is zero on success and non-zero on failure.


This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


About the Author

Nish Sivakumar

United States United States
Nish is a real nice guy who has been writing code since 1990 when he first got his hands on an 8088 with 640 KB RAM. Originally from sunny Trivandrum in India, he has been living in various places over the past few years and often thinks it’s time he settled down somewhere.
Nish has been a Microsoft Visual C++ MVP since October, 2002 - awfully nice of Microsoft, he thinks. He maintains an MVP tips and tricks web site - where you can find a consolidated list of his articles, writings and ideas on VC++, MFC, .NET and C++/CLI. Oh, and you might want to check out his blog on C++/CLI, MFC, .NET and a lot of other stuff -
Nish loves reading Science Fiction, P G Wodehouse and Agatha Christie, and also fancies himself to be a decent writer of sorts. He has authored a romantic comedy Summer Love and Some more Cricket as well as a programming book – Extending MFC applications with the .NET Framework.
Nish's latest book C++/CLI in Action published by Manning Publications is now available for purchase. You can read more about the book on his blog.
Despite his wife's attempts to get him into cooking, his best effort so far has been a badly done omelette. Some day, he hopes to be a good cook, and to cook a tasty dinner for his wife.

Comments and Discussions

GeneralWorks like a charm - Thx! PinmemberJunky695-Apr-07 10:57 
GeneralRe: Works like a charm - Thx! PinmemberJunky695-Apr-07 10:59 
QuestionHow to add a .BMP as a resource (IDB_BITMAP1) Pinmemberleevonk8-Jun-06 10:54 
GeneralDebug Assertion PinmemberSalloum Sotiris30-Jul-05 8:55 
Hi thanks for the code, while I'm trying to change the background inside my code I get a Debug Assertion fault while loading the new bitmap. If this error ignored on a release version after 30 changes of the bitmap the application ignores tottaly the command. I solved this problem by changing your implementation from static CBitmap to CBitmap* more specificaly:

#if !defined(AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_)
#define AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_
#if _MSC_VER > 1000
#pragma once
#endif // _MSC_VER > 1000

#define StyleTile 0
#define StyleCenter 1
#define StyleStretch 2
class CBDialog : public CDialog
// Construction
int SetBitmap(UINT nIDResource);
void SetBitmapStyle(int style);
CBDialog(UINT nIDTemplate, CWnd* pParent = NULL); // standard constructor
// Dialog Data
// NOTE: the ClassWizard will add data members here

// Overrides
// ClassWizard generated virtual function overrides
// Implementation
// Generated message map functions
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
CBitmap* m_bitmap;
int m_style;
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.
#endif // !defined(AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_)


// BDialog.cpp : implementation file
#include "stdafx.h"
#include "BDialog.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
// CBDialog dialog

CBDialog::CBDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
// NOTE: the ClassWizard will add member initialization here
m_bitmap=new CBitmap();

// CBDialog message handlers
BOOL CBDialog::OnEraseBkgnd(CDC* pDC)
return true;

CRect rect;
CDC dc;
CBitmap* pOldBitmap = dc.SelectObject(m_bitmap);
int bmw, bmh ;
BITMAP bmap;
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;
int xo=0, yo=0;
if(m_style == StyleTile)
for (yo = 0; yo < rect.Height(); yo += bmh)
for (xo = 0; xo < rect.Width(); xo += bmw)
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
if(m_style == StyleCenter)
if(bmw < rect.Width())
xo = (rect.Width() - bmw)/2;
if(bmh < rect.Height())
yo = (rect.Height()-bmh)/2;
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
if(m_style == StyleStretch)
pDC->StretchBlt(xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0,bmw,bmh, SRCCOPY);


return true;
void CBDialog::SetBitmapStyle(int style)
m_style = style;
int CBDialog::SetBitmap(UINT nIDResource)
delete m_bitmap;
m_bitmap=new CBitmap();
return 0;
return 1;//error

Enjoy and again thanks for your code Big Grin | :-D
QuestionHow to set the bitmap to menus and status bar PinmemberKoundinya18-Aug-04 3:46 
QuestionHow to make DPI independent dialog PinmemberkmAshif20-Jul-04 21:05 
Generalstatic, radio button, checkbox, menu Pinmemberbrian scott18-Nov-03 6:36 
Generalrounded XP buttons PinmemberJason Henderson22-Oct-03 12:21 
GeneralLoad hi-res bitmap from file Pinmemberdelta00317-Oct-03 20:38 
GeneralNow You Can Implement Transparent Background PinmemberSadru10-Oct-03 6:34 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

| Advertise | Privacy | Terms of Use | Mobile
Web01 | 2.8.141223.1 | Last Updated 7 Jun 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid