Click here to Skip to main content
15,894,291 members
Articles / Desktop Programming / MFC

CodeWizard--A visual tool to automatically generate C++ code files!

Rate me:
Please Sign up or sign in to vote.
2.36/5 (14 votes)
23 Oct 20032 min read 44.2K   1.3K   24  
CodeWizard includes a tool that lets you visually create functionality on-screen and automatically generate C++ code files!
// GWExtBmpListBox.cpp : implementation file
//

#include "stdafx.h"
#include "resource.h"
#include "GWExtBmpListBox.h"

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


/////////////////////////////////////////////////////////////////////////////
// GWExtBmpListBox

GWExtBmpListBox::GWExtBmpListBox() : CListBox()
{
	m_Bitmap.LoadBitmap(IDB_MB_COMP_TYPE);
	CreateTransBmps(&m_Bitmap, &m_MaskBitmap);
}

GWExtBmpListBox::~GWExtBmpListBox()
{
}

IMPLEMENT_DYNAMIC(GWExtBmpListBox, CListBox)

BEGIN_MESSAGE_MAP(GWExtBmpListBox, CListBox)
//{{AFX_MSG_MAP(GWExtBmpListBox)
ON_WM_CREATE()
ON_WM_DESTROY()
//}}AFX_MSG_MAP
END_MESSAGE_MAP()

void GWExtBmpListBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	ASSERT( lpDIS->CtlType == ODT_LISTBOX );
	
	if (lpDIS->itemID == -1)
		return;
	
	CDC* pDC = CDC::FromHandle(lpDIS->hDC);
	COLORREF crBk, crText;
	TEXTMETRIC tm;
	int x, y;
	
	crBk = pDC->SetBkColor(GetSysColor(lpDIS->itemState & ODS_SELECTED ? COLOR_HIGHLIGHT : COLOR_WINDOW) );
	crText = pDC->SetTextColor(GetSysColor(lpDIS->itemState & ODS_SELECTED ? COLOR_HIGHLIGHTTEXT : COLOR_WINDOWTEXT) );
	
	pDC->GetTextMetrics( &tm );
	x = LOWORD(GetDialogBaseUnits()) / 4;
	y = (lpDIS->rcItem.bottom + lpDIS->rcItem.top - tm.tmHeight) / 2;
	
	CRect temRect = CRect(lpDIS->rcItem);
	temRect.left +=  20 + 2 * x;
	
	CString strFaceName;
	GetText(lpDIS->itemID, strFaceName);
	
	CString strSpaces = "                                                     ";  // 50 spaces
	
	CString strFirst;
	CString strSecond;
	strFirst = strFaceName;
	if(strFaceName.ReverseFind(_T('@'))>0)
	{
		strFirst = strFaceName.Left(strFaceName.ReverseFind(_T('@')));
	}
	strSecond ="";
	strSecond = strFaceName.Right(strFaceName.GetLength() - strFirst.GetLength() -1);

	CString strDraw;
	CString strSpace = "";
	if((strSpaces.GetLength() - strFirst.GetLength())>0)
	{
	   strSpace = strSpaces.Left(strSpaces.GetLength() - strFirst.GetLength());
	}

	strDraw = strFirst +strSpace+strSecond;
	pDC->ExtTextOut(lpDIS->rcItem.left + 20 + 2 * x, y, ETO_CLIPPED | ETO_OPAQUE,
		&lpDIS->rcItem,(LPCTSTR) strDraw,
		lstrlen((LPCTSTR) strDraw), NULL );
	
	pDC->SetTextColor( crText );
	pDC->SetBkColor( crBk );
	
	int dy;
	dy = ((lpDIS->rcItem.bottom - lpDIS->rcItem.top) - 12) / 2;
	DrawTransBmp(pDC, &m_Bitmap, &m_MaskBitmap,x, lpDIS->rcItem.top + dy, 20, 12);
	
	if (lpDIS->itemState & ODS_FOCUS)
		pDC->DrawFocusRect( &lpDIS->rcItem );
}

void GWExtBmpListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	CClientDC dc(this);
	TEXTMETRIC tm;
	dc.GetTextMetrics(&tm);
	lpMeasureItemStruct->itemHeight = tm.tmHeight+6;
}

void GWExtBmpListBox::DrawTransBmp(CDC* pDC, CBitmap* pbmp, CBitmap* pbmpMask,
									   int x, int y, int cx, int cy)
{
	COLORREF oldBkColor = pDC->SetBkColor(RGB(255, 255, 255));
	COLORREF oldTextColor = pDC->SetTextColor(RGB(0, 0, 0));
	
	CDC dcCompat;
	dcCompat.CreateCompatibleDC(pDC);
	CBitmap* pbmpSave = dcCompat.SelectObject(pbmp);
	pDC->BitBlt(x, y, cx, cy, &dcCompat, 0, 0, 0x00660046L);
	dcCompat.SelectObject(pbmpMask);
	pDC->BitBlt(x, y, cx, cy, &dcCompat, 0, 0, 0x00220326L);
	dcCompat.SelectObject(pbmp);
	pDC->BitBlt(x, y, cx, cy, &dcCompat, 0, 0, 0x00660046L);
	dcCompat.SelectObject(pbmpSave);
	
	pDC->SetBkColor(oldBkColor);
	pDC->SetTextColor(oldTextColor);
}

void GWExtBmpListBox::CreateTransBmps(CBitmap* pbmp, CBitmap* pbmpMask)
{
	BITMAP bmp;
	pbmp->GetObject(sizeof (BITMAP), &bmp);
	pbmpMask->CreateBitmap(bmp.bmWidth, bmp.bmHeight, 1, 1, NULL);
	
	CDC dcOld;
	dcOld.CreateCompatibleDC(NULL);
	CDC dcSrc;
	dcSrc.CreateCompatibleDC(NULL);
	dcOld.SelectObject(pbmpMask);
	dcSrc.SelectObject(pbmp);
	
	COLORREF oldBkColor = dcSrc.SetBkColor(dcSrc.GetPixel(0, 0));
	dcOld.BitBlt(0, 0, bmp.bmWidth, bmp.bmHeight, &dcSrc, 0, 0, NOTSRCCOPY);
	dcSrc.SetBkColor(oldBkColor);
}

void GWExtBmpListBox::OnDestroy()
{
	CListBox::OnDestroy();
}

int GWExtBmpListBox::OnCreate(LPCREATESTRUCT lpCreateStruct) 
{
	if (CListBox::OnCreate(lpCreateStruct) == -1)
		return -1;

	return 0;
}

void GWExtBmpListBox::DeleteItem(LPDELETEITEMSTRUCT lpDIS)
{
	CListBox::DeleteItem(lpDIS);
}

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 has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
China China
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.

Comments and Discussions