Solved

Panel component which resizes children

Posted on 1998-11-25
6
743 Views
Last Modified: 2010-04-04
This simple descendent of a TPanel component resizes children controls at DESIGN time.  It works OK, except it has a bug where if you close the project, then reopen the project, THEN resize one of the stretchPanels, the children controls of that panel resize themselves wildly.  After that, it works ok.  It also works ok if you drop a 'stretchPanel' component on a form from scratch and drop some controls inside it.
I need to know what is going wrong and why!

Here is the component source:

unit aPanel1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TPanelStretch = class(TPanel)
  private
    { Private declarations }
    nOldWidth, nOldHeight: integer;

  protected
    { Protected declarations }
    procedure Resize; override;
  public
    { Public declarations }
    constructor create(AOwner : TComponent); override;
//    procedure Resize(const Value: TNotifyEvent); override;
  published
    { Published declarations }
//    property OnResize: TNotifyEvent read aaResize write aaResize; override;
//    property OnResize; override;
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TPanelStretch]);
end;

{ TPanel1 }

constructor TPanelStretch.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  if ( csDesigning in ComponentState ) then
    begin
      nOldWidth := Width;
      nOldHeight := Height;
    end
  else
    begin
      nOldWidth := -1;
      nOldHeight := -1;
    end;
end;

procedure TPanelStretch.Resize;
var
  i:     Integer;
  ratio: Double;
  ctl:   TWinControl;
begin
  inherited;
  if ( nOldWidth = -1 ) and ( nOldHeight = -1 ) then
  begin
    nOldWidth := Width;
    nOldHeight := Height;
    exit;
  end;
  ratio := Width / nOldWidth;
  for i := 0 to ControlCount - 1 do
    begin
      ctl := TWinControl( Controls[i] );
      ctl.Left := Round( ctl.Left * ratio );
      ctl.Width := Round( ctl.Width * ratio );
    end;
  ratio := Height / nOldHeight;
  for i := 0 to ControlCount - 1 do
    begin
      ctl := TWinControl( Controls[i] );
      ctl.Top := Round( ctl.Top * ratio );
      ctl.Height := Round( ctl.Height * ratio );
    end;
  nOldWidth := Width;
  nOldHeight := Height;
end;

end.
0
Comment
Question by:abulka
  • 2
  • 2
  • 2
6 Comments
 
LVL 12

Accepted Solution

by:
rwilson032697 earned 200 total points
ID: 1347884
I think the problem is that the TPanelStretch constructor is setting the Old width and height to be the default width and height of TPanel. After this happens the properties of the panel and its children are read in. Then when you resize the old width and height are not the right values.

You need to override the Loaded method (which is called after the properties are read in etc) and set the old widht and height in that method).

Cheers,

Raymond.
0
 
LVL 10

Expert Comment

by:viktornet
ID: 1347885
Do you think it might have to do with the -1 you set for the Width and Height???

Maybe when the project is loaded the OldWidth and Height are set to 0 or something and then when you resize it it resizes wildly :)

Cheers,
Viktor
0
 

Author Comment

by:abulka
ID: 1347886
Sounds good.  Can you give me a template line or two of code for overriding the Loaded method?

Viz. When I overrode the resize method, I was faced with options to override e.g. the resize property and possibly the resize notification events? and I got lost inside the VCL source code trying to find the right thing to override (and and syntax)
0
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 
LVL 10

Expert Comment

by:viktornet
ID: 1347887
procedure Loaded;override;

procedure TPanelStretch.Loaded;
begin
  Inherited;
  OldWidth := Width;
  OldHeight := Height;
end;

Cheers,
Viktor
0
 
LVL 12

Expert Comment

by:rwilson032697
ID: 1347888
Viktor has done it nicely...

Cheers,

Raymond.
0
 

Author Comment

by:abulka
ID: 1347889
Thanks.  Here is the finished component.  Pretty clean and simple.

unit aPanel1;

interface

uses
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  ExtCtrls;

type
  TPanelStretch = class(TPanel)
  private
    { Private declarations }
    nOldWidth, nOldHeight: integer;

  protected
    { Protected declarations }
    procedure Resize; override;
    procedure Loaded; override;
  public
    { Public declarations }
    constructor create(AOwner : TComponent); override;
  published
    { Published declarations }
  end;

procedure Register;

implementation

procedure Register;
begin
  RegisterComponents('Samples', [TPanelStretch]);
end;

{ TPanel1 }

constructor TPanelStretch.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);
  nOldWidth := Width;
  nOldHeight := Height;
end;

procedure TPanelStretch.Loaded;
begin
  inherited;
  nOldWidth := Width;
  nOldHeight := Height;
end;

procedure TPanelStretch.Resize;
var
  i:     Integer;
  ratio: Double;
  ctl:   TWinControl;
begin
  inherited;

  ratio := Width / nOldWidth;
  for i := 0 to ControlCount - 1 do
    begin
      ctl := TWinControl( Controls[i] );
      ctl.Left := Round( ctl.Left * ratio );
      ctl.Width := Round( ctl.Width * ratio );
    end;
  ratio := Height / nOldHeight;
  for i := 0 to ControlCount - 1 do
    begin
      ctl := TWinControl( Controls[i] );
      ctl.Top := Round( ctl.Top * ratio );
      ctl.Height := Round( ctl.Height * ratio );
    end;
  nOldWidth := Width;
  nOldHeight := Height;
end;

end.

0

Featured Post

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article explains how to create forms/units independent of other forms/units object names in a delphi project. Have you ever created a form for user input in a Delphi project and then had the need to have that same form in a other Delphi proj…
Introduction The parallel port is a very commonly known port, it was widely used to connect a printer to the PC, if you look at the back of your computer, for those who don't have newer computers, there will be a port with 25 pins and a small print…
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question