How to resize a TPanel at runtime #93

You should add a WS_SIZEBOX constant to the your panel window style:

type
  TMyNewPanel = class(TPanel)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;
...
procedure TMyNewPanel.CreateParams(var Params: TCreateParams);
begin
  inherited CreateParams(Params);
  Params.Style := Params.Style or WS_SIZEBOX;
end;

Demo code

A ready made project containing this demo code is available. View the project.

In this demo we dynamically create a TMyNewPanel on a form so that you can experiment with resizing it by dragging its borders.

Start a new Delphi VCL application and create an OnCreate event handler for the form. Name the form "Form1" and save it as Unit1.pas. Now code the unit as follows.

unit Unit1;

interface

uses
  ExtCtrls, Controls, Forms;

type

  TMyNewPanel = class(TPanel)
  protected
    procedure CreateParams(var Params: TCreateParams); override;
  end;

  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  private
    fPnl: TMyNewPanel;
  end;

var
  Form1: TForm1;

implementation

uses
  Windows;

{$R *.dfm}

{ TMyNewPanel }

procedure TMyNewPanel.CreateParams(var Params: TCreateParams);
begin
  inherited;
  Params.Style := Params.Style or WS_SIZEBOX;
end;

{ TForm1 }

procedure TForm1.FormCreate(Sender: TObject);
begin
  fPnl := TMyNewPanel.Create(Self);
  fPnl.Parent  := Self;
  fPnl.Top := 20;
  fPnl.Left := 20;
  fPnl.Caption := fPnl.ClassName;
end;

end.

Run the demo and size the panel by dragging on the borders that are added to its edges.

Demo by Peter Johnson

See Tip #92 for a different approach to making a resizeable panel that uses a size grip and hit testing.

Original resource: The Delphi Pool
Author: Serge Gubenko
Added: 2009/09/07
Last updated: 2010/03/16