First, the implementation process
1. Table Header Modification
Create a new MFC class CHEADERCTRLCL, its base class is CHeaderCtrl, the response OnPaint message implementation of the painting, implementation code see source code (because the code is more space, so do not post, sorry), in the header file to define the function
Lresult OnLayout (WPARAM WPARAM, LPARAM LPARAM), then manually add message response On_message (hdm_layout, onlayout), change the height in the message response, and implement the following code:
LRESULT CHeaderCtrlCl::OnLayout( WPARAM wParam, LPARAM lParam )
{
LRESULT lResult = CHeaderCtrl::DefWindowProc(HDM_LAYOUT, 0, lParam);
HD_LAYOUT &hdl = *( HD_LAYOUT * ) lParam;
RECT *prc = hdl.prc;
WINDOWPOS *pwpos = hdl.pwpos;
int nHeight = (int)(pwpos->cy * m_Height); //改变高度,m_Height为倍数
pwpos->cy = nHeight;
prc->top = nHeight;
return lResult;
}
2. Modification of the table
Creates a new MFC class CLISTCTRLCL, its base class is CListCtrl, defines a CHEADERCTRLCL member variable M_header, Overloads Presubclasswindow (), modifies the control type in the function as a custom mode, and then subclasses the header , the code is as follows:
void CListCtrlCl::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
ModifyStyle(0,LVS_OWNERDRAWFIXED);
CListCtrl::PreSubclassWindow();
CHeaderCtrl *pHeader = GetHeaderCtrl();
m_Header.SubclassWindow(pHeader->GetSafeHwnd());
}
Add a member variable to save some basic information.
CPtrList m_ptrListCol; //保存列颜色
CPtrList m_ptrListItem; //保存Item颜色表
CPtrList m_colTextColor; //保存列字体颜色
CPtrList m_ItemTextColor; //保存单元格字体颜色
Overload DrawItem () to implement the custom code, please refer to the code. Manually add the message macro On_wm_measureitem_reflect (), add the following function to implement the Modify row height.
void CListCtrlCl::MeasureItem(LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
if (m_nRowHeight>0)
{
lpMeasureItemStruct->itemHeight = m_nRowHeight;
}
}Add message processing OnMeasureItem ()void CListCtrlCl::OnMeasureItem(int nIDCtl, LPMEASUREITEMSTRUCT lpMeasureItemStruct)
{
// TODO: 在此添加消息处理程序代码和/或调用默认值
CListCtrl::OnMeasureItem(nIDCtl, lpMeasureItemStruct);
}
Overwrite base class InsertColumn to save column information
int CListCtrlCl::InsertColumn(int nCol, LPCTSTR lpszColumnHeading,
int nFormat /* = LVCFMT_LEFT */,
int nWidth /* = -1 */,
int nSubItem /* = -1 */)
{
m_Header.m_HChar.Add(lpszColumnHeading);
if (nFormat==LVCFMT_LEFT)
{
m_Header.m_Format = m_Header.m_Format + "0";
}
else if (nFormat==LVCFMT_CENTER)
{
m_Header.m_Format = m_Header.m_Format + "1";
}
else if (nFormat==LVCFMT_RIGHT)
{
m_Header.m_Format = m_Header.m_Format + "2";
}
else
{
m_Header.m_Format = m_Header.m_Format + "1";
}
return CListCtrl::InsertColumn(nCol,lpszColumnHeading,nFormat,nWidth,nSubItem);
}