How to reset a delphi TForm to original appearance after use of ScaleBy()
Asked Answered
B

4

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.

Bezanson answered 5/1, 2012 at 11:25 Comment(2)
You want the user to be able to scale the form on the fly, repeatedly? Personally I'd just recreate.Culvert
@David: I just want them to be able to drag a slider (say) until it looks right then leave it, so there is no performance implication.Bezanson
C
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.
Corrugate answered 5/1, 2012 at 15:36 Comment(1)
Easysize works a treat and unless and until a native solution appears I'll accept your answer. Thanks.Bezanson
M
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.

Maxwellmaxy answered 5/1, 2012 at 15:14 Comment(1)
Does this really require storing a copy of them at the start? Why is there no way of redrawing them but at default size?Bezanson
C
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;
Coatbridge answered 5/1, 2012 at 17:2 Comment(0)
M
1

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.

Motley answered 20/9, 2022 at 6:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.