CMFCListCtrl是MS从CListCtrl派生出来的新类,灵活性上有所进步,特别是改变行列颜色、改变字体,不用像CListCtrl很多步骤,要处理很复杂的数据结构。现在记录下,先看效果图
- 用类向导从CMFCListCtrl派生出CMyListCtrl类;
- 声明虚函数
virtual COLORREF OnGetCellTextColor(int nRow, int nColum); virtual COLORREF OnGetCellBkColor(int nRow, int nColum); virtual HFONT OnGetCellFont(int nRow, int nColum, DWORD dwData = 0);
- 实现函数里写
COLORREF CMyListCtrl::OnGetCellTextColor(int nRow, int nColum) { return(nRow % 2) == 0 ? RGB(128, 37, 0) : RGB(0, 0, 0); } COLORREF CMyListCtrl::OnGetCellBkColor(int nRow, int nColum) { return(nRow % 2) == 0 ? RGB(253, 241, 249) : RGB(196, 238, 254); } HFONT CMyListCtrl::OnGetCellFont(int nRow, int nColum, DWORD /*dwData* = 0*/) { return afxGlobalData.fontDefaultGUIBold; }
- 标题栏也是可以改变的,改变了字体
m_font.CreateFont(20,0,0,0,FW_DONTCARE,FALSE,FALSE,0, 1, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH|FF_SWISS, _T("宋体")); m_wndListCtrl.GetHeaderCtrl().SetFont(&m_font);
=====================================================================
2015.1.23今天找到了一个设置行高的投机取巧方法
添加一个方法
void CMyListCtrl::SetRowHeight(int nHeight)
{
CImageList im;
im.Create(1, nHeight, ILC_COLOR4, 10, 10);
SetImageList(&im, LVSIL_SMALL );
}
注意这里虚拟插入的icon的宽度为1,高度为需要设定的行高,运行之后效果如下
==================================
2015.7.20
新增加一个自动调整最后一列列宽的方法供调用,需要添加完表头后显式调用
void CMyListCtrl::AutoColumn(void)//自动调节最后一列
{
CRect xRect;
int nLastColumnWidth = 0;
int nColumnNum = 0;
GetClientRect(&xRect);//OleControl的
nLastColumnWidth = xRect.Width();
nColumnNum = GetHeaderCtrl().GetItemCount();
for (int i = 0; i < nColumnNum - 1; i++)
nLastColumnWidth = nLastColumnWidth - GetColumnWidth(i);
SetColumnWidth(nColumnNum - 1, nLastColumnWidth);
}