8

I want to allow a form to be sized by the user with its controls proportionaly resized. ScaleBy( N,M ) works fine for me but there are of course cumulative rounding errors if it is repeated. To counter this I simply wish to precede ScaleBy() with a call to recreate the form in its default appearance and size and then call ScaleBy with various values. I know I can do this by hosting my form within a panel (and disposing / recreating it) but is there a call that will reset the form after use of ScaleBy()?

Edit - I am using Delphi XE2 and would also be interested in anyone's success with a component or other code (paid or free) to scale a form neatly - my own downloads have not produced a working solution.

RRUZ
  • 134,889
  • 20
  • 356
  • 483
Brian Frost
  • 13,334
  • 11
  • 80
  • 154

4 Answers4

7

Try EasySize (TFormResizer) component.
The TFormResizer component resizes all of the controls on a form (or panel) when the form size changes.
I used it successfully years ago - works with D5/7. You might need to make small adjustments for XE2 (I do not have XE2, so I cant test it).

Usage:

uses
  ..., Easysize;

type
  TForm1 = class(TForm)
    ...        
    procedure FormCreate(Sender: TObject);
    procedure FormResize(Sender: TObject);
  private
    FR: TFormResizer;
  end;

...

procedure TForm1.FormCreate(Sender: TObject);
begin
  FR := TFormResizer.Create(Self);
  FR.ResizeFonts := True;
  FR.InitializeForm;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  FR.ResizeAll;
end;

end.
kobik
  • 21,001
  • 4
  • 61
  • 121
2

One solution would be to use the Components property of the form interate over all the child controls of a form and reset them back to their original value.

The following article has example code: http://delphi.about.com/od/adptips2005/qt/storecontrolpos.htm

This is for a slightly different purpose, but it shouldn't be to hard to modify the code to your needs.

Johan
  • 74,508
  • 24
  • 191
  • 319
  • Does this really require storing a copy of them at the start? Why is there no way of redrawing them but at default size? – Brian Frost Jan 05 '12 at 16:29
1

First, adjust the scale to the original scale, then scale to new scale. For example, to scale a form in a OnResize event:

...
  private
    FDesignHeight: Integer;
    FDesignWidth: Integer;
    FPrevWidth: Integer;
  end;

...

procedure TForm1.FormShow(Sender: TObject);
begin
  FDesignHeight := Height;
  FDesignWidth := Width;
  FPrevWidth := Width;
  Scaled := True;
end;

procedure TForm1.FormResize(Sender: TObject);
begin
  if Scaled then
  begin
    DisableAlign;
    ScaleBy(FDesignWidth, FPrevWidth);
    ScaleBy(Width, FDesignWidth);
    EnableAlign;
  end;
  FPrevWidth := Width;
end;

procedure TForm1.FormCanResize(Sender: TObject; var NewWidth,
  NewHeight: Integer; var Resize: Boolean);
begin
  NewHeight := Round(NewWidth * FDesignHeight / FDesignWidth);
end;
NGLN
  • 43,011
  • 8
  • 105
  • 200
0

This works fine for me. To solve rounding problems, I start with a base value of 100000; Using a TSpinEdit with default value of '100' and steps of 10(%):

OnShow: OldScaleValue := 100000;

procedure TLogForm.SpinEdit1Change(Sender: TObject);
begin
  DisableAlign;
  try
    Log('Org Width='+Width.ToString);
    Scaleby(100000, OldScaleValue);
    OldScaleValue := SpinEdit1.Value*1000;
    Scaleby(OldScaleValue, 100000);
    Log('NEW Width='+Width.ToString);
  finally
    EnableAlign;
  end;
end;

This steps forward and backward with 10% increase / decrease without rounding issues.

Bart Kindt
  • 142
  • 13