本篇文章参考自https://blog.csdn.net/qq_36568418/article/details/102921788?ops_request_misc=%25257B%252522request%25255Fid%252522%25253A%252522161251566116780299078381%252522%25252C%252522scm%252522%25253A%25252220140713.130102334.pc%25255Fall.%252522%25257D&request_id=161251566116780299078381&biz_id=0&utm_medium=distribute.pc_search_result.none-task-blog-2allfirst_rank_v2~rank_v29-18-102921788.pc_search_result_cache&utm_term=MFC+Listbox%25E8%2587%25AA%25E7%25BB%2598
对其进行改进版
MFC ClistBox控件,本身不支持设置单元格颜色,以及每个box之间的间距设置。但是我们可以通过对ClistBox进行重载,重写其DrawItem函数进行重绘。
首先添加一个ListBox控件,将其Owner Draw 置为 variable, Has Strings 置为 True。
.h
#pragma once
#include "afxwin.h"
class ReDrawListBox : public CListBox
{
DECLARE_DYNAMIC(ReDrawListBox)
public:
ReDrawListBox();
virtual ~ReDrawListBox();
//控件 常规 按下 边缘 颜色 以及上下边缘距离
void SetSkin(COLORREF normal, COLORREF down, COLORREF edge = 0, int edgespace = 0);
void SetItemHeight(int cy);//设置单个条目高度
private:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);;
virtual void MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct);
afx_msg void OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp);
DECLARE_MESSAGE_MAP()
COLORREF m_NormalColor;
COLORREF m_DownColor;
COLORREF m_EdgeColor;
int mnEdgeSpace;
int mnItemHeigh;
public:
};
.cpp
#include "stdafx.h"
#include "ReDrawListBox.h"
IMPLEMENT_DYNAMIC(ReDrawListBox, CListBox)
ReDrawListBox::ReDrawListBox()
{
m_NormalColor = RGB(255, 255, 255); //白色
m_DownColor = RGB(39, 149, 241); //设置为默认天蓝色
m_EdgeColor = GetSysColor(COLOR_BTNFACE); //边框颜色为按钮灰
mnEdgeSpace = 0; //上下边距默认5
}
ReDrawListBox::~ReDrawListBox()
{
}
BEGIN_MESSAGE_MAP(ReDrawListBox, CListBox)
ON_WM_NCCALCSIZE()
END_MESSAGE_MAP()
//设置按钮的按下弹起等状态颜色
void ReDrawListBox::SetSkin(COLORREF normal, COLORREF down, COLORREF edge, int edgespace)
{
m_NormalColor = normal;
m_DownColor = down;
m_EdgeColor = edge;
mnEdgeSpace = edgespace;
return;
}
//设置单个条目高度
void ReDrawListBox::SetItemHeight(int cy)
{
if (cy > 0)
mnItemHeigh = cy;
return;
}
void ReDrawListBox::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
// TODO: Add your code to draw the specified item
ASSERT(lpDrawItemStruct->CtlType == ODT_LISTBOX);
CString cstr;
GetText(lpDrawItemStruct->itemID, cstr);
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 + mnEdgeSpace, r.right, r.bottom - mnEdgeSpace };
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(cstr, &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 ReDrawListBox::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 = (mnItemHeigh > 0) ? mnItemHeigh : height;
return;
}
//实现滚轮滚动 但不显示滚动条
void ReDrawListBox::OnNcCalcSize(BOOL bCalcValidRects, NCCALCSIZE_PARAMS* lpncsp)
{
ShowScrollBar(SB_BOTH, FALSE);
CListBox::OnNcCalcSize(bCalcValidRects, lpncsp);
}
对控件添加变量,将对象改为 ReDrawlistBox,即可通过其函数设置,每个box的高度,边缘间隔,以及按下弹起时的颜色。
当前效果如下: