TVCustomDrawItem (C++)

From RAD Studio Code Examples
Jump to: navigation, search

Description

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.

Code

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->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 rectangle.
	  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
        //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);
	};
}

Uses