Custom Draw Tree Control
MFC 6 supports Custom Draw Tree Control, which makes setting different colors for individual tree items much simpler (see Zafir Anjum's article "Setting color and font attribute for individual items" to find out how to do it without Custom Draw support). Custom draw tree control sends an NM_CUSTOMDRAW notification to its owner. The trick is that the control sends first NM_CUSTOMDRAW notification with dwDrawStage set to CDDS_PREPAINT, and no further notifications will be sent, unless this notification is processed correctly. Setting pResult to CDRF_NOTIFYITEMDRAW causes the control to send NM_CUSTOMDRAW notifications for individual items with dwDrawStage set to CDDS_ITEMPREPAINT, which allows changing item's attributes before it is drawn.
BOOL CMainFrame::OnNotify(WPARAM wParam, LPARAM lParam, LRESULT* pResult)
{
LPNMHDR pNmhdr = (LPNMHDR)lParam;
switch (pNmhdr->code)
{
case NM_CUSTOMDRAW:
{
LPNMTVCUSTOMDRAW pCustomDraw = (LPNMTVCUSTOMDRAW)lParam;
switch (pCustomDraw->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
// Need to process this case and set pResult to CDRF_NOTIFYITEMDRAW,
// otherwise parent will never receive CDDS_ITEMPREPAINT notification. (GGH)
*pResult = CDRF_NOTIFYITEMDRAW;
return true;
case CDDS_ITEMPREPAINT:
switch (pCustomDraw->iLevel)
{
// painting all 0-level items blue,
// and all 1-level items red (GGH)
case 0:
if (pCustomDraw->nmcd.uItemState == (CDIS_FOCUS | CDIS_SELECTED)) // selected
pCustomDraw->clrText = RGB(255, 255, 255);
else
pCustomDraw->clrText = RGB(0, 0, 255);
break;
case 1:
if (pCustomDraw->nmcd.uItemState == (CDIS_FOCUS | CDIS_SELECTED)) // selected
pCustomDraw->clrText = RGB(255, 255, 255);
else
pCustomDraw->clrText = RGB(255, 0, 0);
break;
}
*pResult = CDRF_SKIPDEFAULT;
return false;
}
}
break;
}
return CFrameWnd::OnNotify(wParam, lParam, pResult);
}

Comments
how can i add list view for the same
Posted by vishal_pansari on 08/20/2004 01:12amThis is really a very good code. but tell me, how can i add list view for the same to the right side of the window. ??
-
Reply
Replyvishal sir plz contact me, paras from indore
Posted by thematrix4u on 12/17/2008 01:51amCan i apply the same logic to a scrollbar?
Posted by Legacy on 01/27/2004 12:00amOriginally posted by: prashanth prabhu p
I would like to know wether the windows scrollbar control supports customdraw and wether the same logic could be applied for drawing a custom scrollbar.
Replychanging item RECT workaround
Posted by Legacy on 10/28/2002 12:00amOriginally posted by: David Woods
Replyhow to make a transparent CTreeCtrl
Posted by Legacy on 10/06/2002 12:00amOriginally posted by: Nitin Dbey
i am trying to make transparent tree control, i have tryed to make it by selecting the options given in the properties window of CTreeCtrl. but its not working
thankz
Replytreeview customdraw and why horizontal scrolling does not work
Posted by Legacy on 11/28/2001 12:00amOriginally posted by: lammerville
I draw my personnal image and text like this
but horizontal scrolling does not work, why ?
void CViewArbre::OnCustomDraw(NMHDR* pNMHDR, LRESULT* pResult)
{
NMTVCUSTOMDRAW* pTVCD = (NMTVCUSTOMDRAW*)pNMHDR;
// TODO: Add your control notification handler code here
*pResult = 0;
switch (pTVCD->nmcd.dwDrawStage)
{
case CDDS_PREPAINT:
*pResult = CDRF_NOTIFYITEMDRAW ;
break;
case CDDS_ITEMPREPAINT:
{
CDC* pDC = CDC::FromHandle(pTVCD->nmcd.hdc);
CRect rc(pTVCD->nmcd.rc);
CRect rcImg(pTVCD->nmcd.rc);
POINT p;
rcImg.OffsetRect(pTVCD->iLevel*16,0) ;
p=rcImg.TopLeft();
rc.OffsetRect(rcImg.left+16,0);
p=rcImg.TopLeft();
m_il.Draw(pDC,0,p,ILD_NORMAL);
CString strTexte = GetTreeCtrl().GetItemText((HTREEITEM)pTVCD->nmcd.dwItemSpec);
pDC->TextOut(rc.left,rc.top,strTexte) ;
TRACE("%d %d,%d sur %d x %d\n",rand(),rc.left,rc.top,rc.Width(),rc.Height());
}
*pResult = CDRF_SKIPDEFAULT ;
break;
}
}
ReplyExTree
Posted by Legacy on 10/18/2001 12:00amOriginally posted by: Mike Philis
Replyhow to get a CDDS_ITEMPOSTPAINT?
Posted by Legacy on 10/20/2000 12:00amOriginally posted by: Don Metzler
How can I ensure that I will get a CDDS_ITEMPOSTPAINT notification?
Thanks,
Don
ReplySolution : I don't get a CDDS_ITEMPREPAINT message
Posted by Legacy on 01/07/2000 12:00amOriginally posted by: Nikolai Sander
In case you don't get a CDDS_ITEMPREPAINT message after returning CDRF_NOTIFYITEMDRAW to the CDDS_PREPAINT, you can try to set SetWindowLong(hDlg,DWL_MSGRESULT,CDRF_NOTIFYITEMDRAW); in case you have a dialog box.
Nikolai
ReplyHow to change item-size ?
Posted by Legacy on 10/27/1999 12:00amOriginally posted by: Martin Thoma
Hi !
The Custom-Draw-TreeCtrl works fine. But now I want to set the width of each item, so I can draw something instead of text. The main problem is, that the scroll-bar will only notice the width of the text, but I want to set a user-defined width. Is this possible ?
Regards
Martin
ReplyChanging the color of a selected item in a MFC CTreeView
Posted by Legacy on 07/15/1999 12:00amOriginally posted by: Carl Bomgardner
ReplyLoading, Please Wait ...