TVCustomDrawItem (C++)

From RAD Studio Code Examples
Jump to: navigation, search


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 is True, any of the node's font
    properties can be changed. Note also that, when 
    DefaultDraw = True, Windows draws the buttons and 
    ignores your font background colors, using instead 
	the TreeView's Color property.
	if (State.Contains(cdsSelected))
	  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 rectangle.
	  if (State.Contains(cdsSelected))
		NodeRect = Node->DisplayRect(True);
	  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;
        //Do not paint over the background bitmap.
		TV->Canvas->Brush->Style = bsClear;

	  NodeRect.Left = NodeRect.Left + (Node->Level * TV->Indent);
      // NodeRect.Left now represents the leftmost 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 you are finally in a position to draw the text.

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