Click here to Skip to main content
15,895,142 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 282.1K   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.
#include "stdafx.h"
#include "IconComboBox.h"

//////////////////////////////////////////////////////
//                                                  //
//                   CIconComboBox                  //
//                                                  //
// Copyright (c) Joel Wahlberg ENATOR Networks 1997 //
//               joel.wahlberg@enator.se            //
//                                                  //
//////////////////////////////////////////////////////

CeIconComboBox::CeIconComboBox()
{
	m_sizeIcon.cx = ::GetSystemMetrics(SM_CXICON); 
	m_sizeIcon.cy = ::GetSystemMetrics(SM_CXICON); 

	m_hImageList = NULL;
}

CeIconComboBox::~CeIconComboBox()
{
}

extern HINSTANCE m_hInst;

int CeIconComboBox::AddIcon(LPCTSTR lpszIconFile)
{
	HICON hIcon;
	UINT nRet = ::ExtractIconEx(lpszIconFile, 0, &hIcon, NULL, 1);

	if (! nRet || hIcon == (HICON) 1 || hIcon == NULL)
		return CB_ERR;

	int nIndex = CeComboBox::AddString(lpszIconFile);

	if (nIndex != CB_ERR && nIndex != CB_ERRSPACE)
		SetItemData(nIndex, (DWORD)hIcon);
	
	return nIndex;
}

int CeIconComboBox::InsertIcon(int nIndex, LPCTSTR lpszIconFile)
{
	HICON hIcon;
	UINT nRet = ::ExtractIconEx(lpszIconFile, 0, &hIcon, NULL, 1);

	if (! nRet || hIcon == (HICON)1 || hIcon == NULL)
		return CB_ERR;

	nIndex = CeComboBox::InsertString(nIndex, lpszIconFile);

	if (nIndex != CB_ERR && nIndex != CB_ERRSPACE)
		SetItemData(nIndex, (DWORD)hIcon);
	
	return nIndex;
}

int CeIconComboBox::SelectIcon(LPCTSTR lpszIconFile)
{
	int nIndex = CeComboBox::FindStringExact(-1, lpszIconFile);
	
	if (nIndex == CB_ERR && CeComboBox::GetCount() > 0)
		nIndex = 0; // select the first icon if not found

	return CeComboBox::SetCurSel(nIndex);
}

int CeIconComboBox::SelectIcon(int nIndex)
{
	return CeComboBox::SetCurSel(nIndex);
}

int CeIconComboBox::DeleteIcon(LPCTSTR lpszIconFile)
{
	int nIndex = CeComboBox::FindStringExact(-1, lpszIconFile);

	return CeComboBox::DeleteString(nIndex);
}

int CeIconComboBox::DeleteIcon(int nIndex)
{
	return CeComboBox::DeleteString(nIndex);
}

int CeIconComboBox::AddString(LPCTSTR lpszString)
{
	ASSERT(FALSE); // cannot add strings 
	return CB_ERR;
}

int CeIconComboBox::InsertString(int nIndex, LPCTSTR lpszString)
{
	ASSERT(FALSE); // cannot insert strings
	return CB_ERR;
}

int CeIconComboBox::DeleteString(int nIndex)
{
	ASSERT(FALSE); // cannot delete strings
	return CB_ERR;
}

void CeIconComboBox::MeasureItem(LPMEASUREITEMSTRUCT lpMIS)
{ 
	lpMIS->itemHeight = (m_sizeIcon.cy + 2);
}

void CeIconComboBox::DrawItem(LPDRAWITEMSTRUCT lpDIS)
{
	HDC hDC = lpDIS->hDC;

	if (! IsWindowEnabled())
	{
        HBRUSH hbrDisabled = ::CreateSolidBrush(RGB(192,192,192)); // light gray

		HBRUSH hOldBrush = (HBRUSH)::SelectObject(hDC, (HGDIOBJ) hbrDisabled);
		HPEN hpenDisabled = ::CreatePen(PS_SOLID, 1, RGB(192,192,192));
        HPEN hOldPen = (HPEN) ::SelectObject(hDC, (HGDIOBJ) hpenDisabled);

        OnOutputIcon(lpDIS, FALSE);

		::SelectObject(hDC, (HGDIOBJ) hOldBrush);
		::SelectObject(hDC, (HGDIOBJ) hOldPen);
		return;
	}

	// Selected
	if ((lpDIS->itemState & ODS_SELECTED) 
		&& (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) 
	{
		// create ojbects
		HBRUSH hbrHighlight = ::CreateSolidBrush(::GetSysColor(COLOR_HIGHLIGHT));
		HBRUSH hOldBrush = (HBRUSH) ::SelectObject(hDC, (HGDIOBJ) hbrHighlight);
		HPEN hpenHighlight = ::CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_HIGHLIGHT));
		HPEN hOldPen = (HPEN) ::SelectObject(hDC, (HGDIOBJ) hpenHighlight);

		// draw
		::Rectangle(hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom);
		::SetBkColor(hDC, ::GetSysColor(COLOR_HIGHLIGHT));
       	::SetTextColor(hDC, ::GetSysColor(COLOR_HIGHLIGHTTEXT));

		OnOutputIcon(lpDIS, TRUE);

		//
		::SelectObject(hDC, (HGDIOBJ) hOldBrush);
		::SelectObject(hDC, (HGDIOBJ) hOldPen);
	}

	// De-Selected
	if (!(lpDIS->itemState & ODS_SELECTED) 
		&& (lpDIS->itemAction & (ODA_SELECT | ODA_DRAWENTIRE))) 
	{
		HBRUSH hbrWindow = ::CreateSolidBrush(::GetSysColor(COLOR_WINDOW)); 
		HBRUSH hOldBrush = (HBRUSH) ::SelectObject(hDC, hbrWindow);
		HPEN hpenHighlight = ::CreatePen(PS_SOLID, 1, ::GetSysColor(COLOR_WINDOW));
		HPEN hOldPen = (HPEN) ::SelectObject(hDC, &hpenHighlight);

		::Rectangle(hDC, lpDIS->rcItem.left, lpDIS->rcItem.top, lpDIS->rcItem.right, lpDIS->rcItem.bottom);
		::SetBkColor(hDC, ::GetSysColor(COLOR_WINDOW));
       	::SetTextColor(hDC, ::GetSysColor(COLOR_WINDOWTEXT));

		OnOutputIcon(lpDIS, FALSE);

		::SelectObject(hDC, (HGDIOBJ) hOldBrush);
		::SelectObject(hDC, (HGDIOBJ) hOldPen);
	}

    // Focus
    if (lpDIS->itemAction & ODA_FOCUS) 
        ::DrawFocusRect(hDC, &lpDIS->rcItem);
}

void CeIconComboBox::OnOutputIcon(LPDRAWITEMSTRUCT lpDIS, BOOL bSelected)
{
	if (GetCurSel() == CB_ERR || GetCount() == 0) 
		return; // no item selected
    
	HDC hDC = lpDIS->hDC;

	HICON hIcon = (HICON) lpDIS->itemData;
	ASSERT(hIcon != NULL);

	int x = lpDIS->rcItem.left + ((lpDIS->rcItem.right - lpDIS->rcItem.left) / 2) - (m_sizeIcon.cx / 2);
	int y = lpDIS->rcItem.top + ((lpDIS->rcItem.bottom - lpDIS->rcItem.top) / 2) - (m_sizeIcon.cy / 2);
	
	::DrawIcon(hDC, x, y, hIcon);
}               

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