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 Nishant

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

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 
I am facing the same problem. Can you please help me that how can i solve this?
I tried BringWindowToTop() but it not worked.
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 
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.150327.1 | Last Updated 7 Jun 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid