I have a TTreeView in Delphi, with nodes at three levels.
I use node data to store another label besides node text.
Type TNodeData = class ExtraNodeLabel: WideString;
I have an OnAdvancedCustomDrawItem event where I want to display this ExtraNodeLabel in front of node text. I want to achieve this:
- The blue text will be an additional label.
- high-light element: the first two words are also an additional label

What I got so far is:

Problems:
- For some reason I cannot draw text with a different style if I use
DrawText / drawTextW (I need drawtextW because of the data in unicode) - Another problem is that everything outside the focus rectangle does not matter
What you need to solve:
- How can I draw text with a different style using
DrawText / drawTextW - How can I make all text clickable?
The code:
procedure TMainForm.TntTreeView1AdvancedCustomDrawItem( Sender: TCustomTreeView; Node: TTreeNode; State: TCustomDrawState; Stage: TCustomDrawStage; var PaintImages, DefaultDraw: Boolean); var txtrect, fullrect : TRect; DC: HDC; fs: integer; fc: TColor; ExtralabelRect: TRect; nData: TNodeData; begin nData := nil; if assigned(Node.Data) then begin nData := TNodeData(Node.Data); end; DC := TntTreeView1.canvas.Handle; txtRect := Node.DisplayRect(True); fullrect := Node.DisplayRect(False); if stage = cdPostPaint then begin TntTreeView1.Canvas.FillRect(txtRect); if (cdsFocused In State) And (cdsSelected in State) then begin DrawFocusRect(DC,txtRect); end; txtRect.Left := txtRect.Left + 1; txtRect.Top := txtRect.Top + 1; txtRect.Right := txtRect.Right - 1; txtRect.Bottom := txtRect.Bottom - 1; ExtralabelRect := txtRect; fs := TntTreeView1.Canvas.Font.size; fc := TntTreeView1.Canvas.Font.Color; if (nData <> nil) And (nData.ExtraNodeLabel <> '') then begin TntTreeView1.Canvas.Font.Size := 7; TntTreeView1.Canvas.Font.color := clBlue; DrawTextW( DC, PWideChar(nData.ExtraNodeLabel), Length(nData.ExtraNodeLabel), ExtraLabelRect, DT_LEFT or DT_CALCRECT or DT_VCENTER ); DrawTextW( DC, PWideChar(nData.ExtraNodeLabel), Length(nData.ExtraNodeLabel), ExtraLabelRect, DT_LEFT or DT_VCENTER ); txtRect.right := txtRect.Right + ExtraLabelRect.Right + 5; txtRect.Left := ExtraLabelRect.Right + 5; end; TntTreeView1.Canvas.Font.Size := fs; TntTreeView1.Canvas.Font.color := fc; DrawTextW( DC, PWideChar((Node as TTntTreeNode).Text), -1, txtRect, DT_LEFT or DT_VCENTER ); end; end;
delphi delphi-5 treeview
beerwin
source share