Solved

Panel component which resizes children

Posted on 1998-11-25
6
740 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Simple Delphi Question 9 83
Convert GUI app into console app for Win32 Env 5 82
TEMBEDDEDWB how can i change its user agent ? 8 50
Firemonkey webbrowser scrollbars ? 1 29
Hello everybody This Article will show you how to validate number with TEdit control, What's the TEdit control? TEdit is a standard Windows edit control on a form, it allows to user to write, read and copy/paste single line of text. Usua…
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…
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

864 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

23 Experts available now in Live!

Get 1:1 Help Now