Click here to Skip to main content
15,885,309 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 478.5K   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: Old style solution? Pin
Albert van Peppen12-Jun-02 21:22
professionalAlbert van Peppen12-Jun-02 21:22 
GeneralRe: Old style solution? Pin
Nish Nishant13-Jun-02 1:59
sitebuilderNish Nishant13-Jun-02 1:59 
GeneralRe: Old style solution? Pin
Shog913-Jun-02 5:56
sitebuilderShog913-Jun-02 5:56 
GeneralRe: Old style solution? Pin
Nish Nishant13-Jun-02 8:31
sitebuilderNish Nishant13-Jun-02 8:31 
GeneralRe: Old style solution? Pin
Shog913-Jun-02 8:53
sitebuilderShog913-Jun-02 8:53 
GeneralRe: Old style solution? Pin
Nish Nishant13-Jun-02 2:00
sitebuilderNish Nishant13-Jun-02 2:00 
GeneralRe: Old style solution? Pin
Rama Krishna Vavilala7-Jun-02 6:33
Rama Krishna Vavilala7-Jun-02 6:33 
GeneralRe: Old style solution? Pin
Nish Nishant7-Jun-02 6:46
sitebuilderNish Nishant7-Jun-02 6:46 
Well, I'd be glad to do that Rama Smile | :)

But one funny thing is I won't be getting even one Rupee out of all this. But the pleasure I seek is not money, but the realization that my friends have read my first book and have partially enjoyed it, even if the only reason they actually liked the book is because they like me Smile | :)

Nish

p.s. I wrote it when I was 19-20. Took me a while to find a publisher. Finally found one and I had to bear 16% of printing/publishing costs. That came to Rs 96,000. Then it took them a long while to get it published. Then nothing. I ask them what happened and they said I have to market my own book!!! Blast!

Anyway last month I was chatting with Kannan Kalyanaraman and I coincidentally found my book from google. I mean there were 3-4 web sites selling my books and I didnt even know about it.

I guess I got thoroughly conned. But I don't care Smile | :) I'd be immensly happy if all my CP buddies read my book and tell me they liked it at least a teeny weeny little bit.

Maybe you guys can buy one book and post it to each other as a sort of chain-book.



Author of the romantic comedy

Summer Love and Some more Cricket [New Win]

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

GeneralRe: Old style solution? Pin
Rama Krishna Vavilala7-Jun-02 6:51
Rama Krishna Vavilala7-Jun-02 6:51 
GeneralRe: Old style solution? Pin
Nish Nishant7-Jun-02 7:16
sitebuilderNish Nishant7-Jun-02 7:16 
GeneralRe: Old style solution? Pin
Rama Krishna Vavilala7-Jun-02 7:37
Rama Krishna Vavilala7-Jun-02 7:37 
GeneralRe: Old style solution? Pin
Shog97-Jun-02 17:34
sitebuilderShog97-Jun-02 17:34 
GeneralRe: Old style solution? Pin
Nish Nishant7-Jun-02 17:56
sitebuilderNish Nishant7-Jun-02 17:56 
GeneralRe: Old style solution? Pin
Shog97-Jun-02 18:08
sitebuilderShog97-Jun-02 18:08 
GeneralRe: Old style solution? Pin
Nish Nishant7-Jun-02 18:11
sitebuilderNish Nishant7-Jun-02 18:11 
GeneralRe: Old style solution? Pin
Shog97-Jun-02 18:22
sitebuilderShog97-Jun-02 18:22 
GeneralRe: Old style solution? Pin
Nish Nishant7-Jun-02 18:43
sitebuilderNish Nishant7-Jun-02 18:43 
GeneralRe: Old style solution? Pin
Shog97-Jun-02 18:50
sitebuilderShog97-Jun-02 18:50 
GeneralRe: Old style solution? Pin
Nish Nishant7-Jun-02 19:22
sitebuilderNish Nishant7-Jun-02 19:22 
GeneralNice!! Pin
Ravi Bhavnani7-Jun-02 5:13
professionalRavi Bhavnani7-Jun-02 5:13 
GeneralRe: Nice!! Pin
Nish Nishant7-Jun-02 5:47
sitebuilderNish Nishant7-Jun-02 5:47 
QuestionHow did you change the titlebar Pin
Matt Newman7-Jun-02 4:55
Matt Newman7-Jun-02 4:55 
AnswerRe: How did you change the titlebar Pin
Nish Nishant7-Jun-02 5:10
sitebuilderNish Nishant7-Jun-02 5:10 
GeneralRe: How did you change the titlebar Pin
Matt Newman7-Jun-02 5:18
Matt Newman7-Jun-02 5:18 
GeneralRe: How did you change the titlebar Pin
Shog97-Jun-02 5:37
sitebuilderShog97-Jun-02 5:37 

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.