CMFCListCtrl控件使用

CMFCListCtrl是MS从CListCtrl派生出来的新类,灵活性上有所进步,特别是改变行列颜色、改变字体,不用像CListCtrl很多步骤,要处理很复杂的数据结构。现在记录下,先看效果图

  1. 用类向导从CMFCListCtrl派生出CMyListCtrl类;
  2. 声明虚函数
    virtual COLORREF OnGetCellTextColor(int nRow, int nColum);
    	virtual COLORREF OnGetCellBkColor(int nRow, int nColum);
    	virtual HFONT OnGetCellFont(int nRow, int nColum, DWORD dwData = 0);

  3. 实现函数里写
    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;
    	
    }

  4. 标题栏也是可以改变的,改变了字体
    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);
}


评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值