unit TControlCanvas;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, ComCtrls, ToolWin, ExtCtrls, Buttons, StdCtrls, DBCtrls, Grids;

type
  TForm1 = class(TForm)
    DBText1: TDBText;
    Label1: TLabel;
    Image1: TImage;
    PaintBox1: TPaintBox;
    Splitter1: TSplitter;
    ToolBar1: TToolBar;
    ListBox1: TListBox;
    StringGrid1: TStringGrid;
    StatusBar1: TStatusBar;
    ComboBox1: TComboBox;
    ControlBar1: TControlBar;
    TreeView1: TTreeView;
    ListView1: TListView;
    PageControl1: TPageControl;
    procedure FormCreate(Sender: TObject);
    procedure ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
      Shift: TShiftState; X, Y: Integer);
    procedure PaintBox1Paint(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
begin
  for I := 0 to Form1.ComponentCount-1 do
    ListBox1.Items.InsertObject(
      0, Form1.Components[I].Name,
      Form1.Components[I] as TObject);
  ListBox1.Items.InsertObject(
      0, Form1.Name, Form1 as TObject);

end;

procedure TForm1.ListBox1MouseDown(Sender: TObject; Button: TMouseButton;
  Shift: TShiftState; X, Y: Integer);
var
  APoint: TPoint;
  Index: integer;
  myToolBar: TToolBar;
  myStatusBar: TStatusBar;
  myDBText: TDBText;
  myLabel: TLabel;
  myImage: TImage;
  myPaintBox: TPaintBox;
  mySplitter: TSplitter;
  myListBox: TListBox;
  myStringGrid: TStringGrid;
  myControlBar: TControlBar;
  myComboBox: TComboBox;
  myTreeView: TTreeView;
  myListView: TListView;
  myForm: TForm1;
  myPageControl: TPageControl;
begin
  APoint.X := X;
  APoint.Y := Y;
  Index := ListBox1.ItemAtPos(APoint, True);
  if (Index < 0) then exit;
// BeginExample: TToolBarCanvas
// Property: ComCtrls.TToolBar.Canvas
// Property: Controls.TControl.ClientRect

// BeginCode
{
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 TToolBar) then
  begin
    myToolBar:= (ListBox1.Items.Objects[Index] as TToolBar);
    with myToolBar.ClientRect do
    begin // make sure we are using the TControls canvas and not the from's!
      myToolBar.Canvas.MoveTo(Left, Top);
      myToolBar.Canvas.LineTo(Right, Bottom);
    end;
  myToolBar:= nil;
  end;
// EndCode
// EndExample: TToolBarCanvas
// BeginExample: TStatusBarCanvas
// Property: ComCtrls.TCustomStatusBar.Canvas

// BeginCode
{
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 TStatusBar) then
  begin
    myStatusBar:= (ListBox1.Items.Objects[Index] as TStatusBar);
    with myStatusBar.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myStatusBar.Canvas.MoveTo(Left, Top);
      myStatusBar.Canvas.LineTo(Right, Bottom);
    end;
  myStatusBar:= nil;
  end;
// EndCode
// EndExample: TStatusBarCanvas
  if (ListBox1.Items.Objects[Index] is TDBText) then
  begin
    myDBText:= (ListBox1.Items.Objects[Index] as TDBText);
    with myDBText.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myDBText.Canvas.MoveTo(Left, Top);
      myDBText.Canvas.LineTo(Right, Bottom);
    end;
  myDBText:= nil;
  end;
// BeginExample: TLabelCanvas
// Property: Controls.TGraphicControl.Canvas

// BeginCode
{
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 TLabel) then
  begin
    myLabel:= (ListBox1.Items.Objects[Index] as TLabel);
    with myLabel.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myLabel.Canvas.MoveTo(Left, Top);
      myLabel.Canvas.LineTo(Right, Bottom);
    end;
  myLabel:= nil;
  end;
// EndCode
// EndExample: TLabelCanvas
// BeginExample: TImageCanvas
// Property: ExtCtrls.TImage.Canvas

// BeginCode
{
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 TImage) then
  begin
    myImage:= (ListBox1.Items.Objects[Index] as TImage);
    with myImage.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myImage.Canvas.MoveTo(Left, Top);
      myImage.Canvas.LineTo(Right, Bottom);
    end;
  myImage:= nil;
  end;
// EndCode
// EndExample: TImageCanvas
  if (ListBox1.Items.Objects[Index] is TPaintBox) then
  begin
    myPaintBox:= (ListBox1.Items.Objects[Index] as TPaintBox);
    with myPaintBox.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myPaintBox.Canvas.MoveTo(Left, Top);
      myPaintBox.Canvas.LineTo(Right, Bottom);
    end;
  myPaintBox:= nil;
  end;
  if (ListBox1.Items.Objects[Index] is TSplitter) then
  begin
    mySplitter:= (ListBox1.Items.Objects[Index] as TSplitter);
    with mySplitter.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      mySplitter.Canvas.MoveTo(Left, Top);
      mySplitter.Canvas.LineTo(Right, Bottom);
    end;
  mySplitter:= nil;
  end;
// BeginExample: TListBoxCanvas
// Property: StdCtrls.TCustomListBox.Canvas

// BeginCode
{
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 TListBox) then
  begin
    myListBox:= (ListBox1.Items.Objects[Index] as TListBox);
    with myListBox.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myListBox.Canvas.MoveTo(Left, Top);
      myListBox.Canvas.LineTo(Right, Bottom);
    end;
  myListBox:= nil;
  end;
// EndCode
// EndExample: TListBoxCanvas
// BeginExample: TStringGridCanvas
// Property: Controls.TCustomControl.Canvas

// BeginCode
{
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 TStringGrid) then
  begin
    myStringGrid:= (ListBox1.Items.Objects[Index] as TStringGrid);
    with myStringGrid.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myStringGrid.Canvas.MoveTo(Left, Top);
      myStringGrid.Canvas.LineTo(Right, Bottom);
    end;
  myStringGrid:= nil;
  end;
// EndCode
// EndExample: TStringGridCanvas
  if (ListBox1.Items.Objects[Index] is TControlBar) then
  begin
    myControlBar:= (ListBox1.Items.Objects[Index] as TControlBar);
    with myControlBar.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myControlBar.Canvas.MoveTo(Left, Top);
      myControlBar.Canvas.LineTo(Right, Bottom);
    end;
  myControlBar:= nil;
  end;
// BeginExample: TComboBoxCanvas
// Property: StdCtrls.TCustomCombo.Canvas

// BeginCode
{
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 TComboBox) then
  begin
    myComboBox:= (ListBox1.Items.Objects[Index] as TComboBox);
    with myComboBox.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myComboBox.Canvas.MoveTo(Left, Top);
      myComboBox.Canvas.LineTo(Right, Bottom);
    end;
  myComboBox:= nil;
  end;
// EndCode
// EndExample: TComboBoxCanvas
// BeginExample: TTreeViewCanvas
// Property: ComCtrls.TCustomTreeView.Canvas

// BeginCode
{
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;
// EndCode
// EndExample: TTreeViewCanvas
// BeginExample: TListViewCanvas
// Property: ComCtrls.TCustomListView.Canvas

// BeginCode
{
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 TListView) then
  begin
    myListView:= (ListBox1.Items.Objects[Index] as TListView);
    with myListView.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myListView.Canvas.MoveTo(Left, Top);
      myListView.Canvas.LineTo(Right, Bottom);
    end;
  myListView:= nil;
  end;
// EndCode
// EndExample: TListViewCanvas
// BeginExample: TFormCanvas
// Property: Forms.TCustomForm.Canvas

// BeginCode
{
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 TForm1) then
  begin
    myForm:= (ListBox1.Items.Objects[Index] as TForm1);
    with myForm.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myForm.Canvas.MoveTo(Left, Top);
      myForm.Canvas.LineTo(Right, Bottom);
    end;
  myForm:= nil;
  end;
// EndCode
// EndExample: TFormCanvas
// BeginExample: TPageControlCanvas
// Property: ComCtrls.TCustomTabControl.Canvas

// BeginCode
{
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 TPageControl) then
  begin
    myPageControl:= (ListBox1.Items.Objects[Index] as TPageControl);
    with myPageControl.ClientRect do
    begin // make sure we are using the TControl's canvas and not the form's!
      myPageControl.Canvas.MoveTo(Left, Top);
      myPageControl.Canvas.LineTo(Right, Bottom);
    end;
  myPageControl:= nil;
  end;
// EndCode
// EndExample: TPageControlCanvas
end;

procedure TForm1.PaintBox1Paint(Sender: TObject);
begin
  PaintBox1.Canvas.Draw(0,0, Image1.Picture.Graphic);
end;
end.
