Icon Picker List Box

Icon Picker List Box (7600 bytes) Select which icon you want.

Download Source Code and Example

If you've used any version of windows you've changed the icon for a shortcut.  The dialog used allows you to select from a list of icons found in a DLL, EXE or even an ICO file.  I needed such a control, so I wrote one.  It is basically an owner draw list box.  The only limitation here is I really wanted a horizontal listbox, but ended up with a vertical one.  The only limitation is the box must be sized to the icons at design time, although modifications on my code could be made to support it.

If anyone knows a way to get it horizontal, let me know.  I tried multicolumn, but it didn't work very well.

Feel free to modify and use this code anyway you like, it's not really hard stuff so I'm not going to place any restrictions on it.

Create a class derived from CListBox

class CIconPickerList : public CListBox
{
// Construction
public:
	CIconPickerList();

// Attributes
public:

// Operations
public:
	int AddIconItem(HICON hIcon); // My Function
// Overrides
	// ClassWizard generated virtual function overrides
	//{{AFX_VIRTUAL(CIconPickerList)
	public:
	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
	virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
	//}}AFX_VIRTUAL

// Implementation
public:
	virtual ~CIconPickerList();

	// Generated message map functions
protected:
	//{{AFX_MSG(CIconPickerList)
		// NOTE - the ClassWizard will add and remove member functions here.
	//}}AFX_MSG

	DECLARE_MESSAGE_MAP()
};
// Implement below
void CIconPickerList::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct) 
{
	CDC* pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
	HICON hIcon = (HICON)lpDrawItemStruct->itemData; // HICON in Item data

	if (lpDrawItemStruct->itemAction & ODA_DRAWENTIRE)
	{
		// Paint the color item in the color requested
		pDC->DrawIcon(lpDrawItemStruct->rcItem.left+5,lpDrawItemStruct->rcItem.top+5,hIcon);
	}

	if ((lpDrawItemStruct->itemState & ODS_SELECTED) &&
		(lpDrawItemStruct->itemAction & (ODA_SELECT | ODA_DRAWENTIRE)))
	{
		// item has been selected - hilite frame
		CBrush br(GetSysColor(COLOR_HIGHLIGHT));
		CRect rect;
		rect.CopyRect(&lpDrawItemStruct->rcItem);
		rect.DeflateRect(2,2,2,2);
		pDC->FrameRect(&rect, &br);

		rect.DeflateRect(1,1,1,1);
		pDC->FrameRect(&rect, &br);

	}

	if (!(lpDrawItemStruct->itemState & ODS_SELECTED) &&
		(lpDrawItemStruct->itemAction & ODA_SELECT))
	{
		// Item has been de-selected -- remove frame
		CBrush br(GetSysColor(COLOR_WINDOW));
		CRect rect;
		rect.CopyRect(&lpDrawItemStruct->rcItem);
		rect.DeflateRect(2,2,2,2);
		pDC->FrameRect(&rect, &br);

		rect.DeflateRect(1,1,1,1);
		pDC->FrameRect(&rect, &br);
	}
	
}

void CIconPickerList::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct) 
{
	lpMeasureItemStruct->itemHeight = 42;
	lpMeasureItemStruct->itemWidth = 42;
}

int CIconPickerList::AddIconItem(HICON hIcon)
{
	return AddString((LPCTSTR) hIcon);
}

Last updated: Aug 11 1998



Comments

  • There are no comments yet. Be the first to comment!

Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: November 20, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT Are you wanting to target two or more platforms such as iOS, Android, and/or Windows? You are not alone. 90% of enterprises today are targeting two or more platforms. Attend this eSeminar to discover how mobile app developers can rely on one IDE to create applications across platforms and approaches (web, native, and/or hybrid), saving time, money, and effort and introducing apps to market faster. You'll learn the trade-offs for gaining long …

  • Live Event Date: October 29, 2014 @ 11:00 a.m. ET / 8:00 a.m. PT Are you interested in building a cognitive application using the power of IBM Watson? Need a platform that provides speed and ease for rapidly deploying this application? Join Chris Madison, Watson Solution Architect, as he walks through the process of building a Watson powered application on IBM Bluemix. Chris will talk about the new Watson Services just released on IBM bluemix, but more importantly he will do a step by step cognitive …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds