Click here to Skip to main content
11,409,766 members (60,602 online)
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 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

Comments and Discussions

QuestionCan you add a video please? PinmemberRalph David Abernathy4-May-13 10:08 
GeneralMy vote of 3 PinmemberRalph David Abernathy4-May-13 10:06 
QuestionStatic control background 2 Pinmemberpippo pioppo22-Dec-11 0:23 
QuestionButton background image stretch PinmemberMember 33126532-Jul-11 3:01 
Questionfor win32 Pinmemberperry.p23-Sep-10 22:02 
GeneralChanging the bitmap Pinmembergilmar6-Jan-10 5:31 
General[modified] got error LNK2001: unresolved external symbol : problem solved PinmemberInair3-Apr-08 1:45 
GeneralReally need help plz plz plz PinmemberMathewsimpson3-Nov-07 15:19 
GeneralShow Static text Pinmembernugroho231-Oct-07 6:44 
GeneralRe: Show Static text PinmemberAnu Koshy28-May-10 0:15 
GeneralRe: Show Static text PinmemberRamveer Singh from Gurgaon, Haryana22-May-11 21:41 
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 
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 
When DPI is changed then the dialog size also could it possible to keep it size fix when DPI is changed..for examnple when i create a dialog in 96 dpi then its size will not increase in 120 dpi environment..thanks...Ashif
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 
QuestionIs it possible? PinmemberWolfSupernova27-Sep-03 10:22 
AnswerRe: Is it possible? PineditorNishant S27-Sep-03 18:57 
GeneralRe: Is it possible? PinmemberWolfSupernova28-Sep-03 12:24 
GeneralThank you! PinmemberHosam Aly Mahmoud12-Aug-03 18:56 
GeneralHelp with CEdit PinsussSatyakiranP18-Oct-02 13:21 
GeneralStatic control bg color Pinsusskobil22-Jul-02 23:00 
GeneralRe: Static control bg color PineditorNishant S18-Oct-02 15:28 
Generalerm vc6.0 ? i think not Pinmemberbryce17-Jun-02 19:58 
GeneralRe: erm vc6.0 ? i think not PinmemberNishant S18-Jun-02 19:30 
GeneralPretty good... PinmemberTim Lesher10-Jun-02 7:45 
GeneralRe: Pretty good... PinmemberWREY10-Jun-02 9:02 
GeneralRe: Pretty good... PinmemberNish - Native CPian11-Jun-02 15:51 
GeneralRe: Pretty good... PinmemberChristian Graus11-Jun-02 16:10 
GeneralRe: Pretty good... PinmemberTim Lesher12-Jun-02 3:54 
GeneralNot to complain... PinmemberEd Gadziemski8-Jun-02 5:48 
GeneralRe: Not to complain... PinmemberShog98-Jun-02 6:10 
GeneralRe: Not to complain... PinmemberNish - Native CPian8-Jun-02 6:17 
GeneralRe: Not to complain... PinmemberNish - Native CPian8-Jun-02 6:12 
GeneralRe: Not to complain... PinmemberEd Gadziemski8-Jun-02 14:16 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 7:27 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 11:11 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 11:22 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 18:19 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 11:13 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 11:24 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 18:21 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 18:23 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 19:14 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 19:21 

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
Web03 | 2.8.150414.5 | Last Updated 7 Jun 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid