当前位置:网站首页>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);
}
边栏推荐
- English语法_形容词/副词3级 -注意事项
- kubernetes命令入门(namespaces,pods)
- 制造业SRM管理系统供应商全方位闭环管理,实现采购寻源与流程高效协同
- Three ways for redis to realize current limiting
- Lumiprobe 活性染料丨吲哚菁绿说明书
- Nacos configuration file publishing failed, please check whether the parameters are correct solution
- Lumiprobe 自由基分析丨H2DCFDA说明书
- The difference between indexof and includes
- Lake Shore—CRX-EM-HF 型低温探针台
- Prices of Apple products rose across the board in Japan, with iphone13 up 19%
猜你喜欢
使用环信提供的uni-app Demo,快速实现一对一单聊
【直播预约】数据库OBCP认证全面升级公开课
网易游戏,激进出海
MySQL common graphics management tools | dark horse programmers
kubernetes命令入门(namespaces,pods)
Huawei cloud experts explain the new features of gaussdb (for MySQL)
More information about M91 fast hall measuring instrument
The best landing practice of cave state in an Internet ⽹⾦ financial technology enterprise
Solution of intelligent supply chain management platform in aquatic industry: support the digitalization of enterprise supply chain and improve enterprise management efficiency
混沌工程平台 ChaosBlade-Box 新版重磅发布
随机推荐
AppGallery Connect场景化开发实战—图片存储分享
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
Implement a Prometheus exporter
实现一个Prometheus exporter
Contos 7 搭建sftp之创建用户、用户组以及删除用户
The difference between indexof and includes
Team up to learn! 14 days of Hongmeng equipment development "learning, practicing and testing" practical camp, free of charge!
Graduation summary
XML语法、约束
Three simple methods of ES6 array de duplication
记一次 .NET 差旅管理后台 CPU 爆高分析
M91 fast hall measuring instrument - better measurement in a shorter time
11. Users, groups, and permissions (1)
CDGA|从事通信行业,那你应该考个数据管理证书
微服务大行其道的今天,Service Mesh是怎样一种存在?
Manufacturing SRM management system supplier all-round closed-loop management, to achieve procurement sourcing and process efficient collaboration
【6.24-7.1】写作社区精彩技术博文回顾
Huawei cloud experts explain the new features of gaussdb (for MySQL)
Lumiprobe 活性染料丨吲哚菁绿说明书
Three ways for redis to realize current limiting