Icon Select the combo box, I want everyone to see in the QQ personal settings, there can be a lot of avatars inside, you can choose one anytime. Below is a simple step of establishing a combo box for selecting icons:
1. This step is obvious, you have to add .cpp and .h files to your project.
2. Add a combination of a combo box in your project, the style of the combo box is on the default setting.
3. Define a control variable using the VC's class wizard to the combo box.
4. In the header file, use CCionCombox instead of CCOMBOBOX, so you can truly use the following.
After the above steps, you can use the following functions to insert, select, and delete icons.
PUBLIC:
Virtual Int Addicon (LPCTSTR LPSZICONFILENAME);
Virtual Int Insertcon (int NINDEX, LPCTSTSTR LPSZCIONFILENAME);
Virtual Int Selecticon (LPCTSTSTSTSZCIONFILENAME);
Virtual Int Selecticon (INT NINDEX);
Virtual Int deleteicon (LPCTSTR LPSZICONFILENAME);
Virtual Int deleteicon (INT NINDEX);
The following function is to display the icon.
protected:
Virtual Void onoutputicon (LPDrawItemstruct LPDIS, BOOL BSELECTED);
You can also set the size through the following functions to accommodate the icon size difference.
M_cbmyiconcomboBox.SetItemHeight (-1, m_cbmyiconcomboBox.m_sizeicon.cy 6);
Of course, there are other features waiting for you to discover. If you really like this feature, you still go to see all the code, this will be more beneficial to your programming, because I have a limited level, I can't Extension it, I hope some readers can extend it. Here are the .h and .cpp files:
//head File
// iconcomboBox.h: header file
//
#ifndef _ICONCOMBOBOX_H_
#define _ICONCOMBOX_H_H
//
///
// CiconComboBox ////
///
// Copyright (c) Joel Wahlberg Enator Networks 1997 //
// joel.wahlberg@enator.se //
///
//
Class CiconComboBox: Public CCOMBOBOX
{
// Contruction / Destruction
PUBLIC:
CiconComboBox ();
Virtual ~ ciconcomboBOX ();
// attributes
PUBLIC:
CSIZE M_SIZEICON;
// Operations
PUBLIC:
Virtual Int Addicon (LPCTSTR LPSZICONFILE);
Virtual Int Insertcon (int NINDEX, LPCTSTSTR LPSZCILE);
Virtual Int Selecticon (LPCTSTSTR LPSZICONFILE); Virtual Int Selecticon (INT NINDEX);
Virtual Int deleteicon (LPCTSTR LPSZICONFILE);
Virtual Int deleteicon (INT NINDEX);
// Implementation
protected:
Virtual Void onoutputicon (LPDrawItemstruct LPDIS, BOOL BSELECTED);
// Overrides
Virtual int Addstring (LPCTSTR LPSZSTRING);
Virtual Int InsertString (int NINDEX, LPCTSTSTR LPSZSTRING);
Virtual Int deleteString (INT NINDEX);
Virtual Void MeasureItem (LpMeasureItemstruct LPMIS);
Virtual Void DrawItem (LPDrawItemstruct LPDIS);
}
#ENDIF / / _ICONCOMBOBOX_H_
// below is the source file
#include "stdafx.h"
#include "iconcomboBox.h"
#ifdef _Debug
#define new debug_new
#undef this_file
Static char based_code this_file [] = __file__;
#ENDIF
//
///
// CiconComboBox ////
///
// Copyright (c) Joel Wahlberg Enator Networks 1997 //
// joel.wahlberg@enator.se //
///
//
CiconComboBox :: CiconComboBox ()
{
m_sizeicon.cx = :: getSystemMetrics (SM_CXICON);
m_sizeicon.cy = :: getSystemMetrics (SM_CXICON);
}
CiconComboBox :: ~ ciconcomboBox ()
{
}
INT CICONCOMBOBOX :: Addicon (LPCTSTR LPSZICONFILE)
{
Hicon Hicon = :: Extracticon (AfxgetInstanceHandle (), LPSZICONFILE, 0);
IF (hicon == (hicon) 1 || hicon == null)
RETURN CB_ERR;
INT NINDEX = CCOMBOBOX :: AddString (lpsziconfile);
IF (Nindex! = CB_ERR && NINDEX! = CB_ERRSPACE)
SETITEMDATA (Nindex, (DWORD) HICON;
Return Nindex;
}
Int CiconConComboBox :: InsertCon (INT NINDEX, LPCTSTSTSZCILE)
{
Hicon Hicon = :: Extracticon (AfxGetInstanceHandle (), LPSZICONFILE, 0); if (Hicon == (HICON) 1 || hicon == null)
RETURN CB_ERR;
Nindex = ccomboBox :: insertstring (nindex, lpsziconfile);
IF (Nindex! = CB_ERR && NINDEX! = CB_ERRSPACE)
SETITEMDATA (Nindex, (DWORD) HICON;
Return Nindex;
}
Int CiconComboBox :: Selecticon (LPCTSTR LPSZICONFILE)
{
INT NINDEX = CCOMBOBOX :: FindStringexact (-1, lpsziconfile;
IF (Nindex == CB_ERR && CCOMBOBOX :: getCount ()> 0)
NINDEX = 0; // select the first icon if not found
Return CCOMBOBOX :: SetCurseel (NINDEX);
}
Int CiconComboBox :: Selecticon (INT NINDEX)
{
Return CCOMBOBOX :: SetCurseel (NINDEX);
}
Int CiconcomboBox :: Deleteicon (LPCTSTR LPSZICONFILE)
{
INT NINDEX = CCOMBOBOX :: FindStringexact (-1, lpsziconfile;
Return CCOMBOBOX :: deleteString (Nindex);
}
Int CiconcomboBox :: Deleteicon (INT NINDEX)
{
Return CCOMBOBOX :: deleteString (Nindex);
}
INT CICONCOMBOBOX :: AddString (LPCTSTR LPSZSTRING)
{
Assert (false); // cannot add strings
RETURN CB_ERR;
}
Int CiconcomboBox :: InsertString (Int Nindex, LPCTSTR LPSZSTRING)
{
Assert (false); // cannot Insert Strings
RETURN CB_ERR;
}
INT CICONCOMBOBOX :: deleteString (int Nindex)
{
Assert (false); // cannot delete strings
RETURN CB_ERR;
}
Void CiconComboBox :: MeasureItem (LpMeasureItemstruct LPMIS)
{
LPMIS-> ITEMHEIGHT = (m_sizeicon.cy 2);
}
Void CiconComboBox :: DrawItem (LPDrawItemstruct LPDIS)
{
CDC * PDC = CDC :: fromHandle (LPDIS-> HDC);
IF (! iswindowenabled ())
{
CBRUSH BRDISABED (RGB (192, 192, 192)); // Light Gray
CBRUSH * POLDBRUSH = PDC-> SelectObject (& brdisable);
CPEN PENDISABED (PS_SOLID, 1, RGB (192, 192, 192);
CPEN * POLDPEN = PDC-> SelectObject (& PENDISABED); onoutputicon (LPDIS, FALSE);
PDC-> SELECTOBJECT (POLDBRUSH);
PDC-> SELECTOBJECT (POLDPEN);
Return;
}
// selected
IF ((LPDIS-> ItemState & ODS_SELECTED)
&& (LPDIS-> ItemAction & (ODA_SELECT | ODA_DRAWENTIRE)))))))
{
CBRUSH BRHighlight (:: getsyscolor (color_highlight));
CBRUSH * POLDBRUSH = PDC-> SelectObject (& BrHighlight);
CPEN Penhighlight (ps_solid, 1, :: getsyscolor (color_highlight));
CPEN * POLDPEN = PDC-> SELECTOBJECT (& Penhighlight);
PDC-> Rectangle (& lpdis-> rcitem);
PDC-> setBkcolor (: getsyscolor (color_highlight));
PDC-> SetTextColor (: getsyscolor (color_highlighttext);
ONOUTPUTICI (LPDIS, TRUE);
PDC-> SELECTOBJECT (POLDBRUSH);
PDC-> SELECTOBJECT (POLDPEN);
}
// de-selected
IF (! (lpdis-> itemState & ods_selected)
&& (LPDIS-> ItemAction & (ODA_SELECT | ODA_DRAWENTIRE)))))))
{
CBRUSH BRWINDOW (:: getsyscolor (color_window));
CBRUSH * POLDBRUSH = PDC-> SELECTOBJECT (& brwindow);
CPEN Penhighlight (ps_solid, 1, :: getsyscolor (color_window);
CPEN * POLDPEN = PDC-> SELECTOBJECT (& Penhighlight);
PDC-> Rectangle (& lpdis-> rcitem);
PDC-> setbkcolor (: getsyscolor (color_window));
PDC-> SetTextColor (: getsyscolor (color_windowtext));
ONOUTPUTICON (LPDIS, FALSE);
PDC-> SELECTOBJECT (POLDBRUSH);
PDC-> SELECTOBJECT (POLDPEN);
}
// focus
IF (LPDIS-> ItemAction & ODA_FOCUS)
{
PDC-> DrawFocusRect (& lpdis-> rcitem);
}
}
Void CiconComboBox :: onoutputicon (LPDrawItemstruct LPDIS, BOOL BSELECTED)
{
IF (getcursl () == cb_err || getCount () == 0)
Return; // no item success
CDC * PDC = CDC :: fromHandle (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);
PDC-> Drawicon (X, Y, Hicon);
}
Translated on September 9, 2001.