★ wanayoo — archive 1999 http://codeguru.developer.com/controls/IconPickerListBox.shtmlNouvelle recherche | Portail wanayoo
EarthWeb
Developer.com Click Here!
Click Here!
Find:
 

EXPERT SEARCH
SEARCH THE IT WEB -----
Site
windows 2000
visual c++
java
visual basic
javascripts
recommend it
 
Contest
mfc codeguru
submission guidelines
 
Book
thinking in c++
stl programmer's guide
reviews
 
Interact
forums
newsgroups
guest book
jobs
jokes
open faq
what's new
 
Other
about
links
newsletters
privacy
tools
 

-----
Visual C++ by E-mail:

Get the weekly e-mail highlights on
Visual C++!
-----



EarthWeb Sites:
IT Management
Networking & Telecommunications
IT Jobs
Software Development
Hardware & Systems
Commerce
Community

EarthWeb Worldwide

   

Icon Picker List Box


This article was contributed by Jeff Lundgren.

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:

Add Comment

   

Give them a visit Click Here!

Click Here!

INSTANT EXPERTS!
Savvy IT Pros report on SupportSource. Take a look inside the largest vault of multi-vendor computing specs ever assembled. Click here for an In-depth Report,.

For tons of Windows technology resources, check out the Windows Programming directory.

Searching the Web for tech topics just got better with
TechCrawler where all results come only from popular technical sites.

WINNER!!!
Congratulations to James Hole, of New Mexico! The grand prize winner of EarthWeb's Flatten the Obstacles Sweepstakes and now a proud Hummer(R) owner.

Give them a visit Click Here!

Click Here!

Click Here!

Click Here!

Home About Us Search Subscribe Advertising Info Contact Us FAQs
Use of this site is subject to certain Terms & Conditions.
Earthweb's statement regarding our users' right to privacy.
Copyright ©1996-2000 EarthWeb Inc.. All rights reserved. Reproduction in whole or in part in any form or medium without express written permission of EarthWeb is prohibited.