Click here to Skip to main content
11,934,783 members (42,662 online)
Click here to Skip to main content
Add your own
alternative version


90 bookmarked

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 Nishant
United States United States
Nish Nishant is a Software Architect/Consultant based out of Columbus, Ohio. He has over 15 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish is a recipient of the annual Microsoft Visual C++ MVP Award since 2002 (13 consecutive awards as of 2014).

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 : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

Comments and Discussions

QuestionHow to make DPI independent dialog Pin
kmAshif20-Jul-04 21:05
memberkmAshif20-Jul-04 21:05 
Generalstatic, radio button, checkbox, menu Pin
brian scott18-Nov-03 6:36
memberbrian scott18-Nov-03 6:36 
Generalrounded XP buttons Pin
Jason Henderson22-Oct-03 12:21
memberJason Henderson22-Oct-03 12:21 
GeneralLoad hi-res bitmap from file Pin
delta00317-Oct-03 20:38
memberdelta00317-Oct-03 20:38 
GeneralNow You Can Implement Transparent Background Pin
Sadru10-Oct-03 6:34
memberSadru10-Oct-03 6: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
WolfSupernova27-Sep-03 10:22
memberWolfSupernova27-Sep-03 10:22 
AnswerRe: Is it possible? Pin
Nishant S27-Sep-03 18:57
editorNishant S27-Sep-03 18:57 
GeneralRe: Is it possible? Pin
WolfSupernova28-Sep-03 12:24
memberWolfSupernova28-Sep-03 12:24 
GeneralThank you! Pin
Hosam Aly Mahmoud12-Aug-03 18:56
memberHosam Aly Mahmoud12-Aug-03 18:56 
GeneralHelp with CEdit Pin
SatyakiranP18-Oct-02 13:21
sussSatyakiranP18-Oct-02 13:21 
GeneralStatic control bg color Pin
kobil22-Jul-02 23:00
susskobil22-Jul-02 23:00 
GeneralRe: Static control bg color Pin
Nishant S18-Oct-02 15:28
editorNishant S18-Oct-02 15:28 
Generalerm vc6.0 ? i think not Pin
bryce17-Jun-02 19:58
memberbryce17-Jun-02 19:58 
GeneralRe: erm vc6.0 ? i think not Pin
Nishant S18-Jun-02 19:30
memberNishant S18-Jun-02 19:30 
GeneralPretty good... Pin
Tim Lesher10-Jun-02 7:45
memberTim Lesher10-Jun-02 7:45 
GeneralRe: Pretty good... Pin
WREY10-Jun-02 9:02
memberWREY10-Jun-02 9:02 
GeneralRe: Pretty good... Pin
Nish - Native CPian11-Jun-02 15:51
memberNish - Native CPian11-Jun-02 15:51 
GeneralRe: Pretty good... Pin
Christian Graus11-Jun-02 16:10
memberChristian Graus11-Jun-02 16:10 
GeneralRe: Pretty good... Pin
Tim Lesher12-Jun-02 3:54
memberTim Lesher12-Jun-02 3:54 
GeneralNot to complain... Pin
Ed Gadziemski8-Jun-02 5:48
memberEd Gadziemski8-Jun-02 5:48 
GeneralRe: Not to complain... Pin
Shog98-Jun-02 6:10
memberShog98-Jun-02 6:10 
GeneralRe: Not to complain... Pin
Nish - Native CPian8-Jun-02 6:17
memberNish - Native CPian8-Jun-02 6:17 
GeneralRe: Not to complain... Pin
Nish - Native CPian8-Jun-02 6:12
memberNish - Native CPian8-Jun-02 6:12 
GeneralRe: Not to complain... Pin
Ed Gadziemski8-Jun-02 14:16
memberEd Gadziemski8-Jun-02 14:16 
GeneralRe: Not to complain... Pin
Ravi Bhavnani9-Jun-02 7:27
memberRavi Bhavnani9-Jun-02 7:27 

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.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.151126.1 | Last Updated 7 Jun 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid