Click here to Skip to main content
15,867,686 members
Articles / Desktop Programming / MFC
Article

Tile, center and stretch a bitmap as your MFC dialog background

Rate me:
Please Sign up or sign in to vote.
4.80/5 (47 votes)
6 Jun 2002CPOL1 min read 476.8K   13.2K   95   106
A small CDialog derived class that helps avoid frequent copy/pasting of the same code, when using a bitmap as dialog background

Screenshots

Image 1

Image 2

Image 3

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)


Written By
United States United States
Nish Nishant is a technology enthusiast from Columbus, Ohio. He has over 20 years of software industry experience in various roles including Chief Technology Officer, Senior Solution Architect, Lead Software Architect, Principal Software Engineer, and Engineering/Architecture Team Leader. Nish is a 14-time recipient of the Microsoft Visual C++ MVP Award.

Nish authored C++/CLI in Action for Manning Publications in 2005, and co-authored Extending MFC Applications with the .NET Framework for Addison Wesley in 2003. In addition, he has over 140 published technology articles on CodeProject.com and another 250+ blog articles on his WordPress blog. Nish is experienced in technology leadership, solution architecture, software architecture, cloud development (AWS and Azure), REST services, software engineering best practices, CI/CD, mentoring, and directing all stages of software development.

Nish's Technology Blog : voidnish.wordpress.com

Comments and Discussions

 
GeneralRe: How did you change the titlebar Pin
Nish Nishant7-Jun-02 17:15
sitebuilderNish Nishant7-Jun-02 17:15 
GeneralRe: How did you change the titlebar Pin
Shog97-Jun-02 17:29
sitebuilderShog97-Jun-02 17:29 
GeneralRe: How did you change the titlebar Pin
Nish Nishant7-Jun-02 17:27
sitebuilderNish Nishant7-Jun-02 17:27 
GeneralRe: How did you change the titlebar Pin
Shog97-Jun-02 17:49
sitebuilderShog97-Jun-02 17:49 
GeneralRe: How did you change the titlebar Pin
Nish Nishant7-Jun-02 17:59
sitebuilderNish Nishant7-Jun-02 17:59 
GeneralRe: How did you change the titlebar Pin
Matt Newman8-Jun-02 4:45
Matt Newman8-Jun-02 4:45 

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.