自绘ListBox

#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);
}

最后效果图片:


  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值