Click here to Skip to main content
15,886,518 members
Articles / Web Development / HTML

A Comprehensive CE Class Library to Replace ATL and MFC

Rate me:
Please Sign up or sign in to vote.
4.48/5 (14 votes)
4 Oct 2000CPOL 279K   998   70  
A collection of classes for CE that do not use ATL or MFC, plus an FTP client, database viewer, and sample application that solves beam deflection equations.
// ImageComboBox.cpp : implementation file
//

#include "stdafx.h"
#include "ImageComboBox.h"

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

/////////////////////////////////////////////////////////////////////////////
// CImageComboBox

CImageComboBox::CImageComboBox()
{
}

CImageComboBox::~CImageComboBox()
{
}


BEGIN_MESSAGE_MAP(CImageComboBox, CComboBox)
	//{{AFX_MSG_MAP(CImageComboBox)
	ON_WM_KEYDOWN()
	ON_WM_SETFOCUS()
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CImageComboBox message handlers

void CImageComboBox::OnDeleteItem(LPDELETEITEMSTRUCT lpDelIS) 
{
	if (lpDelIS->itemData && lpDelIS->itemID != -1)
		delete (CIconIndent*)lpDelIS->itemData;
}

void CImageComboBox::OnDrawItem(LPDRAWITEMSTRUCT lpDIS) 
{
	//////////////////////////////////////////////////////////////////////////////
	// Alias the given device context to simplify GDI a little
	//////////////////////////////////////////////////////////////////////////////
	HDC hDC = lpDIS->hDC;
	CeRect rect(lpDIS->rcItem);
	
	//////////////////////////////////////////////////////////////////////////////
	// Handle the case of no items
	//////////////////////////////////////////////////////////////////////////////
	if (lpDIS->itemID == -1)
	{
		if (lpDIS->itemState & ODS_FOCUS)
			::DrawFocusRect (hDC, rect);
		return;
	}

	//////////////////////////////////////////////////////////////////////////////
	// Determine the indentation level and image index from the item data
	//////////////////////////////////////////////////////////////////////////////
	int nIndentSteps	= 0;
	int nImageIndex		= 0;
	if (lpDIS->itemData)
	{
		CIconIndent* pIconIndent = (CIconIndent*)lpDIS->itemData;

		nIndentSteps	= pIconIndent->m_nIndentSteps;
		nImageIndex		= pIconIndent->m_nImageIndex;
	}

	if (lpDIS->itemState & ODS_COMBOBOXEDIT)
		nIndentSteps = 0;
	
	//////////////////////////////////////////////////////////////////////////////
	// Determine the indentation width from the icon size
	//////////////////////////////////////////////////////////////////////////////
	int nWidth, nHeight;
	if (m_pImages && m_pImages->m_hImageList)
	{
		IMAGEINFO ii;
		ASSERT(0 < m_pImages->GetImageCount());
		m_pImages->GetImageInfo(0, &ii);

		nWidth = (ii.rcImage.right - ii.rcImage.left) / 2;
		nHeight = ii.rcImage.bottom - ii.rcImage.top;
	}
	else
		nWidth = 8;
	
	//////////////////////////////////////////////////////////////////////////////
	// Draw the icon for the list item
	//////////////////////////////////////////////////////////////////////////////
	if (nImageIndex >= 0 && m_pImages && m_pImages->m_hImageList)
	{
		DWORD dwStyle = ILD_NORMAL;
		if (lpDIS->itemState & ODS_SELECTED)
			dwStyle = ILD_SELECTED;
		
		CePoint pt(rect.left + nWidth * nIndentSteps + 2, rect.top);
//		ASSERT(nImageIndex < m_pImages->GetImageCount());
		m_pImages->Draw(pDC, nImageIndex, pt, dwStyle);
	}
	
	//////////////////////////////////////////////////////////////////////////////
	// Draw the text rectangle and text for the list item
	//////////////////////////////////////////////////////////////////////////////
	CeString<64> strText;
	GetLBText(lpDIS->itemID, strText);

	HFONT hFont = GetFont();
	SelectObject(hDC, hFont);

	CSize sizeText;
	::GetTextExtentExPoint(hDC, strText, strText.GetLength(), 0, NULL, NULL, sizeText);

	rect.left += nWidth * (nIndentSteps + 2) + 4;
	rect.right = rect.left + sizeText.cx + 3;
	
	::SaveDC(hDC);

	if (lpDIS->itemState & ODS_SELECTED)
	{
		COLORREF clrHighlight = ::GetSysColor(COLOR_HIGHLIGHT);
		COLORREF clrHighlightText = ::GetSysColor(COLOR_HIGHLIGHTTEXT);

		HBRUSH br = ::CreateSolidBrush(clrHighlight);

		::FillRect(hDC, rect, &br);
		::SetBkColor(hDC, clrHighlight);
		::SetTextColor(hDC, clrHighlightText);

		::Destroy (br);
	}
	else
	{
		HBRUSH br = ::CreateSolidBrush( ::GetSysColor(COLOR_WINDOW) );
		pDC->FillRect(rect, &br);

		::Destroy (br);
	}
	
	CeRect rectText = rect;
	rectText.left += 1;
	rectText.top  += 1;
	::DrawText(hDC, strText, -1, rectText, DT_TOP|DT_LEFT);

	::RestoreDC(hDC, -1);
	
	//////////////////////////////////////////////////////////////////////////////
	// Draw the focus rectangle for the list item
	//////////////////////////////////////////////////////////////////////////////
	if (lpDIS->itemState & ODS_FOCUS)
		::DrawFocusRect(hDC, rect);
}

void CImageComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS) 
{
	int nOffset = (lpMIS->itemID == -1) ? -2: 0;

	if (m_pImages && m_pImages->m_hImageList)
	{
		IMAGEINFO ii;
		m_pImages->GetImageInfo(0, &ii);
		lpMIS->itemHeight = ii.rcImage.bottom - ii.rcImage.top;
	}
	else
		lpMIS->itemHeight = 16 + nOffset;
}

void CImageComboBox::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) 
{
	// TODO: Add your message handler code here and/or call default
	
	CeComboBox::OnKeyDown(nChar, nRepCnt, nFlags);
}

void CImageComboBox::OnSetFocus(CWnd* pOldWnd) 
{
	CeComboBox::OnSetFocus(pOldWnd);
	
//	if (::GetKeyState(VK_TAB) & 0x8000) // Is the tab key down?
//		pOldFocusWindow = NULL;
//	else
//		pOldFocusWindow = OldFocus;
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int CImageComboBox::AddString(LPCTSTR lpszString, int nImage, int nSteps, DWORD dwData)
{
	// Add the string
	int nIndex = CeComboBox::AddString(lpszString);
	CeComboBox::SetItemDataPtr(nIndex, new CIconIndent(nSteps, nImage, dwData));
	return nIndex;
}


////////////////////////////////////////////////////////////////////////////////
////////////////////////////////////////////////////////////////////////////////
int CImageComboBox::InsertString(int nIndex, LPCTSTR lpszString, int nImage, int nSteps, DWORD dwData)
{
	int nActIndex = CeComboBox::InsertString(nIndex, lpszString);
	CeComboBox::SetItemDataPtr(nActIndex, new CIconIndent(nSteps, nImage, dwData));

	return nActIndex;
}


DWORD CImageComboBox::GetItemData(int nIndex) const
{
	CIconIndent* pInfo = (CIconIndent*) CeComboBox::GetItemDataPtr(nIndex);
	return (pInfo) ? pInfo->m_dwItemData: 0;
}


void* CImageComboBox::GetItemDataPtr(int nIndex) const
{
	return (void*) GetItemData(nIndex);
}


int CImageComboBox::SetItemData(int nIndex, DWORD dwItemData)
{
	CIconIndent* pInfo = (CIconIndent*) CeComboBox::GetItemDataPtr(nIndex);
	if (pInfo)
		pInfo->m_dwItemData = dwItemData;
	else
		return CB_ERR;

	return 0;
}


int CImageComboBox::SetItemDataPtr(int nIndex, void* pData)
{
	return SetItemData(nIndex, (DWORD) pData);
}


void CImageComboBox::PreSubclassWindow()
{
	CeComboBox::PreSubclassWindow();

	// Attempt to make the combobox shorter (to match the Explorer)
	int Height = GetItemHeight(-1) - 2;
	SetItemHeight(-1, Height);
}

BOOL CImageComboBox::DestroyWindow() 
{
	int nCnt = GetCount();
	for (int ii = 0; ii < nCnt; ii++)
	{
		CIconIndent* pInfo = (CIconIndent*) CComboBox::GetItemDataPtr(ii);
		if (pInfo)
		{
			delete pInfo;
			CeComboBox::SetItemDataPtr(ii, NULL);
		}
	}
	
	return CeComboBox::DestroyWindow();
}

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)


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

Comments and Discussions