Click here to Skip to main content
15,881,600 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.1K   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

 
General[modified] got error LNK2001: unresolved external symbol : problem solved Pin
Inair3-Apr-08 0:45
Inair3-Apr-08 0:45 
GeneralReally need help plz plz plz Pin
Mathewsimpson3-Nov-07 14:19
Mathewsimpson3-Nov-07 14:19 
GeneralShow Static text Pin
nugroho231-Oct-07 5:44
nugroho231-Oct-07 5:44 
GeneralRe: Show Static text Pin
Anu Koshy27-May-10 23:15
Anu Koshy27-May-10 23:15 
GeneralRe: Show Static text Pin
Ramveer Singh from Gurgaon, Haryana22-May-11 20:41
Ramveer Singh from Gurgaon, Haryana22-May-11 20:41 
GeneralWorks like a charm - Thx! Pin
DavidCarr5-Apr-07 9:57
DavidCarr5-Apr-07 9:57 
GeneralRe: Works like a charm - Thx! Pin
DavidCarr5-Apr-07 9:59
DavidCarr5-Apr-07 9:59 
QuestionHow to add a .BMP as a resource (IDB_BITMAP1) Pin
leevonk8-Jun-06 9:54
leevonk8-Jun-06 9:54 
GeneralDebug Assertion Pin
30-Jul-05 7:55
suss30-Jul-05 7:55 
QuestionHow to set the bitmap to menus and status bar Pin
Koundinya18-Aug-04 2:46
Koundinya18-Aug-04 2:46 
QuestionHow to make DPI independent dialog Pin
KmAshif20-Jul-04 20:05
KmAshif20-Jul-04 20:05 
Generalstatic, radio button, checkbox, menu Pin
brian scott18-Nov-03 5:36
brian scott18-Nov-03 5:36 
Generalrounded XP buttons Pin
Jason Henderson22-Oct-03 11:21
Jason Henderson22-Oct-03 11:21 
GeneralLoad hi-res bitmap from file Pin
delta00317-Oct-03 19:38
delta00317-Oct-03 19:38 
GeneralNow You Can Implement Transparent Background Pin
Sadru10-Oct-03 5:34
Sadru10-Oct-03 5:34 
I have modify the code so that it can also implement the transparent background to the dialog ---- So you can watermark your company logo on the background.

Create the bitmap you want to display and also create the mask. (In the mask, whatever is white need to be converted to black and all other color needed to convert the black)



Modify BDialog.h
#define StyleTile 0
#define StyleCenter 1
#define StyleStretch 2

class CBDialog : public CDialog
{
// Construction
public:
int SetBitmap(UINT nIDResource,UINT nIDResourcemask=0);
void SetBitmapStyle(int style);
CBDialog(UINT nIDTemplate, CWnd* pParent = NULL); // standard constructor

// Dialog Data
//{{AFX_DATA(CBDialog)
// NOTE: the ClassWizard will add data members here
//}}AFX_DATA


// Overrides
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CBDialog)
protected:
//}}AFX_VIRTUAL

// Implementation
protected:

// Generated message map functions
//{{AFX_MSG(CBDialog)
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
private:
CBitmap m_bitmap;
CBitmap m_bitmap_mask;
int m_style;
};

//{{AFX_INSERT_LOCATION}}
// Microsoft Visual C++ will insert additional declarations immediately before the previous line.

#endif // !defined(AFX_BDIALOG_H__A303185C_4254_4F5F_B5A8_462531A59B83__INCLUDED_)



Modify the BDialog.cpp

// BDialog.cpp : implementation file
//

#include "stdafx.h"
#include "BDialog.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CBDialog dialog


CBDialog::CBDialog(UINT nIDTemplate, CWnd* pParent /*=NULL*/)
: CDialog(nIDTemplate, pParent)
{
//{{AFX_DATA_INIT(CBDialog)
// NOTE: the ClassWizard will add member initialization here
m_style=StyleTile;
//}}AFX_DATA_INIT
}


BEGIN_MESSAGE_MAP(CBDialog, CDialog)
//{{AFX_MSG_MAP(CBDialog)
ON_WM_ERASEBKGND()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CBDialog message handlers

BOOL CBDialog::OnEraseBkgnd(CDC* pDC)
{
CDialog::OnEraseBkgnd(pDC);
if(!m_bitmap.m_hObject && !m_bitmap_mask.m_hObject)
return true;

if (!m_bitmap_mask.m_hObject){ //old way
CBitmap* pOldBitmap = NULL;
CRect rect;
CDC dc;
int bmw, bmh ;
BITMAP bmap;
int xo=0, yo=0;

GetClientRect(&rect);
dc.CreateCompatibleDC(pDC);
pOldBitmap = dc.SelectObject(&m_bitmap);
m_bitmap.GetBitmap(&bmap);
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;

if(m_style == StyleTile){
for (yo = 0; yo < rect.Height(); yo += bmh){
for (xo = 0; xo < rect.Width(); xo += bmw){
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
}
}
}

if(m_style == StyleCenter){
if(bmw < rect.Width())
xo = (rect.Width() - bmw)/2;
else
xo=0;
if(bmh < rect.Height())
yo = (rect.Height()-bmh)/2;
else
yo=0;
pDC->BitBlt (xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0, SRCCOPY);
}

if(m_style == StyleStretch){
pDC->StretchBlt(xo, yo, rect.Width(),
rect.Height(), &dc,
0, 0,bmw,bmh, SRCCOPY);
}


dc.SelectObject(pOldBitmap);
::DeleteObject(dc);
}
else {
// transparent background image
CRect rect;
int x0=0, y0=0;
int cx = 0, cy = 0;
GetClientRect(&rect);
int bmw, bmh ;
BITMAP bmap;
m_bitmap.GetBitmap(&bmap);
bmw = bmap.bmWidth;
bmh = bmap.bmHeight;
x0 =rect.left;
x0 =rect.top;
cx = rect.Width();
cy = rect.Height();

if(m_style == StyleCenter){
if(bmw < cx)
x0 = (cx - bmw)/2;
else
x0=0;
if(bmh < cy)
y0 = (cy-bmh)/2;
else
y0=0;
}


CDC dcBmp,dcMask;
dcBmp.CreateCompatibleDC(pDC);
dcBmp.SelectObject(&m_bitmap);
dcMask.CreateCompatibleDC(pDC);
dcMask.SelectObject(&m_bitmap_mask);

CDC hdcMem;
hdcMem.CreateCompatibleDC(pDC);
CBitmap hBitmap;
if(m_style == StyleCenter)
hBitmap.CreateCompatibleBitmap(pDC,bmw,bmh);
else if(m_style == StyleStretch || m_style == StyleTile)
hBitmap.CreateCompatibleBitmap(pDC,cx,cy);
else{
}
hdcMem.SelectObject(hBitmap);

hdcMem.BitBlt(0,0,bmw,bmh,pDC,0,0,SRCCOPY);
hdcMem.BitBlt(0,0,bmw,bmh,&dcBmp,0,0,SRCINVERT);
hdcMem.BitBlt(0,0,bmw,bmh,&dcMask,0,0,SRCAND);
hdcMem.BitBlt(0,0,bmw,bmh,&dcBmp,0,0,SRCINVERT);
if(m_style == StyleCenter)
pDC->BitBlt (x0,y0,bmw,bmh,&hdcMem,0,0,SRCCOPY);
else if(m_style == StyleStretch)
pDC->StretchBlt (0,0,cx,cy,&hdcMem,0,0,bmw,bmh,SRCCOPY);
else{
for (y0 = 0; y0 < rect.Height(); y0 += bmh){
for (x0 = 0; x0 < rect.Width(); x0 += bmw){
pDC->BitBlt (x0, y0, bmw, bmh, &hdcMem, 0, 0, SRCCOPY);
}
}
}

hdcMem.DeleteDC();
hBitmap.DeleteObject();
DeleteDC(dcMask);
DeleteDC(dcBmp);
}
return true;
}

void CBDialog::SetBitmapStyle(int style)
{
if((style==StyleTile)||
(style==StyleCenter)||
(style==StyleStretch))
{
m_style = style;
}

}

int CBDialog::SetBitmap(UINT nIDResource,UINT nIDResourcemask)
{
if(m_bitmap.LoadBitmap(nIDResource)){
if (nIDResourcemask > 0){
if(!m_bitmap_mask.LoadBitmap(nIDResource))
return 1;
}
return 0;

}
return 1;//error
}


Have fun!
QuestionIs it possible? Pin
speedpacer27-Sep-03 9:22
speedpacer27-Sep-03 9:22 
AnswerRe: Is it possible? Pin
Nish Nishant27-Sep-03 17:57
sitebuilderNish Nishant27-Sep-03 17:57 
GeneralRe: Is it possible? Pin
speedpacer28-Sep-03 11:24
speedpacer28-Sep-03 11:24 
GeneralThank you! Pin
Hosam Aly Mahmoud12-Aug-03 17:56
Hosam Aly Mahmoud12-Aug-03 17:56 
GeneralHelp with CEdit Pin
SatyaKiran18-Oct-02 12:21
SatyaKiran18-Oct-02 12:21 
GeneralStatic control bg color Pin
kobil22-Jul-02 22:00
kobil22-Jul-02 22:00 
GeneralRe: Static control bg color Pin
Nish Nishant18-Oct-02 14:28
sitebuilderNish Nishant18-Oct-02 14:28 
Generalerm vc6.0 ? i think not Pin
bryce17-Jun-02 18:58
bryce17-Jun-02 18:58 
GeneralRe: erm vc6.0 ? i think not Pin
Nish Nishant18-Jun-02 18:30
sitebuilderNish Nishant18-Jun-02 18:30 
GeneralPretty good... Pin
Tim Lesher10-Jun-02 6:45
Tim Lesher10-Jun-02 6: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.