还在苦苦敲代码开发APP?你out啦! 试试积木搭建APP吧~

[完整源码实例] 修改 CListCtrl 的标题栏字体颜色;重绘 CListCtrl 标题栏

来源:清泛原创     2016-07-28 17:46:04    人气:     我有话说( 0 人参与)

如果只需设置标题栏字体的话,无需自绘CHeaderCtrl,部分代码如下:CFont *f = new CFont; f->CreateFont(13, nHeight ...

如果只需设置标题栏字体的话,无需自绘CHeaderCtrl,部分代码如下:
CFont *f = new CFont;
    f->CreateFont(13, // nHeight 
                                    0, // nWidth 
                                    0, // nEscapement 
                                    0, // nOrientation 
                                    700, // nWeight 
                                    FALSE, // bItalic 
                                    FALSE, // bUnderline 
                                    0, // cStrikeOut 
                                    ANSI_CHARSET, // nCharSet 
                                    OUT_DEFAULT_PRECIS, // nOutPrecision 
                                    CLIP_DEFAULT_PRECIS, // nClipPrecision 
                                    DEFAULT_QUALITY, // nQuality 
                                    DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily 
                                    _T("宋体")); // lpszFac

	//加载自定义Header
	CHeaderCtrl *pHeader = m_ListCtrl.GetHeaderCtrl();
	if(pHeader == NULL)
		return FALSE;
	m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd);
	m_HeaderCtrl.SetFont(f);
效果:


这个比较简单,但是如果需要设置颜色等其他属性,则需要自绘重画CHeaderCtrl标题栏,重载其DrawItem虚函数,然后就是想怎么画就怎么画了。。。

步骤如下:
MyHeaderCtrl.h
#pragma once
class CMyHeaderCtrl : public CHeaderCtrl
{
public:
	CMyHeaderCtrl(void);
	~CMyHeaderCtrl(void);

	DECLARE_MESSAGE_MAP()

	virtual void DrawItem(_In_ LPDRAWITEMSTRUCT lpDrawItemStruct);
};
MyHeaderCtrl.cpp
#include "stdafx.h"
#include "MyHeaderCtrl.h"

BEGIN_MESSAGE_MAP(CMyHeaderCtrl, CHeaderCtrl)
//{{AFX_MSG_MAP(CHeaderCtrlEx)
// NOTE - the ClassWizard will add and remove mapping macros here.
//}}AFX_MSG_MAP	
END_MESSAGE_MAP()

CMyHeaderCtrl::CMyHeaderCtrl(void)
{
}


CMyHeaderCtrl::~CMyHeaderCtrl(void)
{
}

void CMyHeaderCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
   // This code only works with header controls.
   ASSERT(lpDrawItemStruct->CtlType == ODT_HEADER);
   HDITEM hdi;
   TCHAR  lpBuffer[256];
   hdi.mask = HDI_TEXT;
   hdi.pszText = lpBuffer;
   hdi.cchTextMax = 256;
   GetItem(lpDrawItemStruct->itemID, &hdi);   // Draw the button frame.
   ::DrawFrameControl(lpDrawItemStruct->hDC, &lpDrawItemStruct->rcItem, DFC_BUTTON, DFCS_BUTTONPUSH);   // Draw the items text using the text color red.
   COLORREF crOldColor = ::SetTextColor(lpDrawItemStruct->hDC, RGB(255,0,0));
   ::DrawText(lpDrawItemStruct->hDC, lpBuffer, _tcslen(lpBuffer), &lpDrawItemStruct->rcItem, DT_SINGLELINE|DT_VCENTER|DT_CENTER);
   ::SetTextColor(lpDrawItemStruct->hDC, crOldColor);
}

使用自定义HeaderCtrl的地方:
定义:

CListCtrl m_ListCtrl;

CMyHeaderCtrl m_HeaderCtrl;

使用:OnInitDialog中

//表格数据初始化
	m_ListCtrl.InsertColumn(0, _T("序号"), LVCFMT_LEFT, 60);
	m_ListCtrl.InsertColumn(1, _T("测试"), LVCFMT_LEFT, 200);
	m_ListCtrl.InsertColumn(2, _T("备注"), LVCFMT_LEFT, 100);

	//插入行
	int nRow = m_ListCtrl.InsertItem(0, _T("1"));
	m_ListCtrl.SetItemText(nRow, 1, _T("www.tsingfun.com")); //设置数据
	m_ListCtrl.SetItemText(nRow, 2, _T("无"));



	CFont *f = new CFont;
    f->CreateFont(13, // nHeight 
                                    0, // nWidth 
                                    0, // nEscapement 
                                    0, // nOrientation 
                                    700, // nWeight 
                                    FALSE, // bItalic 
                                    FALSE, // bUnderline 
                                    0, // cStrikeOut 
                                    ANSI_CHARSET, // nCharSet 
                                    OUT_DEFAULT_PRECIS, // nOutPrecision 
                                    CLIP_DEFAULT_PRECIS, // nClipPrecision 
                                    DEFAULT_QUALITY, // nQuality 
                                    DEFAULT_PITCH | FF_SWISS, // nPitchAndFamily 
                                    _T("宋体")); // lpszFac

	//加载自定义Header
	CHeaderCtrl *pHeader = m_ListCtrl.GetHeaderCtrl();
	if(pHeader == NULL)
		return FALSE;
	m_HeaderCtrl.SubclassWindow(pHeader->m_hWnd);
	m_HeaderCtrl.SetFont(f);

	//设置标题栏自绘
	HDITEM hdItem;
	hdItem.mask = HDI_FORMAT; 
	for(int i=0; i<m_HeaderCtrl.GetItemCount(); i++)
	{
		m_HeaderCtrl.GetItem(i,&hdItem);
		hdItem.fmt|= HDF_OWNERDRAW;

		m_HeaderCtrl.SetItem(i,&hdItem);
	}
效果:


完整工程源码下载CHeaderCtrlDemo.zip

CListCtrl 标题栏 字体颜色 CHeaderCtrl

注:本文为本站或本站会员原创优质内容,版权属于原作者及清泛网所有,
欢迎转载,转载时须注明版权并添加来源链接,谢谢合作! (编辑:admin)
分享到: