由于很少用界面对MFC不太熟,所在也参数了一下网络完成了一个自绘clistctrl类,不正确的请指出来。
//MyListCtrl.h
#pragma once
#include "afxcmn.h"
class CMyListCtrl :
public CListCtrl
{
public:
CString m_KeyStr;//要更改颜色的字符串
public:
CMyListCtrl(void);
~CMyListCtrl(void);
void GetKeyStrInfo(CString str);//获得要显示的字符串
protected:
//DECLARE_MESSAGE_MAP()
virtual void PreSubclassWindow();
public:
virtual void DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct);
};
//MyListCtrl.cpp
#include "StdAfx.h"
#include "MyListCtrl.h"
#include <vector>
#include <iostream>
CMyListCtrl::CMyListCtrl(void)
{
m_KeyStr = L"";
}
CMyListCtrl::~CMyListCtrl(void)
{
}
void CMyListCtrl::PreSubclassWindow()
{
// TODO: 在此添加专用代码和/或调用基类
ModifyStyle(0,LVS_OWNERDRAWFIXED);
CListCtrl::PreSubclassWindow();
}
//获得要显示的字符串
void CMyListCtrl::GetKeyStrInfo(CString str)
{
m_KeyStr = str;
}
void CMyListCtrl::DrawItem(LPDRAWITEMSTRUCT lpDrawItemStruct)
{
TCHAR lpBuffer[256];
LV_ITEM lvi;
LV_COLUMN lvc, lvcprev ;
::ZeroMemory(&lvc, sizeof(lvc));
::ZeroMemory(&lvcprev, sizeof(lvcprev));
lvc.mask = LVCF_WIDTH | LVCF_FMT;
lvcprev.mask = LVCF_WIDTH | LVCF_FMT;
CDC* pDC;
pDC = CDC::FromHandle(lpDrawItemStruct->hDC);
CRect rtClient;
GetClientRect(&rtClient);
for ( int nCol=0; GetColumn(nCol, &lvc); nCol++)
{
CRect rcItem;
if (!GetSubItemRect(lpDrawItemStruct->itemID,nCol,LVIR_LABEL,rcItem))
continue;
::ZeroMemory(&lvi, sizeof(lvi));
lvi.iItem = lpDrawItemStruct->itemID;
lvi.mask = LVIF_TEXT | LVIF_PARAM;
lvi.iSubItem = nCol;
lvi.pszText = lpBuffer;
lvi.cchTextMax = sizeof(lpBuffer);
VERIFY(GetItem(&lvi));
CRect rcTemp;
rcTemp = rcItem;
if (nCol==0)
{
rcTemp.left -=2;
}
if ( lpDrawItemStruct->itemState & ODS_SELECTED )
{
pDC->FillSolidRect(&rcTemp, GetSysColor(COLOR_HIGHLIGHT)) ;
pDC->SetTextColor(GetSysColor(COLOR_HIGHLIGHTTEXT)) ;
}
COLORREF oltextcl = pDC->GetTextColor();//以前字的颜色
CString strt = lpBuffer;
std::vector<int>vi;
int oldInext=0;
if (strt!=L""&&m_KeyStr!=L"")
{
int index=0,findstr=0;
while((index =strt.Find(m_KeyStr,findstr))!=-1)
{
vi.push_back(index);
findstr=index+m_KeyStr.GetLength();
}
std::vector<int>::iterator iter=vi.begin();
for (;iter!=vi.end();++iter)
{
CRect rcTemp;
rcTemp = rcItem;
if (oldInext==0)
{
if (*iter!=0)
{
DrawText(lpDrawItemStruct->hDC, strt.GetBuffer(),*iter,&rcTemp, DT_LEFT) ;
CString strtt = strt.Mid(*iter);
CString stt1 = strt.Mid(oldInext,*iter);
CSize tt= pDC->GetTextExtent(stt1);
rcTemp.left+=tt.cx;
pDC->SetTextColor(RGB(255,0,0));
DrawText(lpDrawItemStruct->hDC, strt.GetBuffer()+(*iter),m_KeyStr.GetLength(),&rcTemp, DT_LEFT);
pDC->SetTextColor(oltextcl);
oldInext
= *iter+m_KeyStr.GetLength();
}
else
{
pDC->SetTextColor(RGB(255,0,0));
DrawText(lpDrawItemStruct->hDC, strt.GetBuffer(),m_KeyStr.GetLength(),&rcTemp, DT_LEFT);
pDC->SetTextColor(oltextcl);
oldInext
= m_KeyStr.GetLength();
}
}
else
{
CString strtt1 = strt.Mid(0,oldInext);
CSize tt= pDC->GetTextExtent(strtt1);
rcTemp.left+=tt.cx;
DrawText(lpDrawItemStruct->hDC, strt.GetBuffer()+oldInext,*iter-oldInext,&rcTemp, DT_LEFT) ;//画以前的
CString strtt = strt.Mid(*iter);
strtt1 = strt.Mid(0,*iter);
tt= pDC->GetTextExtent(strtt1);
rcTemp = rcItem;
rcTemp.left+=tt.cx;
pDC->SetTextColor(RGB(255,0,0));
DrawText(lpDrawItemStruct->hDC, strt.GetBuffer()+(*iter),m_KeyStr.GetLength(),&rcTemp, DT_LEFT);
pDC->SetTextColor(oltextcl);
oldInext
= *iter+m_KeyStr.GetLength();
}
}
CString stt1 = strt.Mid(0,oldInext);
CSize tt= pDC->GetTextExtent(stt1);
rcItem.left+=tt.cx;
DrawText(lpDrawItemStruct->hDC, strt.GetBuffer()+oldInext,strt.GetLength()-oldInext,&rcItem, DT_LEFT) ;
}
else
DrawText(lpDrawItemStruct->hDC, lpBuffer, _tcslen(lpBuffer),&rcItem, DT_LEFT) ;
}
}
//调用
//在初始化函数里
LONG lStyle;
lStyle = GetWindowLong(m_listData.m_hWnd, GWL_STYLE);// 获取当前窗口style
lStyle &= ~LVS_TYPEMASK; // 清除显示方式位
lStyle |= LVS_REPORT|LVS_SHOWSELALWAYS|LVS_SINGLESEL; // 设置style
SetWindowLong(m_listData.m_hWnd, GWL_STYLE, lStyle);// 设置style
SetWindowLong(m_listData.m_hWnd, GWL_STYLE, lStyle);// 设置style
//LVS_EX_DOUBLEBUFFER 双缓冲
m_listData.SetExtendedStyle(LVS_EX_GRIDLINES |LVS_EX_FULLROWSELECT|LVS_EX_HEADERDRAGDROP|LVS_EX_DOUBLEBUFFER); // 设置扩展风格
m_listData.InsertColumn( 0, L"456456465465", LVCFMT_LEFT/*LVCFMT_CENTER*/, 150);//插入列
m_listData.InsertColumn( 1, L"564564654", LVCFMT_LEFT/*LVCFMT_CENTER*/, 100);//插入列
m_listData.InsertColumn( 2, L"dfasdfsafsadf", LVCFMT_LEFT/*LVCFMT_CENTER*/, 100);//插入列
m_listData.InsertColumn( 3, L"afdsfsdfsfa", LVCFMT_LEFT/*LVCFMT_CENTER*/, 115);//插入列
m_listData.GetKeyStrInfo(L"5");
m_listData.InsertItem(0,L"张三456465");
m_listData.SetItemText(0,1,L"551755855cm");
m_listData.SetItemText(0,2,L"57505kg");
m_listData.SetItemText(0,3,L"52059年1月15日23时40分");
结果:如图
原文:http://blog.csdn.net/yjwffgip456/article/details/44594479