当前位置:网站首页>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语言高级
- [live broadcast appointment] database obcp certification comprehensive upgrade open class
- MATLAB中subplot函数的使用
- Lake Shore - crx-em-hf low temperature probe station
- Three simple methods of ES6 array de duplication
- 【Go ~ 0到1 】 第四天 6月30 defer,结构体,方法
- Dlib+opencv library for fatigue detection
- [quick application] there are many words in the text component. How to solve the problem that the div style next to it will be stretched
- SuperVariMag 超导磁体系统 — SVM 系列
- Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
猜你喜欢
![[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?](/img/66/674a06d8e45a31ae879b81554ef373.png)
[AGC] how to solve the problem that the local display of event analysis data is inconsistent with that in AGC panel?

Lumiprobe 自由基分析丨H2DCFDA说明书

宝,运维100+服务器很头疼怎么办?用行云管家!

ECS summer money saving secret, this time @ old users come and take it away

Graduation season | Huawei experts teach the interview secret: how to get a high paying offer from a large factory?

【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数

【pytorch记录】自动混合精度训练 torch.cuda.amp

Improve yolov5 with gsconv+slim neck to maximize performance!

The intelligent epidemic prevention system provides safety guarantee for the resumption of work and production at the construction site

【To .NET】C#集合类源码解析
随机推荐
Lumiprobe 自由基分析丨H2DCFDA说明书
M91快速霍尔测量仪—在更短的时间内进行更好的测量
C-end dream is difficult to achieve. What does iFLYTEK rely on to support the goal of 1billion users?
Lake Shore低温恒温器的氦气传输线
Intensive cultivation of channels for joint development Fuxin and Weishi Jiajie held a new product training conference
使用环信提供的uni-app Demo,快速实现一对一单聊
Getting started with kubernetes command (namespaces, pods)
Go language self-study series | go language data type
ES6 summary "suggestions collection" of array methods find(), findindex()
nacos配置文件发布失败,请检查参数是否正确的解决方案
Cache problems after app release
VBA simple macro programming of Excel
The difference between indexof and includes
华为联机对战服务玩家掉线重连案例总结
【Go ~ 0到1 】 第五天 7月1 类型别名,自定义类型,接口,包与初始化函数
精益思想:来源,支柱,落地。看了这篇文章就懂了
PostgreSQL varchar[] 数组类型操作
市值蒸发740亿,这位大佬转身杀入预制菜
Lake Shore continuous flow cryostat transmission line
Yyds dry inventory ravendb start client API (III)