在网上找了好久都没结果,后终于找到一个全部贴图的,稍微修改下就变成时修改颜色的
1.要重写CListCtrl组件。
2.在里面增加修改标题栏的类。
3.在这个类里面重写OnPaint()函数。
---------------------------------------------------------------------------------
//重写的CListCtrl
class CMyListCtrl : public CListCtrl
{
public:
CSkinHeaderCtrl m_SkinHeaderCtrl;
}
---------------------------------------------------------------------------------
//CListCtrl标题栏皮肤类(CSkinHeaderCtrl)中的OnPaint()函数
//在此可以全部贴图,或修改颜色
void CSkinHeaderCtrl::OnPaint()
{
CPaintDC dc(this); // device context for painting
CRect rect, rectItem, clientRect;
GetClientRect(&rect);
GetClientRect(&clientRect);
CMemDC memDC(&dc, rect);
CDC bitmapDC;
bitmapDC.CreateCompatibleDC(&dc);
memDC.FillSolidRect(&rect, m_BkColor);//CListCtrl标头背景颜色
int nItems = GetItemCount();
CBitmap bitmap;
bitmap.LoadBitmap(IDB_COLUMNHEADER_SPAN);//标题中区分项目的线条,在资源中应该是一条1个像素宽的竖线图片
for(int i = 0; i
TCHAR buf1[256];
HD_ITEM hditem1;
hditem1.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
hditem1.pszText = buf1;
hditem1.cchTextMax = 255;
GetItem( i, &hditem1 );
GetItemRect(i, &rect);
CBitmap* pOldBitmap = NULL;
//make sure we draw the start piece
//on the first item so it has a left border
//For the following items we will just use the
//right border of the previous items as the left
//border
if(hditem1.iOrder==0)
{
}
else
{
pOldBitmap = bitmapDC.SelectObject(&bitmap);
memDC.BitBlt(rect.left,rect.top,1,20,&bitmapDC,0,0,SRCCOPY);
}
//draw the end piece of the column header
if(i==nItems-1)
{
memDC.BitBlt((rect.right-0), 0, 2, 20, &bitmapDC,0,0,SRCCOPY);
bitmapDC.SelectObject(pOldBitmap);
}
rectItem.DeflateRect(2,2,2,2);
UINT uFormat = DT_SINGLELINE | DT_NOPREFIX | DT_TOP |DT_CENTER | DT_END_ELLIPSIS ;
DRAWITEMSTRUCT DrawItemStruct;
GetItemRect(i, &rectItem);
DrawItemStruct.CtlType = 100;
DrawItemStruct.hDC = dc.GetSafeHdc();
DrawItemStruct.itemAction = ODA_DRAWENTIRE;
DrawItemStruct.hwndItem = GetSafeHwnd();
DrawItemStruct.rcItem = rectItem;
DrawItemStruct.itemID = i;
DrawItem(&DrawItemStruct);
CFont font;
LOGFONT lf;
memset(&lf, 0, sizeof(LOGFONT));
lf.lfHeight = 12;
strcpy(lf.lfFaceName, "宋体");
font.CreateFontIndirect(&lf);
CFont* def_font = memDC.SelectObject(&font);
memDC.SetBkMode(TRANSPARENT);
memDC.SetTextColor(RGB(0,0,255));//CListCtrlt头部字体颜色
rectItem.DeflateRect(2,2,2,2);
TCHAR buf[256];
HD_ITEM hditem;
hditem.mask = HDI_TEXT | HDI_FORMAT | HDI_ORDER;
hditem.pszText = buf;
hditem.cchTextMax = 255;
GetItem( DrawItemStruct.itemID, &hditem );
memDC.DrawText(buf, &rectItem, uFormat);
memDC.SelectObject(def_font);
font.DeleteObject();
}
}