Andy k
Andy k

Reputation: 1116

How to tell when an application has finished updating all the visual controls of a Delphi application on startup

My system is windows 10 and Delphi 10.4.

I have several applications that work fine, they just look very ugly on startup.

The main issue is that I save the main window state when the application shuts down, such as window state, size, position etc and then restore them at start up.

I restore the main window parameters in the mainform oncreate event from an ini file and in the onactivate event, I load the program dynamic content from a project file.

Loading the project file may take 10 seconds or so, but in the meantime, the window has scaled to its final size (ie maximised) but all the main form visual components are still in their design time sizes.

It all sorts itself out once the project file has loaded, it just looks so ugly during that startup process.

I've tried all sorts of combinations of application.processmessages, update, refresh etc before loading the project file but nothing works in the onactivate event.

Weirdly, I tried to printscreen so I could post a picture of the effect but the resultant picture looked fine, so I am guessing it something held up in the graphics pipeline during application startup.

Is there an event that fires after the mainwindow has finished all its processing, so I can then start the dynamic content loading?

Upvotes: 1

Views: 212

Answers (1)

HeartWare
HeartWare

Reputation: 8261

Here is an example of the code I describe in the comment:

unit Unit83;

interface

uses
  Winapi.Windows, Winapi.Messages, System.SysUtils, System.Variants, System.Classes, Vcl.Graphics,
  Vcl.Controls, Vcl.Forms, Vcl.Dialogs;

CONST
  UM_LOADPROJECT = WM_USER+10;

type
  TForm83 = class(TForm)
    procedure FormActivate(Sender: TObject);
  private
    { Private declarations }
    procedure LoadProject(var Msg : TMsg); MESSAGE UM_LOADPROJECT;
    procedure LoadProjectFile;
  public
    { Public declarations }
  end;

var
  Form83: TForm83;

implementation

{$R *.dfm}

procedure TForm83.FormActivate(Sender: TObject);
begin
  OnActivate:=NIL;
  PostMessage(Handle,UM_LOADPROJECT,10,0)
end;

procedure TForm83.LoadProject(var Msg: TMsg);
begin
  IF Msg.wParam>0 THEN
    PostMessage(Handle,Msg.message,PRED(Msg.wParam),Msg.lParam)
  ELSE
    LoadProjectFile
end;

procedure TForm83.LoadProjectFile;
begin
  // Do the project file load
end;

end.

The "10" in the FormActivate's PostMessage is the number of times the message is put to the back of the queue. Start with the value 0 to test if it is necessary.

Upvotes: 3

Related Questions