Click here to Skip to main content
11,481,265 members (65,038 online)
Click here to Skip to main content
Add your own
alternative version

MFC Extension library : Enhanced print preview plug-in

, 24 May 2004 CPOL 51.7K 1.5K 47
MFC Extension library : Enhanced print preview plug-in
// MappedBitmapButton.cpp : implementation file
// Draws color-correct buttons
// Copyright (c) 1999 Robin J. Leatherbarrow, Erithacus Software Limited
// Use and distribute freely, except: don't remove my name from the
// source or documentation, mark your changes, don't alter
// or remove this notice.
// No warrantee of any kind, express or implied, is included with this
// software; use at your own risk, responsibility for damages (if any) to
// anyone resulting from the use of this software rests entirely with the
// user.
// Send bug reports, bug fixes, enhancements, requests, flames, etc.,
// and I'll try to keep a version up to date.  I can be reached at:
// Usage -- a replacement for the MFC CBitmapButton
// 1. include an owner-draw button in your dialog
// 2. declare a CMappedBitmapButton member in the CDialog code
// 3. hook in the CMappedBitmapButton using a call to AutoLoad
// the bitmap resource specified in AutoLoad must be divisable into
// 4 equally sized images that represent (left to right) the
// up, down, focused and disabled states of the button

#include "stdafx.h"
#include "MappedBitmapButton.h"

IMPLEMENT_DYNAMIC( CMappedBitmapButton, CButton )

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

// CMappedBitmapButton

	m_idResource = (UINT)-1;


BEGIN_MESSAGE_MAP(CMappedBitmapButton, CButton)

BOOL CMappedBitmapButton::LoadBitmap( UINT idBitmapResource )

	m_idResource = idBitmapResource;

	CBitmap bitImage;
	if (!bitImage.LoadMappedBitmap( idBitmapResource )) {
		ASSERT( FALSE ); // failed to load the bitmap. Does this resource ID exist?
		return FALSE;

	if (0==bitImage.GetBitmap( &bm ))
		return FALSE;

	// we don't use a mask for the bitmaps in this case
	UINT flags = 0;
	if (bm.bmBitsPixel <= 4)
		flags |= ILC_COLOR4;
	else if (bm.bmBitsPixel <= 8)
		flags |= ILC_COLOR8;
	else if (bm.bmBitsPixel <= 16)
		flags |= ILC_COLOR16;
		flags |= ILC_COLOR24;

	// image should contain EXACTLY four subimages
	ASSERT( bm.bmWidth % 4 == 0 );

	if (!m_image.Create( bm.bmWidth/4, bm.bmHeight, flags, 4, 0 ))
		return FALSE;

	// the mask bitmap will be ignored here, so use the	bitImage for both
	if (-1 == m_image.Add( &bitImage, &bitImage ))
		return FALSE;

	return TRUE;

// SizeToContent will resize the button to the size of the bitmap
void CMappedBitmapButton::SizeToContent()
	ASSERT(m_image.GetImageCount() == 4);

	VERIFY( m_image.GetImageInfo( 0, &info ) );
	VERIFY(SetWindowPos(NULL, -1, -1, info.rcImage.right, info.rcImage.bottom,

// Autoload will load the bitmap resources
BOOL CMappedBitmapButton::AutoLoad(UINT nID, CWnd* pParent, UINT idBitmapResource)
	// first attach the CMappedBitmapButton to the dialog control
	if (!SubclassDlgItem(nID, pParent))
		return FALSE;

	if (!LoadBitmap(idBitmapResource))
		return FALSE;

	// size to content
	return TRUE;

// CMappedBitmapButton message handlers

void CMappedBitmapButton::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
	ASSERT(lpDrawItemStruct != NULL);
	ASSERT(m_image.GetImageCount() == 4);

	// images are:
	// 0 = up
	// 1 = down
	// 2 = focused
	// 3 = disabled
	int image = 0;
	UINT state = lpDrawItemStruct->itemState;
	if (state & ODS_SELECTED)
		image = 1;
	else if (state & ODS_FOCUS)
		image = 2;   // third image for focused
	else if (state & ODS_DISABLED)
		image = 3;   // last image for disabled

	// draw the whole button
	CRect rect;

	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	VERIFY( m_image.Draw( pDC, image, rect.TopLeft(), ILD_NORMAL ) );

void CMappedBitmapButton::OnSysColorChange() 
	if (m_image.GetImageCount() == 4) {
		LoadBitmap( m_idResource );


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.


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


About the Author

Roger Allen
Software Developer (Senior) Sirius Analytical Instruments
United Kingdom United Kingdom
A research and development programmer working for a pharmaceutical instrument company for the past 17 years.

I am one of those lucky people who enjoys his work and spends more time than he should either doing work or reseaching new stuff. I can also be found on playing DDO on the Cannith server (Send a tell to "Maetrim" who is my current main)

I am also a keep fit fanatic, doing cross country running and am seriously into [url][/url] Karate at this time of my life, training from 4-6 times a week and recently achieved my 1st Dan after 6 years.

| Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.150520.1 | Last Updated 25 May 2004
Article Copyright 2004 by Roger Allen
Everything else Copyright © CodeProject, 1999-2015
Layout: fixed | fluid