The ctreectrl class is overloaded because of the company's project requirements. Effect
The Code is as follows:
# Pragma once
# Include "afxcen. H"
Class cownerdrawtree:
Public ctreectrl
{
Public:
Cownerdrawtree (void );
~ Cownerdrawtree (void );
Void drawitems (CDC * PDC );
Protected:
Declare_message_map ()
Afx_msg void onpaint ();
Afx_msg void onnmclick (nmhdr * pnmhdr, lresult * presult );
PRIVATE:
Cbitmap m_bmp parentitem;
};
# Include "stdafx. H"
# Include "ownerdrawtree. H"
# Include "resource. H"
Cownerdrawtree: cownerdrawtree (void)
{
M_bmp parentitem.loadbitmap (idb_bitmap1 );
}
COwnerDrawTree ::~ COwnerDrawTree (void)
{
M_bmp parentitem.deleteobject ();
}
BEGIN_MESSAGE_MAP (COwnerDrawTree, CTreeCtrl)
ON_WM_PAINT ()
On_policy_reflect (NM_CLICK, & COwnerDrawTree: OnNMClick)
END_MESSAGE_MAP ()
Void COwnerDrawTree: OnPaint ()
{
CPaintDC dc (this );
CDC memDC;
CBitmap bm_ff;
CBitmap * bm_old;
CFont * font, * old_font;
CFont fontDC;
Int old_mode;
CRect rcClient;
GetClientRect (& rcClient );
MemDC. CreateCompatibleDC (& dc );
Bm_ff.CreateCompatibleBitmap (& dc, rcClient. Width (), rcClient. Height ());
// Select the bitmap into the off-screen DC.
Bm_old = (CBitmap *) memDC. SelectObject (& bm_ff );
CBrush bshTmp (RGB (255,255,255 ));
MemDC. FillRect (& rcClient, & bshTmp );
Font = GetFont ();
Old_font = memDC. SelectObject (font );
// We're going to draw text transparently
Old_mode = memDC. SetBkMode (TRANSPARENT );
DrawItems (& memDC );
Dc. BitBlt (0, 0, rcClient. Width (), rcClient. Height (), & memDC, 0, 0, SRCCOPY );
MemDC. SelectObject (old_font );
MemDC. SetBkMode (old_mode );
MemDC. SelectObject (bm_old );
}
Void COwnerDrawTree: OnNMClick (NMHDR * pNMHDR, LRESULT * pResult)
{
// TODO: Add your control notification handler code here
Htreeitem hitem = getselecteditem ();
Trace (L "% d/N", hitem );
* Presult = 0;
}
Void cownerdrawtree: drawitems (CDC * PDC)
{
Htreeitem hitem = getfirstvisibleitem ();
If (hitem = NULL)
Return;
Do
{
Crect rcclient;
Crect rcitem;
Crect rctext;
Getclientrect (& rcclient );
Getitemrect (hitem, rcitem, true );
Rctext = rcitem;
// Draw Group Background
Htreeitem hparent = getparentitem (hitem );
If (hparent = NULL)
{
CDC memdc;
MemDC. CreateCompatibleDC (pDC );
CBitmap * pbmpOld = memDC. SelectObject (& m_bmp parentitem );
PDC-> StretchBlt (0, rcItem. top, rcClient. Width (), rcItem. Height (), & memDC, 0, 0, 4, 24, SRCCOPY );
MemDC. SelectObject (pbmpOld );
// Icon
UINT uState = GetItemState (hItem, TVIS_EXPANDED );
If (uState & TVIS_EXPANDED) = TVIS_EXPANDED)
{
//: DrawIconEx (pDC-> GetSafeHdc (), rcText. left + 10, rcItem. top + 4, m_hExpandIcon, m_sizeExpand.cx, m_sizeExpand.cy, 0, 0, DI_NORMAL );
}
Else
{
//: Drawiconex (PDC-> getsafehdc (), rctext. Left + 10, rcitem. Top + 4, m_hcollapseicon, m_sizeexpand.cx, m_sizeexpand.cy, 0, 0, di_normal );
}
Rctext = rcitem;
Rctext. Left + = 30;
}
Else
{
// Draw selected item.
Uint nstate = getitemstate (hitem, tvis_selected );
If (tvis_selected & nstate)
{
Crect rcitemtmp = rcitem;
Rcitemtmp. Left = 0;
Rcitemtmp. Right = rcclient. Right;
PDC-> fillsolidrect (rcitemtmp, RGB (127 ));
}
}
// Draw image
Int nImage =-1;
Int nSelectedImage =-1;
BOOL bRet = GetItemImage (hItem, nImage, nSelectedImage );
CImageList * pImgList = GetImageList (TVSIL_NORMAL );
If (bRet & pImgList! = NULL)
{
IMAGEINFO ii;
PImgList-> GetImageInfo (nImage, & ii );
CRect rcImage = ii. rcImage;
CRect rcItem;
GetItemRect (hItem, rcItem, FALSE );
Int nImageTop = max (rcItem. top, rcItem. top + (rcItem. Height ()-rcImage. Height ()/2 );
PImgList-> Draw (pDC, nImage, CPoint (rcText. left-rcImage. Width ()-2, nImageTop), ILD_TRANSPARENT );
}
// Draw text
/* CFont * pFontOld = NULL;
If (hParent = NULL)
{
CFont * pFontD = NULL;
GetTextFont (NULL, & pFontD );
PFontOld = pDC-> SelectObject (pFontD );
}
Else
{
CFont * pFontN = NULL;
GetTextFont (& pFontN, NULL );
PFontOld = pDC-> SelectObject (pFontN );
}*/
RcText. right = rcClient. right;
PDC-> SetBkMode (TRANSPARENT );
CString strText = GetItemText (hItem );
PDC-> drawtext (strtext, rctext, dt_left | dt_vcenter | dt_singleline | dt_vcenter );
// PDC-> SelectObject (pfontold );
} While (hitem = getnextvisibleitem (hitem ))! = NULL );
}