Click here to Skip to main content
13,248,365 members (39,167 online)
Click here to Skip to main content

Stats

401.4K views
12.2K downloads
90 bookmarked
Posted 6 Jun 2002

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

, 6 Jun 2002
A small CDialog derived class that helps avoid frequent copy/pasting of the same code, when using a bitmap as dialog background
// 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)
		return true;
	
	CRect rect;
	GetClientRect(&rect);
	CDC dc;
	dc.CreateCompatibleDC(pDC);
	CBitmap* pOldBitmap = dc.SelectObject(&m_bitmap);
	int bmw, bmh ;
	BITMAP bmap;
	m_bitmap.GetBitmap(&bmap);
	bmw = bmap.bmWidth;
	bmh = bmap.bmHeight;
	int xo=0, yo=0;

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


	return true;
}

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

}

int CBDialog::SetBitmap(UINT nIDResource)
{
	if(m_bitmap.LoadBitmap(nIDResource))
		return 0;
	else
		return 1;//error
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

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 Nishant
United States United States
Nish Nishant is the Principal Software Architect/Consultant for Ganymede Software Solutions LLC, and is based out of Columbus, Ohio. He has over 17 years of software industry experience in various roles including Lead Software Architect, Principal Software Engineer, and Product Manager. Nish was a Microsoft Visual C++ MVP between 2002 and 2015.

Nish is an industry acknowledged expert in the Microsoft technology stack. He authored C++/CLI in Action for Manning Publications in 2005, and had previously 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 vastly experienced in team management, mentoring teams, and directing all stages of software development.

Contact Nish : If you are interested in hiring Nish as a consultant, you can reach him via his google email id voidnish.

Company Website : www.ganymedesoftwaresolutions.com

You may also be interested in...

Pro
Pro
Permalink | Advertise | Privacy | Terms of Use | Mobile
Web03 | 2.8.171114.1 | Last Updated 7 Jun 2002
Article Copyright 2002 by Nish Nishant
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid