Solved

Panel component which resizes children

Posted on 1998-11-25
6
737 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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Have you ever had your Delphi form/application just hanging while waiting for data to load? This is the article to read if you want to learn some things about adding threads for data loading in the background. First, I'll setup a general applica…
Introduction I have seen many questions in this Delphi topic area where queries in threads are needed or suggested. I know bumped into a similar need. This article will address some of the concepts when dealing with a multithreaded delphi database…
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…

762 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now