当前位置:网站首页>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);
}
边栏推荐
- Supervarimag superconducting magnet system SVM series
- Superoptimag superconducting magnet system - SOM, Som2 series
- English语法_形容词/副词3级 -注意事项
- MySQL common graphics management tools | dark horse programmers
- Lake Shore—CRX-EM-HF 型低温探针台
- Netease games, radical going to sea
- Shell array
- JS find the next adjacent element of the number in the array
- Getting started with kubernetes command (namespaces, pods)
- Mipi interface, DVP interface and CSI interface of camera [easy to understand]
猜你喜欢
Viewing the whole ecology of Tiktok from a macro perspective
Once the SQL is optimized, the database query speed is increased by 60 times
一次SQL优化,数据库查询速度提升 60 倍
ACM mm 2022 video understanding challenge video classification track champion autox team technology sharing
【快应用】text组件里的文字很多,旁边的div样式会被拉伸如何解决
Lake Shore 连续流动低温恒温器传输线
Chaos engineering platform chaosblade box new heavy release
Summary of the core steps in the life cycle of beans
Lake Shore—OptiMag 超导磁体系统 — OM 系列
数商云:从规划到落地,五矿集团如何快速构建数字化发展新格局?
随机推荐
Summary of the core steps in the life cycle of beans
Lake Shore—OptiMag 超导磁体系统 — OM 系列
一次SQL优化,数据库查询速度提升 60 倍
Huawei cloud experts explain the new features of gaussdb (for MySQL)
Go language self-study series | go language data type
Prices of Apple products rose across the board in Japan, with iphone13 up 19%
Lumiprobe 细胞成像研究丨PKH26细胞膜标记试剂盒
B2B e-commerce platform solution for fresh food industry to improve the standardization and transparency of enterprise transaction process
从零开始学 MySQL —数据库和数据表操作
华为游戏初始化init失败,返回错误码907135000
Bao, what if the O & M 100+ server is a headache? Use Xingyun housekeeper!
Contos 7 搭建sftp之创建用户、用户组以及删除用户
ES6 summary "suggestions collection" of array methods find(), findindex()
【直播预约】数据库OBCP认证全面升级公开课
XML语法、约束
Contos 7 set up SFTP to create users, user groups, and delete users
机械设备行业数字化供应链集采平台解决方案:优化资源配置,实现降本增效
Redis 实现限流的三种方式
Cache problems after app release
Today, with the popularity of micro services, how does service mesh exist?