MFC—ClistBox控件重绘,实现扁平单元风格

MFC ClistBox控件,本身不支持设置单元格颜色,以及每个box之间的间距设置。但是我们可以通过对ClistBox进行重载,重写其DrawItem函数进行重绘。

首先添加一个ListBox控件,将其Owner Draw 设置为 variable,然后添加类继承ClistBox,对其DrawItem进行重写。

头文件:


class CMyListBox :public CListBox
{
	DECLARE_DYNAMIC(CMyListBox)
public:
	CMyListBox();
	~CMyListBox();

	//控件 常规  按下 边缘 颜色 以及上下边缘距离
	void SetSkin(COLORREF normal,COLORREF down,COLORREF edge = 0, int edgespace = 0);
	void SetItemHeight(int cy);//设置单个条目高度
private:
	COLORREF m_NormalColor;
	COLORREF m_DownColor;
	COLORREF m_EdgeColor;
	int m_EdgeSpace;
	int m_ItemHeigh;

	virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);;
	virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
	DECLARE_MESSAGE_MAP()

public:
	afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp);

};

.cpp文件

#include "stdafx.h"
#include "MyListBox.h"

IMPLEMENT_DYNAMIC(CMyListBox, CListBox)
CMyListBox::CMyListBox()
{
	m_NormalColor = RGB(255, 255, 255); //白色
	m_DownColor =  RGB(39, 149, 241); //设置为默认天蓝色
	m_EdgeColor = GetSysColor(COLOR_BTNFACE); //边框颜色为按钮灰
	m_EdgeSpace = 5; //上下边距默认5
}


CMyListBox::~CMyListBox()
{
}


BEGIN_MESSAGE_MAP(CMyListBox, CListBox)
	ON_WM_NCCALCSIZE()
END_MESSAGE_MAP()

//设置按钮的按下弹起等状态颜色
void CMyListBox::SetSkin(COLORREF normal, COLORREF down, COLORREF edge, int edgespace)
{
	m_NormalColor = normal;
	m_DownColor = down;
	m_EdgeColor = edge;
	m_EdgeSpace = edgespace;
	return;
}


//设置单个条目高度
void CMyListBox::SetItemHeight(int cy)
{
	if (cy > 0)
	m_ItemHeigh = cy;

	return;
}


void CMyListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
	// TODO: Add your code to draw the specified item  
	ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
	LPCTSTR lpszText = (LPCTSTR)lpDrawItemStruct->itemData;
	ASSERT(lpszText != NULL);
	CDC dc;


	dc.Attach(lpDrawItemStruct->hDC);

	// Save these value to restore them when done drawing.  
	COLORREF crOldTextColor = dc.GetTextColor();
	COLORREF crOldBkColor = dc.GetBkColor();

	// If this item is selected, set the background color   
	// and the text color to appropriate values. Also, erase  
	// rect by filling it with the background color.  

	//获取当前item 窗体位置
	CRgn rgn1;
	RECT r = lpDrawItemStruct->rcItem;					
	RECT tr = { r.left, r.top + m_EdgeSpace, r.right, r.bottom - m_EdgeSpace };
	rgn1.CreateRectRgnIndirect(&tr);

	//窗口被选中 或者为活动
	if ((lpDrawItemStruct->itemAction | ODA_SELECT) &&(lpDrawItemStruct->itemState & ODS_SELECTED))
	{
		dc.SetTextColor(::GetSysColor(COLOR_HIGHLIGHTTEXT));//字体颜色白色
		dc.SetBkColor(m_DownColor);//::GetSysColor(COLOR_HIGHLIGHT)
		
		CBrush brush1(m_DownColor);
		dc.FillRgn(&rgn1, &brush1);
		brush1.DeleteObject();
		
	//	SetCurSel(lpDrawItemStruct->itemID + 2);
		
		//dc.FillSolidRect(&lpDrawItemStruct->rcItem,::GetSysColor(COLOR_HIGHLIGHT));
	}
	else//窗口未被选中
	{
		//内嵌窗体颜色绘制
		CBrush brush1(m_NormalColor);
		dc.FillRgn(&rgn1, &brush1);
		
		//内嵌窗体边框绘制
		CBrush brush2(m_EdgeColor);
		dc.FrameRgn(&rgn1, &brush2, 2, 2);
		
		brush1.DeleteObject();
		brush2.DeleteObject();
		/*if (lpDrawItemStruct->itemID % 2)
			dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(128, 128, 128));
		else
			dc.FillSolidRect(&lpDrawItemStruct->rcItem, RGB(255, 128, 255));*/
	}

	// 选中某一条目时使其边框高亮 
	if ((lpDrawItemStruct->itemAction | ODA_FOCUS) &&(lpDrawItemStruct->itemState & ODS_FOCUS))
	{
		
		//CBrush br(RGB(0, 0, 128));
		//dc.FrameRect(&lpDrawItemStruct->rcItem, &br);
	}

	lpDrawItemStruct->rcItem.left += 5;
	
	// Draw the text.  
	//dc.DrawText(lpszText, strlen(buf), &lpDrawItemStruct->rcItem, DT_SINGLELINE | DT_VCENTER | DT_CENTER);
	dc.DrawText(lpszText, &tr, DT_SINGLELINE | DT_VCENTER | DT_CENTER);

	// Reset the background color and the text color back to their  
	// original values.  
	dc.SetTextColor(crOldTextColor);
	dc.SetBkColor(crOldBkColor);
	dc.Detach();
	rgn1.DeleteObject();
	return;
}

void CMyListBox::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
	// TODO: Add your code to determine the size of specified item  
	ASSERT(lpMeasureItemStruct->CtlType == ODT_LISTBOX);
	LPCTSTR lpszText = (LPCTSTR)lpMeasureItemStruct->itemData;
	ASSERT(lpszText != NULL);
	CSize sz;
	CDC* pDC = GetDC();

	sz = pDC->GetTextExtent(lpszText);

	ReleaseDC(pDC);

	int height = 2 * sz.cy + 10;
	lpMeasureItemStruct->itemHeight = (m_ItemHeigh > 0) ? m_ItemHeigh : height;
	
	return;

}

//实现滚轮滚动 但不显示滚动条
void CMyListBox::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
	ShowScrollBar(SB_BOTH, FALSE);
	CListBox::OnNcCalcSize(bCalcValidRects, lpncsp);
}

对控件添加变量,将对象改为 CMylistBox,即可通过其函数设置,每个box的高度,边缘间隔,以及按下弹起时的颜色。

当前效果如下:

 

评论 8
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值