Click here to Skip to main content
14,603,442 members

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

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


Image 1

Image 2

Image 3

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 Nishant
United States United States
Nish Nishant is a Principal Software Architect based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on and another 250+ blog articles on his WordPress blog. Nish is vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website :

Comments and Discussions

QuestionCan you add a video please? Pin
Ralph David Abernathy4-May-13 9:08
MemberRalph David Abernathy4-May-13 9:08 
GeneralMy vote of 3 Pin
Ralph David Abernathy4-May-13 9:06
MemberRalph David Abernathy4-May-13 9:06 
QuestionStatic control background 2 Pin
pippo pioppo21-Dec-11 23:23
Memberpippo pioppo21-Dec-11 23:23 
QuestionButton background image stretch Pin
Member 33126532-Jul-11 2:01
MemberMember 33126532-Jul-11 2:01 
Questionfor win32 Pin
perry.p23-Sep-10 21:02
Memberperry.p23-Sep-10 21:02 
GeneralChanging the bitmap Pin
gilmar6-Jan-10 4:31
Membergilmar6-Jan-10 4:31 
General[modified] got error LNK2001: unresolved external symbol : problem solved Pin
Inair3-Apr-08 0:45
MemberInair3-Apr-08 0:45 
GeneralReally need help plz plz plz Pin
Mathewsimpson3-Nov-07 14:19
MemberMathewsimpson3-Nov-07 14:19 
GeneralShow Static text Pin
nugroho231-Oct-07 5:44
Membernugroho231-Oct-07 5:44 
GeneralRe: Show Static text Pin
Anu Koshy27-May-10 23:15
MemberAnu Koshy27-May-10 23:15 
GeneralRe: Show Static text Pin
Ramveer Singh from Gurgaon, Haryana22-May-11 20:41
MemberRamveer Singh from Gurgaon, Haryana22-May-11 20:41 
GeneralWorks like a charm - Thx! Pin
DavidCarr5-Apr-07 9:57
MemberDavidCarr5-Apr-07 9:57 
GeneralRe: Works like a charm - Thx! Pin
DavidCarr5-Apr-07 9:59
MemberDavidCarr5-Apr-07 9:59 
QuestionHow to add a .BMP as a resource (IDB_BITMAP1) Pin
leevonk8-Jun-06 9:54
Memberleevonk8-Jun-06 9:54 
GeneralDebug Assertion Pin
30-Jul-05 7:55
suss30-Jul-05 7:55 
QuestionHow to set the bitmap to menus and status bar Pin
Koundinya18-Aug-04 2:46
MemberKoundinya18-Aug-04 2:46 
QuestionHow to make DPI independent dialog Pin
KmAshif20-Jul-04 20:05
MemberKmAshif20-Jul-04 20:05 
Generalstatic, radio button, checkbox, menu Pin
brian scott18-Nov-03 5:36
Memberbrian scott18-Nov-03 5:36 
Generalrounded XP buttons Pin
Jason Henderson22-Oct-03 11:21
MemberJason Henderson22-Oct-03 11:21 
GeneralLoad hi-res bitmap from file Pin
delta00317-Oct-03 19:38
Memberdelta00317-Oct-03 19:38 
GeneralNow You Can Implement Transparent Background Pin
Sadru10-Oct-03 5:34
MemberSadru10-Oct-03 5:34 
I have modify the code so that it can also implement the transparent background to the dialog ---- So you can watermark your company logo on the background.

Create the bitmap you want to display and also create the mask. (In the mask, whatever is white need to be converted to black and all other color needed to convert the black)

Modify BDialog.h
#define StyleTile 0
#define StyleCenter 1
#define StyleStretch 2

class CBDialog : public CDialog
// Construction
int SetBitmap(UINT nIDResource,UINT nIDResourcemask=0);
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;
CBitmap m_bitmap_mask;
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_)

Modify the BDialog.cpp

// 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


// CBDialog message handlers

BOOL CBDialog::OnEraseBkgnd(CDC* pDC)
if(!m_bitmap.m_hObject && !m_bitmap_mask.m_hObject)
return true;

if (!m_bitmap_mask.m_hObject){ //old way
CBitmap* pOldBitmap = NULL;
CRect rect;
CDC dc;
int bmw, bmh ;
BITMAP bmap;
int xo=0, yo=0;

pOldBitmap = dc.SelectObject(&m_bitmap);
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;

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

else {
// transparent background image
CRect rect;
int x0=0, y0=0;
int cx = 0, cy = 0;
int bmw, bmh ;
BITMAP bmap;
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;
x0 =rect.left;
cx = rect.Width();
cy = rect.Height();

if(m_style == StyleCenter){
if(bmw < cx)
x0 = (cx - bmw)/2;
if(bmh < cy)
y0 = (cy-bmh)/2;

CDC dcBmp,dcMask;

CDC hdcMem;
CBitmap hBitmap;
if(m_style == StyleCenter)
else if(m_style == StyleStretch || m_style == StyleTile)

if(m_style == StyleCenter)
pDC->BitBlt (x0,y0,bmw,bmh,&hdcMem,0,0,SRCCOPY);
else if(m_style == StyleStretch)
pDC->StretchBlt (0,0,cx,cy,&hdcMem,0,0,bmw,bmh,SRCCOPY);
for (y0 = 0; y0 < rect.Height(); y0 += bmh){
for (x0 = 0; x0 < rect.Width(); x0 += bmw){
pDC->BitBlt (x0, y0, bmw, bmh, &hdcMem, 0, 0, SRCCOPY);

return true;

void CBDialog::SetBitmapStyle(int style)
m_style = style;


int CBDialog::SetBitmap(UINT nIDResource,UINT nIDResourcemask)
if (nIDResourcemask > 0){
return 1;
return 0;

return 1;//error

Have fun!
QuestionIs it possible? Pin
speedpacer27-Sep-03 9:22
Memberspeedpacer27-Sep-03 9:22 
AnswerRe: Is it possible? Pin
Nish Nishant27-Sep-03 17:57
sitebuilderNish Nishant27-Sep-03 17:57 
GeneralRe: Is it possible? Pin
speedpacer28-Sep-03 11:24
Memberspeedpacer28-Sep-03 11:24 
GeneralThank you! Pin
Hosam Aly Mahmoud12-Aug-03 17:56
MemberHosam Aly Mahmoud12-Aug-03 17:56 

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

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

Posted 6 Jun 2002


94 bookmarked