Click here to Skip to main content
12,301,965 members (57,539 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 16 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 (14 consecutive awards as of 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 : You can reach Nish on his google email id voidnish.

Website and Blog

You may also be interested in...

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
Junky695-Apr-07 9:57
memberJunky695-Apr-07 9:57 
GeneralRe: Works like a charm - Thx! Pin
Junky695-Apr-07 9:59
memberJunky695-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
Salloum Sotiris30-Jul-05 7:55
memberSalloum Sotiris30-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 
Hi! This works really well for a dialog background. However, in the app on which I am working (as a not-very experienced programmer), I need to use static text, radio buttons, check boxes and a menu bar. If I want the dialog background to be a simple colour, then the override for OnCtlColor() works fine. However, I really want to use a textured background, so that the text is set against that and not a background colour. Is there a way to set the background of, say, a radio button to the same texture as that of the main dialog?

Confused | :confused: Confused | :confused: Confused | :confused:

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 
QuestionIs it possible? Pin
WolfSupernova27-Sep-03 9:22
memberWolfSupernova27-Sep-03 9:22 
AnswerRe: Is it possible? Pin
Nishant S27-Sep-03 17:57
editorNishant S27-Sep-03 17:57 
GeneralRe: Is it possible? Pin
WolfSupernova28-Sep-03 11:24
memberWolfSupernova28-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.

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