kol
kol

Reputation: 28678

Resizing Delphi form after resizing its font in form editor

I develop a legacy Delphi 6 application, and I would like to increase the font size of one of the forms. All other forms have Microsoft Sans Serif 8pt, but this one is set to Microsoft Sans Serif 7pt. All controls have ParentFont = True, so I could simply set the font size of the form to 8pt. The problem is that the form and its controls won't resize, and the text of the labels would overlap. Is there a simple way to resize the form after resizing its font, without resizing its controls manually in the form editor?

Upvotes: 4

Views: 1986

Answers (1)

David Heffernan
David Heffernan

Reputation: 612794

At designtime you can effect the change by editing the .dfm file manually. Make sure that the form is saved with the Scaled property set to True.

Then, close your project in Delphi, or close Delphi altogether. Next open the .dfm file in a text editor and adjust the forms TextHeight property. For example, if you want to scale from 7pt to 8pt, and TextHeight is set to 13, then you should change it to 11. Then re-load the project and open the form in the designer and your form will be scaled. This won't be a perfect scaling because you aren't allowed floating point values for TextHeight. But it may be good enough.


At runtime, you can to call ChangeScale:

ChangeScale(NewFont.Size, OldFont.Size);

Note that ChangeScale is a protected member. So, depending on where you are calling this, you may need to use the protected member hack.

One option then would be to call the form persistence framework at runtime to generate a scaled version of the .dfm file. That would allow you more precise control than playing tricks with TextHeight

For example, you can attach the following to the OnShow event of your form:

procedure TMyForm.FormShow(Sender: TObject);
var
  BinaryStream, TextStream: TStream;
begin
  BinaryStream := TMemoryStream.Create;
  Try
    BinaryStream.WriteComponent(Self);
    BinaryStream.Position := 0;
    TextStream := TFileStream.Create('MyForm.dfm', fmCreate);
    Try
      ObjectBinaryToText(BinaryStream, TextStream);
    Finally
      TextStream.Free;
    End;
  Finally
    BinaryStream.Free;
  End;
end;

This will generate a new .dfm file based on the runtime state. You can then compare this with the version of the .dfm file that is in your revision control system. There will be a few changes that you won't want to accept, but mostly the changes will be the position and size changes that you do want.

Upvotes: 5

Related Questions