CustomDraw with TreeViews

This is the forum for miscellaneous technical/programming questions.

Moderator: 2ffat

CustomDraw with TreeViews

Postby greg221b » Thu Sep 09, 2004 10:30 pm

Hi -
I'm trying to change the foreground colour of items in a tree view using CustomDraw.
I capture the CDDS_PREPAINT drawstage - and set msg.Result = CDRF_NOTIFYITEMDRAW and dispatch this to the parent form.
However, no following interaction with the tree view results in a CDDS_ITEMPREPAINT drawstage being generated - hence I'm unable to do anything. :(
If anyone could help me understand this I'd be eternally thankful -

Postby Damon » Sat Sep 11, 2004 9:26 am

Hi Greg,
The CDDS_ITEMPREPAINT notification should be coming through; could you post a snippet of your code that handles the WM_NOTIFY message? Also, you mentioned that you're dispatching the custom draw messages to the parent form. In order for this to work, the tree-view must be a direct child of the parent form (not, e.g., placed upon a TPanel that's on the form).

User avatar
BCBJ Editor and Admin
BCBJ Editor and Admin
Posts: 285
Joined: Wed May 26, 2004 11:25 pm
Location: Stillwater, OK, USA

Some code...

Postby greg221b » Mon Sep 20, 2004 10:35 pm

Thanks for your reply Damon -
here is the code I am using:

void __fastcall TForm1::WMNotify(TMessage &Msg)
// see if the message is a Custom Draw message
if (lpnm->hdr.code == NM_CUSTOMDRAW)
// see if the message is from the TreeView
if (lpnm->hdr.hwndFrom == TreeView1->Handle)
// grab a pointer to the NMTVCUSTDRAW structure
lptvcd = (NMTVCUSTOMDRAW *)Msg.LParam;

// check the drawing stage
switch (lptvcd->nmcd.dwDrawStage)
// prior to painting...
// tell Windows we want individual
// notification of each item being drawn
ULONG JItem = lptvcd->nmcd.dwItemSpec;
// notification of each item...
// change the font of the first item for example
// for TreeViews, dwItemSpec corresponds to ItemId
ULONG HItem = (ULONG)TreeView1->Items->Item[0]->ItemId;
ULONG IItem = (ULONG)TreeView1->Items->Item[1]->ItemId;
if (lptvcd->nmcd.dwItemSpec == HItem)
SelectObject(lptvcd->nmcd.hdc, NewFont->Handle);

// determine the state of the TreeView item
UINT State = lptvcd->nmcd.uItemState;

// change background color (clHighlight if selected
// item otherwise TreeView's color
if (State & CDIS_SELECTED)
lptvcd->clrTextBk = ColorToRGB(clAqua);
lptvcd->clrTextBk = ColorToRGB(TreeView1->Color);

// tell Windows we changed an attribute
Msg.Result = CDRF_NEWFONT;

//otherwise, let Windows draw the Node
default: Msg.Result = CDRF_DODEFAULT;

Yes, I was getting the CDDS_ITEMPREPAINT message - it
was my inexperience that stopped me seeing that -
my problem seems to be that at the line marked ****,
lptvcd->nmcd.dwItemSpec never seems to correlate to
the two items I am looking at - which are (ULONG)TreeView1->Items->Item[0]->ItemId & ...->Item[1]->...,
hence I am unable to change the colour/fonts of these items.

If anyone could help me I'd very very grateful,
Thanks, Greg.

Return to Technical

Who is online

Users browsing this forum: Bing [Bot] and 14 guests