当前位置:网站首页>MFC中如何重绘CListCtrl的表头
MFC中如何重绘CListCtrl的表头
2022-07-01 18:43:00 【mary288267】
MFC中的CListCtrl实际上是由两个控件组成,一个是表头控件,一个是列表控件。有些时候,我们需要重绘表头,使其满足特定的场景要求。
本文介绍了CListCtrl表头的重绘方法,自定义表头效果如下。
一、从CHeaderCtrl派生出自定义表头类CCustomHeader
MFC中表示表头控件的类是CHeaderCtrl,我们从它派生一个新类CCustomHeader 。
头文件为:
class CCustomHeader : public CHeaderCtrl
{
DECLARE_DYNAMIC(CCustomHeader)
public:
CCustomHeader();
virtual ~CCustomHeader();
//设置表头单元格的对齐方式,参见DrawText函数中文字对齐格式
void SetTextAlign(UINT uFormat = DT_CENTER | DT_SINGLELINE | DT_VCENTER);
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
DECLARE_MESSAGE_MAP()
private:
UINT m_nTextAlignFormat; //表头文字对齐方式
};
实现文件
// CCustomHeader.cpp: 实现文件
//
#include "pch.h"
#include "TestCustomHeader.h"
#include "CCustomHeader.h"
// CCustomHeader
IMPLEMENT_DYNAMIC(CCustomHeader, CHeaderCtrl)
CCustomHeader::CCustomHeader()
:m_nTextAlignFormat(DT_CENTER | DT_SINGLELINE | DT_VCENTER)
{
}
CCustomHeader::~CCustomHeader()
{
}
void CCustomHeader::SetTextAlign(UINT uFormat)
{
m_nTextAlignFormat = uFormat;
}
BEGIN_MESSAGE_MAP(CCustomHeader, CHeaderCtrl)
END_MESSAGE_MAP()
void CCustomHeader::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
ASSERT(lpDrawItemStruct->CtlType == ODT_HEADER);
HDITEM hdi;
TCHAR lpBuffer[256];
hdi.mask = HDI_TEXT;
hdi.pszText = lpBuffer;
hdi.cchTextMax = 256;
GetItem(lpDrawItemStruct->itemID, &hdi);
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
HGDIOBJ hOldFont = pDC->SelectObject(GetStockObject(DEFAULT_GUI_FONT));
// 绘制按钮边框
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH);
//绘制文本
::DrawText(lpDrawItemStruct->hDC, lpBuffer, _tcslen(lpBuffer),
&lpDrawItemStruct->rcItem, m_nTextAlignFormat);
pDC->SelectObject(hOldFont);
}
请注意,在实现文件中,我们重写了DrawItem虚函数;因此当表头控件是自绘样式时,每次绘制都会调用DrawItem函数,在该函数中我们获取表头控件的DC,然后即可进行绘制。
另请注意在DrawItem中使用的如下函数:
BOOL GetItem( int nPos, HDITEM* pHeaderItem ) const;
说明:该函数可以获取表头控件每一项的信息(Retrieves information about a header control item.)
利用该函数,我们获取了表头中每一项的标题内容。
二、使用自定义表头类CCustomHeader
新建一个基于对话框的程序,在对话框类的头文件中增加:
CListCtrl m_wndLstMain; //表示列表控件本身
CCustomHeader m_wndHeader; //表示列表控件的表头控件
在OnInitDialog函数中增加:
BOOL CTestDlg::OnInitDialog()
{
//.... 省略
// TODO: 在此添加额外的初始化代码
CRect rect;
m_wndLstMain.GetClientRect(rect);
m_wndLstMain.InsertColumn(0, _T("姓名"), LVCFMT_LEFT, rect.Width() / 3);
m_wndLstMain.InsertColumn(1, _T("班级"), LVCFMT_LEFT, rect.Width() / 3);
m_wndLstMain.InsertColumn(2, _T("学号"), LVCFMT_LEFT, rect.Width() / 3);
m_wndLstMain.InsertItem(0, _T("张三"));
m_wndLstMain.SetItemText(0, 1, _T("一班"));
m_wndLstMain.SetItemText(0, 2, _T("001"));
m_wndLstMain.InsertItem(1, _T("李四"));
m_wndLstMain.SetItemText(1, 1, _T("一班"));
m_wndLstMain.SetItemText(1, 2, _T("002"));
m_wndLstMain.InsertItem(2, _T("王五"));
m_wndLstMain.SetItemText(2, 1, _T("一班"));
m_wndLstMain.SetItemText(2, 2, _T("003"));
m_wndLstMain.SetExtendedStyle(LVS_EX_GRIDLINES);
//子类化列表控件的表头
CHeaderCtrl* pHeader = m_wndLstMain.GetHeaderCtrl();
if(pHeader)
m_wndHeader.SubclassWindow(pHeader->GetSafeHwnd());
HDITEM hdItem;
hdItem.mask = HDI_FORMAT;
for (int i = 0; i < m_wndHeader.GetItemCount(); i++)
{
m_wndHeader.GetItem(i, &hdItem);
hdItem.fmt |= HDF_OWNERDRAW; //增加自绘样式
m_wndHeader.SetItem(i, &hdItem);
}
//.... 省略
}
注意在上述函数中,我们首先初始化了CListCtrl对象,创建了多个列及多个子项,然后利用GetHeaderCtrl()函数找到了CListCtrl对象的表头控件,最后调用SubclassWindow子类化表头控件并将其绑定到m_wndHeader(这是我们重写的表头控件类对象)。
但是,仅仅如此还并未结束,我们需要将表头的每一项都改为自绘样式,这样,表头重绘时才会调用DrawItem函数,具体代码为:
HDITEM hdItem;
hdItem.mask = HDI_FORMAT;
for (int i = 0; i < m_wndHeader.GetItemCount(); i++)
{
m_wndHeader.GetItem(i, &hdItem);
hdItem.fmt |= HDF_OWNERDRAW; //增加自绘样式
m_wndHeader.SetItem(i, &hdItem);
}
边栏推荐
- Lumiprobe 自由基分析丨H2DCFDA说明书
- Three ways for redis to realize current limiting
- Go language self-study series | go language data type
- M91 fast hall measuring instrument - better measurement in a shorter time
- 精耕渠道共谋发展 福昕携手伟仕佳杰开展新产品培训大会
- 【6.24-7.1】写作社区精彩技术博文回顾
- Digital business cloud: from planning to implementation, how does Minmetals Group quickly build a new pattern of digital development?
- Lake Shore低温恒温器的氦气传输线
- 【直播预约】数据库OBCP认证全面升级公开课
- The former 4A executives engaged in agent operation and won an IPO
猜你喜欢
C端梦难做,科大讯飞靠什么撑起10亿用户目标?
kubernetes命令入门(namespaces,pods)
华为游戏初始化init失败,返回错误码907135000
[live broadcast appointment] database obcp certification comprehensive upgrade open class
How to use the low code platform of the Internet of things for personal settings?
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
Altair HyperWorks 2022 software installation package and installation tutorial
Lumiprobe cell imaging study PKH26 cell membrane labeling kit
机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
Clean up system cache and free memory under Linux
随机推荐
苹果产品在日本全面涨价,iPhone13涨19%
Love business in Little Red Book
Database foundation: select basic query statement
Excel之VBA简单宏编程
Netease games, radical going to sea
11. Users, groups, and permissions (1)
太爱速M源码搭建,巅峰小店APP溢价寄卖源码分享
组队学习! 14天鸿蒙设备开发“学练考”实战营限时免费加入!
Superoptimag superconducting magnet system - SOM, Som2 series
中英说明书丨人可溶性晚期糖基化终末产物受体(sRAGE)Elisa试剂盒
Three ways for redis to realize current limiting
Docker deploy mysql8.0
Lake shore M91 fast hall measuring instrument
一次SQL优化,数据库查询速度提升 60 倍
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
学习笔记-JDBC连接数据库操作的步骤
数据仓库(四)之ETL开发
M91快速霍尔测量仪—在更短的时间内进行更好的测量
[quick application] win7 system cannot run and debug projects using Huawei ide
ECS summer money saving secret, this time @ old users come and take it away