图标选择的组合框,我想大家在QQ的个人设置中肯定看见过,里面可以有很多头像,你可以任意的选择一个。下面就是建立一个选择图标的组合框的简单步骤:
1.这一步,很显然,得把.CPP和.H文件加到你的工程中去。
2.在你的工程中加一个组合框的控件,组合框的风格按默认的设置就行了。
3.利用VC的类向导给组合框定义一个控件变量。
4.在头文件中用CCionComBox代替CComboBox,那样你才能真正用下面的这个类。
上面的步骤好了之后,你就可以用下面的函数来插入,选择和删除图标了。
public:
virtual int addicon(lpctstr lpsziconfilename);
virtual int inserticon(int nindex, lpctstr lpsziconfilename);
virtual int selecticon(lpctstr lpsziconfilename);
virtual int selecticon(int nindex);
virtual int deleteicon(lpctstr lpsziconfilename);
virtual int deleteicon(int nindex);
下面的这个函数是拿来显示图标的。
protected:
virtual void onoutputicon(lpdrawitemstruct lpdis, bool bselected);
你还可以通过下面的函数来设置大小,以适应图标大小不同而不同。
m_cbmyiconcombobox.setitemheight(-1, m_cbmyiconcombobox.m_sizeicon.cy + 6);
当然还有其他的功能等你去发现,如果你真的很喜欢这项功能,你还是去把全部的代码看一下,这样会对你的编程更有好处,由于本人的水平有限,我还不能把它给扩展了,我希望有读者能把它给扩展了。下面是该类的.H和.CPP文件:
//头文件
// IconComboBox.h : header file
//
#ifndef _ICONCOMBOBOX_H_
#define _ICONCOMBOBOX_H_
//////////////////////////////////////////////////////
// //
// CIconComboBox //
// //
// Copyright (c) Joel Wahlberg ENATOR Networks 1997 //
//
[email protected] //
// //
//////////////////////////////////////////////////////
class CIconComboBox : public CComboBox
{
// Contruction/Destruction
public:
CIconComboBox();
virtual ~CIconComboBox();
// Attributes
public:
CSize m_sizeIcon;
// Operations
public:
virtual int AddIcon(LPCTSTR lpszIconFile);
virtual int InsertIcon(int nIndex, LPCTSTR lpszIconFile);
virtual int SelectIcon(LPCTSTR 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, LPCTSTR lpszString);
virtual int DeleteString(int nIndex);
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMIS);
virtual void DrawItem(LPDRAWITEMSTRUCT lpDIS);
};
#endif // _ICONCOMBOBOX_H_
//下面是源文件
#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 //
//
[email protected] //
// //
//////////////////////////////////////////////////////
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 CIconComboBox::InsertIcon(int nIndex, LPCTSTR lpszIconFile)
{
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::SetCurSel(nIndex);
}
int CIconComboBox::SelectIcon(int nIndex)
{
return CComboBox::SetCurSel(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 brDisabled(RGB(192,192,192)); // light gray
CBrush* pOldBrush = pDC->SelectObject(&brDisabled);
CPen penDisabled(PS_SOLID, 1, RGB(192,192,192));
CPen* pOldPen = pDC->SelectObject(&penDisabled);
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));
OnOutputIcon(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 (GetCurSel() == CB_ERR || GetCount() == 0)
return; // no item selected
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);
}
译于2001年9月9号。
本文地址:http://com.8s8s.com/it/it256.htm