当前位置:网站首页>How to redraw the header of CListCtrl in MFC
How to redraw the header of CListCtrl in MFC
2022-07-01 19:24:00 【mary288267】
MFC Medium CListCtrl It is actually composed of two controls , One is the header control , One is list control . Sometimes , We need to redraw the header , Make it meet the requirements of specific scenarios .
This paper introduces CListCtrl Redrawing method of header , The effect of custom header is as follows .
One 、 from CHeaderCtrl Derive a custom header class CCustomHeader
MFC The class representing the header control in is CHeaderCtrl, We derive a new class from it CCustomHeader .
The header file is :
class CCustomHeader : public CHeaderCtrl
{
DECLARE_DYNAMIC(CCustomHeader)
public:
CCustomHeader();
virtual ~CCustomHeader();
// Set the alignment of header cells , See DrawText Text alignment format in function
void SetTextAlign(UINT uFormat = DT_CENTER | DT_SINGLELINE | DT_VCENTER);
protected:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
DECLARE_MESSAGE_MAP()
private:
UINT m_nTextAlignFormat; // Heading text alignment
};
Implementation file
// CCustomHeader.cpp: Implementation file
//
#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));
// Draw the button border
::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH);
// Draw text
::DrawText(lpDrawItemStruct->hDC, lpBuffer, _tcslen(lpBuffer),
&lpDrawItemStruct->rcItem, m_nTextAlignFormat);
pDC->SelectObject(hOldFont);
}
Please note that , In the implementation file , We rewrote DrawItem Virtual functions ; Therefore, when the header control is self drawn , Called every time you draw DrawItem function , In this function, we get the... Of the header control DC, Then you can draw .
Please also note that in DrawItem The following functions used in :
BOOL GetItem( int nPos, HDITEM* pHeaderItem ) const;
explain : This function can get the information of each item of the header control (Retrieves information about a header control item.)
Using this function , We get the title content of each item in the header .
Two 、 Use custom header class CCustomHeader
Create a dialog based program , Add :
CListCtrl m_wndLstMain; // Represents the list control itself
CCustomHeader m_wndHeader; // The header control that represents the list control
stay OnInitDialog Add to function :
BOOL CTestDlg::OnInitDialog()
{
//.... Omit
// TODO: Add additional initialization code here
CRect rect;
m_wndLstMain.GetClientRect(rect);
m_wndLstMain.InsertColumn(0, _T(" full name "), LVCFMT_LEFT, rect.Width() / 3);
m_wndLstMain.InsertColumn(1, _T(" class "), LVCFMT_LEFT, rect.Width() / 3);
m_wndLstMain.InsertColumn(2, _T(" Student number "), LVCFMT_LEFT, rect.Width() / 3);
m_wndLstMain.InsertItem(0, _T(" Zhang San "));
m_wndLstMain.SetItemText(0, 1, _T(" Class one "));
m_wndLstMain.SetItemText(0, 2, _T("001"));
m_wndLstMain.InsertItem(1, _T(" Li Si "));
m_wndLstMain.SetItemText(1, 1, _T(" Class one "));
m_wndLstMain.SetItemText(1, 2, _T("002"));
m_wndLstMain.InsertItem(2, _T(" Wang Wu "));
m_wndLstMain.SetItemText(2, 1, _T(" Class one "));
m_wndLstMain.SetItemText(2, 2, _T("003"));
m_wndLstMain.SetExtendedStyle(LVS_EX_GRIDLINES);
// Subclass the header of the list control
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; // Add self drawing style
m_wndHeader.SetItem(i, &hdItem);
}
//.... Omit
}
Note in the above function , We first initialize CListCtrl object , Created multiple columns and children , And then use it GetHeaderCtrl() The function found CListCtrl Object's header control , Last call SubclassWindow Subclass the header control and bind it to m_wndHeader( This is our overridden header control class object ).
however , That's not the end , We need to change every item in the header to self drawn style , such , Only when the header is redrawn DrawItem function , The specific code is :
HDITEM hdItem;
hdItem.mask = HDI_FORMAT;
for (int i = 0; i < m_wndHeader.GetItemCount(); i++)
{
m_wndHeader.GetItem(i, &hdItem);
hdItem.fmt |= HDF_OWNERDRAW; // Add self drawing style
m_wndHeader.SetItem(i, &hdItem);
}
边栏推荐
- 【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
- [6.24-7.1] review of wonderful technical blog posts in the writing community
- Openai video pre training (VPT): action learning based on watching unmarked online videos
- XML语法、约束
- Transform + ASM data
- Dom4J解析XML、Xpath检索XML
- Lumiprobe phosphide hexaethylene phosphide specification
- Go语言高级
- 实现一个Prometheus exporter
- 市值蒸发740亿,这位大佬转身杀入预制菜
猜你喜欢

Stanford, salesforce|maskvit: masked vision pre training for video prediction

华为游戏初始化init失败,返回错误码907135000

Specification of lumiprobe reactive dye indocyanine green
![[pytorch record] automatic hybrid accuracy training torch cuda. amp](/img/a5/cf1eb2801380cf2887dfd532d3eb1e.jpg)
[pytorch record] automatic hybrid accuracy training torch cuda. amp

SuperOptiMag 超导磁体系统 — SOM、SOM2 系列

3. "Create your own NFT collections and publish a Web3 application to show them" cast NFT locally

机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效

C端梦难做,科大讯飞靠什么撑起10亿用户目标?

Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency

Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
随机推荐
CDGA|从事通信行业,那你应该考个数据管理证书
【森城市】GIS数据漫谈(一)
Three ways for redis to realize current limiting
English语法_形容词/副词3级 -注意事项
Technical secrets of ByteDance data platform: implementation and optimization of complex query based on Clickhouse
Netease games, radical going to sea
Transform + ASM data
2020, the regular expression for mobile phone verification of the latest mobile phone number is continuously updated
Qfile read / write file operation in QT
Lumiprobe free radical analysis h2dcfda instructions
Improve yolov5 with gsconv+slim neck to maximize performance!
见证时代!“人玑协同 未来已来”2022弘玑生态伙伴大会开启直播预约
Lefse analysis
微服务大行其道的今天,Service Mesh是怎样一种存在?
nacos配置文件发布失败,请检查参数是否正确的解决方案
从零开始学 MySQL —数据库和数据表操作
Lumiprobe 自由基分析丨H2DCFDA说明书
论文阅读【Learning to Discretely Compose Reasoning Module Networks for Video Captioning】
Yyds dry inventory ravendb start client API (III)
ES6 summary "suggestions collection" of array methods find(), findindex()