RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TCustomTreeView.Canvas Property

Provides access to the canvas.

Pascal
property Canvas: TCanvas;
C++
__property TCanvas Canvas;

Use the Canvas property to paint to the canvas from the OnCustomDraw and OnCustomDrawItem event handlers.  

C++ Examples: 

 

/*
The following code uses ClientRect to find and draw a line
from the top left to the bottom right of the current
control.  Select a TControl in the ListBox list.  This
example assumes that the current object is a descendent of
TControl and has a public Canvas property.
*/
{
  TPoint APoint = Point(X, Y);
  int Index = ListBox1->ItemAtPos(APoint, True);
  TTreeView *myTreeView;
  TClass ClassRef = ListBox1->Items->Objects[Index]->ClassType();
  if (String(ClassRef->ClassName()) == "TTreeView")
  {
    myTreeView = dynamic_cast<TTreeView *>(ListBox1->Items->Objects[Index]);
    // make sure we are using the TControls canvas and not the from's!
    myTreeView->Canvas->MoveTo(myTreeView->ClientRect.Left, myTreeView->ClientRect.Top);
    myTreeView->Canvas->LineTo(myTreeView->ClientRect.Right, myTreeView->ClientRect.Bottom);
    myTreeView = NULL;
  }
}
/*
The following example is taken from the custom draw demo. It
shows how the OnCustomDraw event handler draws the
background for the tree view before the items and lines are
drawn.
*/
void __fastcall TCustomDrawForm::TVCustomDraw(TCustomTreeView *Sender,
      const TRect &ARect, bool &DefaultDraw)
{
/*
This event should be used to draw any background colors or 
images.  ARect represents the entire client area of the TreeView.
Use the TreeView's canvas to do the drawing.
Note that drawing a background bitmap is not really 
supported by CustomDraw, so scrolling can get messy. Best to
subclass the TreeView and handle scrolling messages.
*/
  if (None1 != NULL)
  {
    if (None1->Checked) //no picture
    {
      TV->Canvas->Brush->Color = BkgColorDialog->Color;
      TV->Canvas->Brush->Style = FBrushStyle;
      TV->Canvas->FillRect(ARect);
    }
    else if (Tile1->Checked) //tile bitmap
    {
      TV->Canvas->Brush->Bitmap = Image1->Picture->Bitmap;
      TV->Canvas->FillRect(ARect);
    }
    else //Stretch across the canvas.
      TV->Canvas->StretchDraw(ARect, Image1->Picture->Bitmap);
  };
  DefaultDraw = FDefaultDraw;
  // Setting DefaultDraw to false here prevents all calls to
  // OnCustomDrawItem.
}

 

Delphi Examples: 

{
The following code uses ClientRect to find and draw a line
from the top left to the bottom right of the current
control.  Select a TControl in the ListBox list.  This
example assumes that the current object is a descendent of
TControl and has a public Canvas property.
}
  if (ListBox1.Items.Objects[Index] is TTreeView) then
  begin
    myTreeView:= (ListBox1.Items.Objects[Index] as TTreeView);
    with myTreeView.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myTreeView.Canvas.MoveTo(Left, Top);
      myTreeView.Canvas.LineTo(Right, Bottom);
    end;
  myTreeView:= nil;
  end;
{
The following example is taken from the custom draw demo. It
shows how the OnCustomDraw event handler draws the
background for the tree view before the items and lines are
drawn.
}
procedure TCustomDrawForm.TVCustomDraw(Sender: TCustomTreeView; const ARect: TRect;
  var DefaultDraw: Boolean);
begin
{
This event should be used to draw any background colors or 
images.  ARect represents the entire client area of the TreeView.
Use the TreeView's canvas to do the drawing.
Note that drawing a background bitmap is not really 
supported by CustomDraw, so scrolling can get messy. Best to
subclass the TreeView and handle scrolling messages.
}
  with TV.Canvas do
  begin
    if None1 <> nil then
    begin
      if None1.Checked then //no picture
      begin
        Brush.Color := BkgColorDialog.Color;
        Brush.Style := FBrushStyle;
        FillRect(ARect);
      end else
        if Tile1.Checked then //tile bitmap
        begin
          Brush.Bitmap := Image1.Picture.Bitmap;
            FillRect(ARect);
          end else //Stretch across the canvas.
              StretchDraw(ARect, Image1.Picture.Bitmap);
    end;
  end;
  DefaultDraw := FDefaultDraw;
  // Setting DefaultDraw to false here prevents all calls to
  // OnCustomDrawItem.
end;

 

Copyright(C) 2009 Embarcadero Technologies, Inc. All Rights Reserved.
What do you think about this topic? Send feedback!