#pragma once
// CMyListBox
class CMyListBox : public CListBox
{
DECLARE_DYNAMIC(CMyListBox)
public:
CMyListBox();
virtual ~CMyListBox();
protected:
DECLARE_MESSAGE_MAP()
virtual BOOL PreCreateWindow(CREATESTRUCT& cs);
public:
virtual void DrawItem(LPDRAWITEMSTRUCT /*lpDrawItemStruct*/);
virtual void MeasureItem(LPMEASUREITEMSTRUCT /*lpMeasureItemStruct*/);
protected:
virtual void PreSubclassWindow();
public:
void Add(CString strItemName,HICON hData);
void Destroy();
protected:
typedef struct DataItems
{
CString strItemData;
HICON hIcon;
}DATAITEMS,*LPDATAITEMS;
LPDATAITEMS m_pListBoxDataItems;
CRect m_rectListBoxItem;
public:
afx_msg void OnPaint();
afx_msg BOOL OnEraseBkgnd(CDC* pDC);
};
#include "stdafx.h"
#include "Relf.h"
#include "MyListBox.h"
// CMyListBox
IMPLEMENT_DYNAMIC(CMyListBox, CListBox)
CMyListBox::CMyListBox()
{
}
CMyListBox::~CMyListBox()
{
}
BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
ON_WM_PAINT()
ON_WM_ERASEBKGND()
END_MESSAGE_MAP()
// CMyListBox 消息处理程序
BOOL CMyListBox::PreCreateWindow(CREATESTRUCT& cs)
{
cs.dwExStyle |= WS_EX_CLIENTEDGE;
cs.style &= ~(LBS_OWNERDRAWVARIABLE | LBS_SORT);
cs.style |= LBS_OWNERDRAWFIXED | LBS_NOTIFY;
return CListBox::PreCreateWindow(cs);
}
void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
CDC dc;
dc.Attach(lpDrawItemStruct ->hDC);
CRect rect = lpDrawItemStruct ->rcItem;
int nIndex = lpDrawItemStruct ->itemID;
CBrush * pBrush = new CBrush;
// pBrush ->CreateSolidBrush(::GetSysColor((lpDrawItemStruct ->itemState & ODS_SELECTED) ?
// COLOR_HIGHLIGHT : COLOR_WINDOW));
if(lpDrawItemStruct ->itemState & ODS_SELECTED)
{
pBrush ->CreateSolidBrush(RGB(72,121,247));
}
else
pBrush ->CreateSolidBrush(RGB(168,191,241));
dc.FillRect(rect,pBrush);
delete pBrush;
if(lpDrawItemStruct ->itemState & ODS_FOCUS)
dc.DrawFocusRect(rect);
LPDATAITEMS pListDataItem = (LPDATAITEMS)GetItemDataPtr(lpDrawItemStruct ->itemID);
dc.DrawIcon(rect.left + (rect.Height() - 32) / 2,rect.top + (rect.Height() - 32) / 2,pListDataItem ->hIcon);
rect.OffsetRect(10,0);
dc.SetBkMode(TRANSPARENT);
dc.SetTextColor(RGB(0,85,234));
dc.DrawText(pListDataItem ->strItemData,&rect,DT_CENTER | DT_SINGLELINE | DT_VCENTER);
dc.Detach();
}
void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
lpMeasureItemStruct ->itemHeight = 36;
lpMeasureItemStruct ->itemWidth = 100;
}
void CMyListBox::PreSubclassWindow()
{
// BOOL bTrue = ModifyStyle(0,LBS_OWNERDRAWFIXED);
CListBox::PreSubclassWindow();
}
void CMyListBox::Add(CString strItemName,HICON hData)
{
m_pListBoxDataItems = new DATAITEMS;
m_pListBoxDataItems ->strItemData = strItemName;
m_pListBoxDataItems ->hIcon = hData;
UINT nIndex = AddString(_T(""));
SetItemDataPtr(nIndex,(void*)m_pListBoxDataItems);
}
void CMyListBox::OnPaint()
{
CPaintDC dc(this); // device context for painting
}
BOOL CMyListBox::OnEraseBkgnd(CDC* pDC)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CDC dc;
dc.Attach(pDC ->m_hDC);
CRect rect;
GetClientRect(rect);
CBrush brush;
brush.CreateSolidBrush(RGB(168,191,241));
CBrush * pOld = dc.SelectObject(&brush);
dc.FillRect(rect,&brush);
dc.SelectObject(pOld);
brush.DeleteObject();
dc.Detach();
return TRUE;
return CListBox::OnEraseBkgnd(pDC);
}
最后效果图片: