CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    586

    custom draw listctrl: how to draw little colored rect in subitem

    Hi all,

    The listctrl i use now fills the subitem of an item in the listctrl with a color. This means the complete rectangle of the field is filled with that color. I think it looks better when it is slightly smaller, but i do not know if this is possible. Does somebody has a clue?
    This is what i use now:
    Code:
    void CMyDlg::OnCustomDrawList(NMHDR* pNMHDR, LRESULT* pResult)
    {
    	*pResult = 0;
    
    	LPNMLVCUSTOMDRAW  lplvcd = (LPNMLVCUSTOMDRAW)pNMHDR;
    	
    	// Retrieve row
    	int row = lplvcd->nmcd.dwItemSpec;
    
    	// Check if row is between valid boundaries
    	if(row < 0 || row > m_lst.GetItemCount() - 1)
    	{
    		if(lplvcd->nmcd.dwDrawStage == CDDS_PREPAINT)
    		{
    			*pResult = CDRF_NOTIFYSUBITEMDRAW;
    		}
    		else
    		{
    			*pResult = CDRF_DODEFAULT;
    		}
    		return;
    	}
    
    	// Retrieve item data, which contains ID, color
    	CItemData* pData = (CItemData*)m_lst.GetItemData(row);
    	
    	switch(lplvcd->nmcd.dwDrawStage)
    	{
    		case CDDS_PREPAINT :
    		{
    			*pResult = CDRF_NOTIFYITEMDRAW;
    			return;
    		}
    
    	    // Modify item text and or background
    		case CDDS_ITEMPREPAINT:
    		{
    			// Set color of text
    			lplvcd->clrText = RGB(0,0,0);
    			
    			// If you want the sub items the same as the item,
    			// set *pResult to CDRF_NEWFONT
    			*pResult = CDRF_NOTIFYSUBITEMDRAW;
    			return;
    		}
    
    		// Modify sub item text and/or background
    		case CDDS_SUBITEM | CDDS_PREPAINT | CDDS_ITEM:
    		{
    			// Check if item to be drawn is the color column 
    			if(lplvcd->iSubItem == 1)
    			{
    				// Retrieve color from itemdata
    				if(pData)
    					lplvcd->clrTextBk = pData->m_cItemColor;
    			}
    			*pResult = CDRF_NEWFONT;
    			return;
    		}
    		default:// it wasn't a notification that was interesting to us.
    			*pResult = CDRF_DODEFAULT;
    
    	}
    }
    
    BOOL CMyDlg::OnEraseBkgnd(CDC* pDC) 
    {
    	CRect rect;
    	GetClientRect(rect);
    
    	CBrush brush0(RGB(247, 247, 247));
    	CBrush brush1(RGB(255, 255, 255));
    
    	SCROLLINFO si;
    	ZeroMemory(&si, sizeof(SCROLLINFO));
        si.cbSize = sizeof(SCROLLINFO);
        si.fMask = SIF_POS;
    	GetScrollInfo(SB_HORZ, &si);
    	rect.left -= si.nPos;
    
    	for(int i = 0; i <= m_lst.GetItemCount(); i++)
    	{
    		rect.right = rect.left + m_lst.GetColumnWidth(i);
    		pDC->FillRect(&rect,i%2 ? &brush1 : &brush0);
    		rect.left += m_lst.GetColumnWidth(i);
    	}
    
    	brush0.DeleteObject();
    	brush1.DeleteObject();
    
    	return FALSE;
    	//return CDialog::OnEraseBkgnd(pDC);
    }
    Time is fun when you're having flies

  2. #2
    Join Date
    Feb 2000
    Location
    San Diego, CA
    Posts
    10,354
    Never used LVCUSTOMDRAW before, but could you try manipulating the rcText member of the structure and see if it makes a difference.

  3. #3
    Join Date
    Mar 2003
    Location
    The Netherlands
    Posts
    586
    You mean the rect that is in the nmcd struct : lplvcd->nmcd.rc.
    Changing this rect does not make a difference. Still the complete field is filled with the same color.
    I think it has something to do with the drawing stage in which the rectangle has to be changed, but i do not know what drawing stage i should use.
    Time is fun when you're having flies

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured