Download Demos http://www.foolab.com/download/cooltree_r.zip
Download source code http://www.foolab.com/download/cooltree.zip
Download Flight (OUTPLAY ...) http://www.foolab.com/download/outplay_1.03.zip
Motivation
When I use my spare time to develop a flying (OUTPLAY ...), after the mail program like Outlook,
I finally jumped up easily, but when I gooked in my feet, I suddenly saw Outlook used in front of me.
Single columns use different colors to display new emails, so I decided, I should make my Outplay ...
So I immediately found some articles about CTREEVIEW in CodeGuru, I found some suitable letters.
Interest, the big flower spent about 30 minutes, I made my flying (Outplay ...) also had similar results.
Let me slowly tell you, you will also find that it is so simple and convenient, just add it in your ctreeView.
A few lines of code
What should you do? (FOLLOW ME)
First, assume your ctreeView derived CTreeViewEx, then you need to be in TreeViewEx.h
Add as follows:
AFX_MSG Void OncustomDraw (NmHDR * PNMHDR, LRESULT * PRESULT);
Then you need to add it in your message mapping
ON_NOTIFY_REFLECT (nm_customdraw, oncustomed)
Add the following code in ONCUSTeomDraw:
Void CleftView :: OnCustomDraw (lpnmhdr pnmhdr, lresult * pRESULT) {static crect rcitem; static int nitemstate
LPNMTVCUSTOMDRAW pCustomDraw = (LPNMTVCUSTOMDRAW) pNmhdr; 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;
// reposuition the viewport So the TreeCtrl DefWindowProc Doesn't draw to viewport 0/0 // :: setviewportorgex(pcustomDraw biamcd.hdc, / * m_noffset * / 0, 0, null); Break;
case CDDS_ITEMPREPAINT: // set the background and foregroundcolor of the item to the background, so you do not see the default drawing of the text // get item state (focus, selected ..), because it will be drawn of us nItemState = pcustomdraw-> nmcd.uitemstate;
pCustomDraw-> nmcd.uItemState & = ~ CDIS_FOCUS; pCustomDraw-> clrText = m_colHilightText; // remember the drawing rectangle of the item so we can draw it ourselves m_pTree-> GetItemRect ((HTREEITEM) pCustomDraw-> nmcd.dwItemSpec, & rcItem, TRUE); * pResult = CDRF_NOTIFYPOSTPAINT; break; case CDDS_ITEMPOSTPAINT: DrawTreeItem (nItemState, rcItem, pCustomDraw-> nmcd.hdc, (HTREEITEM) pCustomDraw-> nmcd.dwItemSpec); break; default: * pResult = CDRF_DODEFAULT;}}
Finally, what you need to do is just painted in DrawTreeItem, you want the effect you want is as follows:
void CLeftView :: DrawTreeItem (int nItemState, CRect rcItem, HDC hdc, HTREEITEM hItem) {// if the item has got the focus, we have to draw sorouinding rectangle and fill a rect blue COLORREF colText = m_colText;
if (nItemState & CDIS_FOCUS) {:: FillRect (hdc, & rcItem, (HBRUSH) m_BackBrush.m_hObject); // Add new focus rect code ... :: DrawFocusRect (hdc, & rcItem); colText = m_colHilightText; // =: GetSysColor (COLOR_HIGHLIGHTTEXT);} else if (nItemState & CDIS_SELECTED) {:: FillRect (hdc, & rcItem, (HBRUSH) m_GrayBrush.m_hObject);} else {TRACE ( "CLEAR HIGH / n"); // add this to clear leftover Highlight Bar :: FillRect (HDC, & RCITEM, (HBRUSH) M_BackBrushnormal.m_hobject);
// always write text without background :: SetBkMode (hdc, TRANSPARENT); :: SetTextColor (hdc, colText); CString str = m_pTree-> GetItemText (hItem); :: DrawText (hdc, str, -1, & rcItem, DT_VCENTER | DT_SINGLINE | DT_WORD_ELLIPSIS;
// You May Modify THEM According to your need int nnew = 20; cstring strnew = _t (""); strnew.format (_t ("(" (% d) "), nnew); :: setTextColor (HDC, RGB (0 , 0,255)); RC.LEFT = rcitem.right 2; rc.right = rc.Left 100; :: FillRect (HDC, & RC, (HBRUSH) m_backbrushnormal.m_hobject); :: drawtext HDC, STRNEW, -1, & RC, DT_VCENTER | DT_SINGLINE | DT_WORD_ELLIPSIS);} You need to explain if you support drag and drop and you need some changes.
May you be happy
Lazen (email: outplay@foolab.com) 2002.12.29