Home > Software engineering >  CTreeCtrl custom painting
CTreeCtrl custom painting

Time:11-08

I just want to custom painting tree control item text content, because need two lines, what is the convenient way?

+ - in front of the parent node, and to display the dotted line,

CodePudding user response:

1 each double height
Void CMyTree: : PreSubclassWindow ()
{
//TODO: Add your specialized code here and/or call the base class
GetClientRect (m_cltRect);
SetItemHeight (40);//2 lines
CTreeCtrl: : PreSubclassWindow ();
}

2
 
Void CMyTree: : OnCustomDraw (NMHDR * pNMHDR, LRESULT * pResult)
{
LPNMCUSTOMDRAW pNMCD=reinterpret_cast & lt; LPNMCUSTOMDRAW> (pNMHDR);

LPNMTVCUSTOMDRAW LPNMCD=(LPNMTVCUSTOMDRAW pNMCD);
If (LPNMCD - & gt; NMCD. DwDrawStage==CDDS_PREPAINT)
{
* pResult=CDRF_NOTIFYITEMDRAW;
return;
}
Else if (LPNMCD - & gt; NMCD. DwDrawStage==CDDS_ITEMPREPAINT)
{
HTREEITEM hItem=(HTREEITEM) LPNMCD - & gt; NMCD. DwItemSpec;
HDC HDC=pNMCD - & gt; HDC.
UINT state=GetItemState (hItem, TVIF_STATE);
CRect rcItem (pNMCD - & gt; Rc);
Int ind=GetIndent ();//=20
RcItem. Left +=ind * LPNMCD - & gt; ILevel;
RcItem. Right +=ind * LPNMCD - & gt; ILevel;
//the draw bt box
CRect rc;//h=20
Rc=rcItem;
//rc. Left +=2;
Rc. Right=rc. Left + 16;
Rc. Top +=2;
Rc. Bottom -=2;
If (state & amp; TVIS_EXPANDED)
DrawIconEx (HDC, rc. Left, rc. Top, m_hIcon2, dec 16, NULL, NULL (HBRUSH), DI_NORMAL);
The else
DrawIconEx (HDC, rc. Left, rc. Top, m_hIcon1, dec 16, NULL, NULL (HBRUSH), DI_NORMAL);
//draw the image
Int nImage nSelectedImage;
GetItemImage (hItem, nImage nSelectedImage);
//
Rc=rcItem;
Rc. Left +=26;
Rc. Right=rc. Left + 16;
Rc. Top +=2;
Rc. Bottom -=2;
HICON ico.
If (state & amp; TVIS_SELECTED)
{
: : SetBkColor (HDC, RGB (0, 255));
Ico=((CTreeCtrlDlg *) GetParent ()) - & gt; ImageTreeList. ExtractIcon (nSelectedImage);
}
The else
{
: : SetBkColor (HDC, RGB (255255255));
Ico=((CTreeCtrlDlg *) GetParent ()) - & gt; ImageTreeList. ExtractIcon (nImage);
}
//
DrawIconEx (HDC, rc. Left, rc. Top, ico, dec 16, NULL, NULL (HBRUSH), DI_NORMAL);
//
Cstrings strText;
StrText=GetItemText (hItem);
//
RcItem. Left +=50;//| DT_SINGLELINE | DT_VCENTER | DT_SINGLELINE | DT_VCENTER
DrawText (HDC, strText, strText GetLength (), & amp; RcItem DT_LEFT);
RcItem. Top +=20;
DrawText (HDC, "2nd Line", 8, & amp; RcItem DT_LEFT);
* pResult=CDRF_SKIPDEFAULT;
return;
}
//
* pResult=CDRF_DODEFAULT;
return;
}

CodePudding user response:

  • Related