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

Screenshots

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.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

Share

About the Author

Nish Sivakumar

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 - www.voidnish.com 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 - blog.voidnish.com.
 
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 9:08 
GeneralMy vote of 3 PinmemberRalph David Abernathy4-May-13 9:06 
QuestionStatic control background 2 Pinmemberpippo pioppo21-Dec-11 23:23 
QuestionButton background image stretch PinmemberMember 33126532-Jul-11 2:01 
Questionfor win32 Pinmemberperry.p23-Sep-10 21:02 
GeneralChanging the bitmap Pinmembergilmar6-Jan-10 4:31 
General[modified] got error LNK2001: unresolved external symbol : problem solved PinmemberInair3-Apr-08 0:45 
GeneralReally need help plz plz plz PinmemberMathewsimpson3-Nov-07 14:19 
GeneralShow Static text Pinmembernugroho231-Oct-07 5:44 
GeneralRe: Show Static text PinmemberAnu Koshy27-May-10 23:15 
GeneralRe: Show Static text PinmemberRamveer Singh from Gurgaon, Haryana22-May-11 20:41 
GeneralWorks like a charm - Thx! PinmemberJunky695-Apr-07 9:57 
GeneralRe: Works like a charm - Thx! PinmemberJunky695-Apr-07 9:59 
QuestionHow to add a .BMP as a resource (IDB_BITMAP1) Pinmemberleevonk8-Jun-06 9:54 
GeneralDebug Assertion PinmemberSalloum Sotiris30-Jul-05 7:55 
QuestionHow to set the bitmap to menus and status bar PinmemberKoundinya18-Aug-04 2:46 
QuestionHow to make DPI independent dialog PinmemberkmAshif20-Jul-04 20:05 
Generalstatic, radio button, checkbox, menu Pinmemberbrian scott18-Nov-03 5:36 
Generalrounded XP buttons PinmemberJason Henderson22-Oct-03 11:21 
GeneralLoad hi-res bitmap from file Pinmemberdelta00317-Oct-03 19:38 
GeneralNow You Can Implement Transparent Background PinmemberSadru10-Oct-03 5:34 
QuestionIs it possible? PinmemberWolfSupernova27-Sep-03 9:22 
AnswerRe: Is it possible? PineditorNishant S27-Sep-03 17:57 
GeneralRe: Is it possible? PinmemberWolfSupernova28-Sep-03 11:24 
GeneralThank you! PinmemberHosam Aly Mahmoud12-Aug-03 17:56 
GeneralHelp with CEdit PinsussSatyakiranP18-Oct-02 12:21 
GeneralStatic control bg color Pinsusskobil22-Jul-02 22:00 
GeneralRe: Static control bg color PineditorNishant S18-Oct-02 14:28 
Generalerm vc6.0 ? i think not Pinmemberbryce17-Jun-02 18:58 
GeneralRe: erm vc6.0 ? i think not PinmemberNishant S18-Jun-02 18:30 
GeneralPretty good... PinmemberTim Lesher10-Jun-02 6:45 
GeneralRe: Pretty good... PinmemberWREY10-Jun-02 8:02 
GeneralRe: Pretty good... PinmemberNish - Native CPian11-Jun-02 14:51 
GeneralRe: Pretty good... PinmemberChristian Graus11-Jun-02 15:10 
GeneralRe: Pretty good... PinmemberTim Lesher12-Jun-02 2:54 
GeneralNot to complain... PinmemberEd Gadziemski8-Jun-02 4:48 
GeneralRe: Not to complain... PinmemberShog98-Jun-02 5:10 
GeneralRe: Not to complain... PinmemberNish - Native CPian8-Jun-02 5:17 
GeneralRe: Not to complain... PinmemberNish - Native CPian8-Jun-02 5:12 
GeneralRe: Not to complain... PinmemberEd Gadziemski8-Jun-02 13:16 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 6:27 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 10:11 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 10:22 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 17:19 
Ravi Bhavnani wrote:
My WebResourceProvider article uses GIFs because they're smaller.
 
Oh, okay Smile | :)
 
Nish
 


Author of the romantic comedy

Summer Love and Some more Cricket [New Win]

Buy it, read it and admire me Smile | :)

GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 10:13 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 10:24 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 17:21 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 17:23 
GeneralRe: Not to complain... PinmemberNish - Native CPian9-Jun-02 18:14 
GeneralRe: Not to complain... PinmemberRavi Bhavnani9-Jun-02 18: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 | Mobile
Web01 | 2.8.140814.1 | Last Updated 7 Jun 2002
Article Copyright 2002 by Nish Sivakumar
Everything else Copyright © CodeProject, 1999-2014
Terms of Service
Layout: fixed | fluid