Solved

Panel component which resizes children

Posted on 1998-11-25
6
753 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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 Webinar] The Cloud Skills Gap

As Cloud technologies come of age, business leaders grapple with the impact it has on their team's skills and the gap associated with the use of a cloud platform.

Join experts from 451 Research and Concerto Cloud Services on July 27th where we will examine fact and fiction.

Question has a verified solution.

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

The uses clause is one of those things that just tends to grow and grow. Most of the time this is in the main form, as it's from this form that all others are called. If you have a big application (including many forms), the uses clause in the in…
In my programming career I have only very rarely run into situations where operator overloading would be of any use in my work.  Normally those situations involved math with either overly large numbers (hundreds of thousands of digits or accuracy re…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…
This tutorial will teach you the special effect of super speed similar to the fictional character Wally West aka "The Flash" After Shake : http://www.videocopilot.net/presets/after_shake/ All lightning effects with instructions : http://www.mediaf…
Suggested Courses
Course of the Month6 days, 23 hours left to enroll

623 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