ravellin
ravellin

Reputation: 26

Form positioning when using TStyleManager in Delphi XE7 application

I'm trying to make a Delphi application form remember it's position and recall it the next time the application is started:

procedure TForm1.LoadSettings;
var
  xml : IXMLDocument;
  node : IXMLNode;
begin
  [ ... initializing XML ...]

  // Loading theme
  if node.HasAttribute('Theme') then TStyleManager.SetStyle(node.Attributes['Theme']);

  // Loading position & size
  if node.HasAttribute('PosTop') then self.Top := StrToInt(node.Attributes['PosTop']);
  if node.HasAttribute('PosLeft') then self.Left := StrToInt(node.Attributes['PosLeft']);
  if node.HasAttribute('PosWidth') then self.Width := StrToInt(node.Attributes['PosWidth']);
  if node.HasAttribute('PosHeight') then self.Height := StrToInt(node.Attributes['PosHeight']);

  [ ... some other stuff ... ]
end;

Everything works fine when the "Theme" is set to default "Windows" theme. With any theme, the form appears at it's default position set up by Windows, close to the upper left display corner, no matter where the form was located before.

Any ideas about what may be the cause and how to fix that?

Thanks in advance!

Upvotes: 0

Views: 560

Answers (1)

Dalija Prasnikar
Dalija Prasnikar

Reputation: 28516

If you want to properly restore form's position you have to set it initial Position property to poDesigned in form designer.

Default Position property is poDefaultPosOnly. That means your form position will be determined automatically by Windows. Depending at which point you trigger your own form positioning during the form creating process and depending on other code you may either successfully change forms position or not.

However, if the Position property is poDesigned all positioning will be left to Delphi code itself and you will be able to properly apply your setting.

Why?

Because if you use any code that will trigger form's window recreation, that code will again try to set your form's position to Windows default one and can interfere with your positioning process.

Calling TStyleManager.SetStyle does exactly that.


Simple test case for above would be creating new VCL project, adding some style to it and put following code in FormCreate event

procedure TForm1.FormCreate(Sender: TObject);
var Styled: boolean;
begin
  Styled := true;

  if Styled then TStyleManager.SetStyle('Silver');

  Top := 200;
  Left := 300;
  Width := 800;
  Height := 600;
end;

If Styled is false setting position works, if it is true it doesn't.

Upvotes: 4

Related Questions