RAD Studio VCL Reference
ContentsIndex
PreviousUpNext
TControl.BoundsRect Property

Specifies the bounding rectangle of the control, expressed in the coordinate system of the parent control.

Pascal
property BoundsRect: TRect;
C++
__property TRect BoundsRect;

Use BoundsRect as a quick way to obtain the pixel locations of all corners of the control all at once. 

For example, the statement

R := Control.BoundsRect;

 

R = Control->BoundsRect;

corresponds to

R.Left := Control.Left;
R.Top := Control.Top;
R.Right := Control.Left + Control.Width;
R.Bottom := Control.Top + Control.Height;

 

R.Left = Control->Left;
R.Top = Control->Top;
R.Right = Control->Left + Control->Width;
R.Bottom = Control->Top + Control->Height;

The origin of the pixel coordinate system is in the top left corner of the parent window.

Note: A point is considered within the control's bounds rectangle if it lies on the left or top side but not if it lies on the right or bottom side. That is, to be inside the bounds rectangle, the x-coordinate must be greater than or equal to BoundsRect.Left and less than BoundsRect.Right, and the y-coordinate must be greater than or equal to BoundsRect.Top and less than BoundsRect.Bottom.
 

C++ Examples: 

 

/*
This code resizes the active control to twice as wide and
half as high:
*/
void __fastcall TForm1::ButtonTSClick(TObject *Sender)
{
  TRect MyRect;
  if (myActiveControl == NULL) exit;
  MyRect = myActiveControl->BoundsRect;
  MyRect.Right = MyRect.Left + (MyRect.Right - MyRect.Left) / 2;
  MyRect.Bottom = MyRect.Top + 2 * (MyRect.Bottom - MyRect.Top);
  myActiveControl->BoundsRect = MyRect;
}
void __fastcall TForm1::ButtonSFClick(TObject *Sender)
{
  TRect MyRect;
  if (myActiveControl == NULL) exit;
  MyRect = myActiveControl->BoundsRect;
  MyRect.Right = MyRect.Left + 2 * (MyRect.Right - MyRect.Left);
  MyRect.Bottom = MyRect.Top + (MyRect.Bottom - MyRect.Top) / 2;
  myActiveControl->BoundsRect = MyRect;
}
void __fastcall TForm1::FormCreate(TObject *Sender)
{
  TComponent *Temp;
  TListItem *ListItem;
  ListView1->ViewStyle = vsList;
  ListItem = ListView1->Items->Add();
  ListItem->Caption = "Components: ";
  for (int I = ComponentCount - 1; I >= 0; I--)
  {
    Temp = Components[I];
    ListItem = ListView1->Items->Add();
    ListItem->Caption = Temp->Name;
  }
}
void __fastcall TForm1::ListView1SelectItem(TObject *Sender, TListItem *Item,
      bool Selected)
{
  TComponent *comp = FindComponent(Item->Caption);
  if (dynamic_cast<TWinControl *>(comp) != NULL)
    myActiveControl = (TWinControl *)comp;
}

 

Delphi Examples: 

{
This code resizes the active control to twice as wide and
half as high:
}
procedure TForm1.ButtonSFClick(Sender: TObject);
var
  MyRect: TRect;
begin
  if (myActiveControl = nil) then exit;
  MyRect := myActiveControl.BoundsRect;
  MyRect.Right := MyRect.Left + (MyRect.Right - MyRect.Left) * 2;
  MyRect.Bottom := MyRect.Top + (MyRect.Bottom - MyRect.Top) div 2;
  myActiveControl.BoundsRect := MyRect;
end;

procedure TForm1.ButtonTSClick(Sender: TObject);
var
  MyRect: TRect;
begin
  if (myActiveControl = nil) then exit;
  MyRect := myActiveControl.BoundsRect;
  MyRect.Right := MyRect.Left + (MyRect.Right - MyRect.Left) div 2;
  MyRect.Bottom := MyRect.Top + (MyRect.Bottom - MyRect.Top) * 2;
  myActiveControl.BoundsRect := MyRect;
end;

procedure TForm1.FormCreate(Sender: TObject);
var
  I: Integer;
  Temp: TComponent;
  ListItem: TListItem;
begin
  ListView1.ViewStyle := vsList;
  ListItem := ListView1.Items.Add;
  ListItem.Caption := 'Components: ';
  for I := ComponentCount - 1 downto 0 do
  begin
    Temp := Components[I];
    begin
      ListItem := ListView1.Items.Add;
      ListItem.Caption := Temp.Name;
    end;
  end;
end;

procedure TForm1.ListView1SelectItem(Sender: TObject; Item: TListItem;
  Selected: Boolean);
var comp: TComponent;
begin
  comp := FindComponent(Item.Caption);
  if comp is TWinControl then
    myActiveControl := TWinControl(comp);
end;

 

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