RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TTreeView.OnCustomDrawItem Event

Occurs immediately prior to painting a node in a tree view control.

Pascal
property OnCustomDrawItem: TTVCustomDrawItemEvent;
C++
__property TTVCustomDrawItemEvent OnCustomDrawItem;

Write an OnCustomDrawItem event handler to paint individual items in the tree view, or to provide a background to the item before the default rendering of the item.

Note: OnCustomDrawItem only occurs prior to painting individual tree items. To customize the painting of items at other stages of the paint process (such as after the item is painted), use OnAdvancedCustomDrawItem instead.
 

C++ Examples: 

 

/*
The following example shows how the OnCustomDrawItem event
handler draws items and lines of the tree view after the 
OnCustomDraw event handler has filled in the background.
*/ 
void __fastcall TCustomDrawForm::TVCustomDrawItem(TCustomTreeView *Sender,
      TTreeNode *Node, TCustomDrawState State, bool &DefaultDraw)
{
  TRect NodeRect;
/*
    If DefaultDraw it is true, any of the node's font
    properties can be changed. Note also that when 
    DefaultDraw = True, Windows draws the buttons and 
    ignores our font background colors, using instead the
    TreeView's Color property.
*/
    if (State.Contains(cdsSelected))
    {
      TV->Canvas->Font->Assign(SelectedFontDialog->Font);
      TV->Canvas->Brush->Color = SelBkgColorDialog->Color;
    };

    DefaultDraw = FDefaultDrawItem;
/*
    DefaultDraw = False means you have to handle all the
    item drawing yourself, including the buttons, lines,
    images, and text.
*/
    if (!DefaultDraw)
    {
      //draw the selection rect.
      if (State.Contains(cdsSelected))
      {
        NodeRect = Node->DisplayRect(True);
        TV->Canvas->FillRect(NodeRect);
      };
      NodeRect = Node->DisplayRect(False);

      if (None1->Checked)
      //no bitmap, so paint in the background color.
      {
        TV->Canvas->Brush->Color = BkgColorDialog->Color;
        TV->Canvas->Brush->Style = FBrushStyle;
        TV->Canvas->FillRect(NodeRect);
      }
      else
        //don't paint over the background bitmap.
        TV->Canvas->Brush->Style = bsClear;

      NodeRect.Left = NodeRect.Left + (Node->Level * TV->Indent);
      // NodeRect.Left now represents the left-most portion 
      // of the expand button
      DrawButton(&NodeRect, Node); // See the CustomDraw demo

      NodeRect.Left = NodeRect.Left + TV->Indent + FButtonSize;
      //NodeRect->Left is now the leftmost portion of the image.
      DrawImage(&NodeRect, Node->ImageIndex); // See the CustomDraw demo

      NodeRect.Left = NodeRect.Left + ImageList->Width;
      //Now we are finally in a position to draw the text.

      TV->Canvas->TextOut(NodeRect.Left, NodeRect.Top, Node->Text);
    };
}

 

Delphi Examples: 

{
The following example shows how the OnCustomDrawItem event 
handler draws items and lines of the tree view after the 
OnCustomDraw event handler has filled in the background.
} 
procedure TCustomDrawForm.TVCustomDrawItem(Sender: TCustomTreeView; Node: TTreeNode;
  State: TCustomDrawState; var DefaultDraw: Boolean);
var
  NodeRect: TRect;
begin
  with TV.Canvas do
  begin
{
    If DefaultDraw it is true, any of the node's font 
    properties can be changed. Note also that when 
    DefaultDraw = True, Windows draws the buttons and 
    ignores our font background colors, using instead the
    TreeView's Color property.
}
    if cdsSelected in State then
    begin
      Font.Assign(SelectedFontDialog.Font);
      Brush.Color := SelBkgColorDialog.Color;
    end;

    DefaultDraw := False; // FDefaultDrawItem;
{
    DefaultDraw = False means you have to handle all the
    item drawing yourself, including the buttons, lines,
    images, and text.
}
    if not DefaultDraw then
    begin
      //draw the selection rect.
      if cdsSelected in State then
      begin
        NodeRect := Node.DisplayRect(True);
        FillRect(NodeRect);
      end;
      NodeRect := Node.DisplayRect(False);

      if None1.Checked then
      //no bitmap, so paint in the background color.
      begin
        Brush.Color := BkgColorDialog.Color;
        Brush.Style := FBrushStyle;
        FillRect(NodeRect)
      end
      else
        //don't paint over the background bitmap.
        Brush.Style := bsClear;

      NodeRect.Left := NodeRect.Left + (Node.Level * TV.Indent);
      // NodeRect.Left now represents the left-most portion 
      // of the expand button
      DrawButton(NodeRect, Node); // See the CustomDraw demo

      NodeRect.Left := NodeRect.Left + TV.Indent + FButtonSize;
      //NodeRect.Left is now the leftmost portion of the image.
      DrawImage(NodeRect, Node.ImageIndex); // See the CustomDraw demo

      NodeRect.Left := NodeRect.Left + ImageList.Width;
      //Now we are finally in a position to draw the text.

      TextOut(NodeRect.Left, NodeRect.Top, Node.Text);
    end;
  end;
end;

 

Copyright(C) 2008 CodeGear(TM). All Rights Reserved.
What do you think about this topic? Send feedback!