CListCtrl 自定义栅格(vc6)

有时候需要自定义CListCtrl 的栅格,比如其颜色,宽度等,但是如果通过自定义画,宿主画均不能达到这种效果,这个时候只能通过自己来处理WM_PAINT消息来达到这种效果了,代码如下:

void CMyList::OnPaint() 
{
	
	CPaintDC dc(this); // device context for painting
	
	// TODO: Add your message handler code here

	// Do not call CListCtrl::OnPaint() for painting messages
	CRect rect,txtRect;
	CPoint mypoint; 
	int chunk_height=GetCountPerPage();
	int nStart=GetTopIndex();

	CPen pen(PS_SOLID,1,RGB(200,200,100));

	dc.SelectObject(&pen);
	CFont *ft=GetFont();
	dc.SelectObject(ft);
	dc.SetBkMode(TRANSPARENT);

	CRect itemRC;
	GetClientRect(itemRC);
	for (int i=nStart;i<= (nStart + chunk_height);i++)
	{
		int nWidth=GetColumnWidth(0);
		GetItemPosition(i,&mypoint);
		rect.left = mypoint.x -2;
		rect.top  = mypoint.y-2;
		rect.right = mypoint.x + nWidth -2;
		GetItemPosition(i+1,&mypoint);
		rect.bottom =mypoint.y;

		dc.Rectangle(rect);

		CString szText=GetItemText(i,0);
		txtRect = rect;
		txtRect.DeflateRect(6,0);
		dc.DrawText(szText,txtRect,DT_LEFT );

		rect.left = rect.right ;
		nWidth=GetColumnWidth(1);
		rect.right = rect.left + nWidth ;		
		dc.Rectangle(rect);

		rect.left = rect.right ;
		nWidth=GetColumnWidth(2);
		rect.right = rect.left + nWidth ;		
		dc.Rectangle(rect);	
	

		rect.left = rect.right;
		rect.right = itemRC.right;
		dc.Rectangle(rect);	
	}
}


程序效果如下:

你可能感兴趣的:(CListCtrl 自定义栅格(vc6))