Execute action after form is shown

From Free Pascal wiki
Revision as of 10:28, 9 September 2018 by Chronos (talk | contribs)
Jump to navigationJump to search

Lazarus doesn't support something like form OnShown/OnShowAfter event where custom code can be executed after form was shown. This is useful mainly to do run custom code after main form is shown. It would serve as kind of one time OnApplicationStart event. In would be useful for example for loading of previous opened project/game file just after application start which can take longer time and main form should be already visible at that moment.

There are multiple ways ho to solve this problem.

Methods

OnActivate event

Form OnActivate event is fired every time if form gets focus. Also this event is fired after OnShow event. Then it is possible to use this event to executed code only once after OnShow event. Boolean variable is needed so after form shown code will be executed only once.

type
  TFormMain = class
    ...
  private
    FormActivated: Boolean;
    ...
  end;

...

procedure TFormMain.FormActivated(Sender: TObject);
begin
  if not FormActivated then begin
    FormActivated := True;
    // After show code
  end;
end;

Send custom message

It is possible to define custom message and send it to application message queue so it will be handled as soon as possible.

uses
  ..., LMessages, LCLIntf;

const
  LM_STARTUP = LM_USER; 

procedure TFormMain.FormShow(Sender: TObject);
begin
  // On on show code
  SendMessage(Handle, LM_STARTUP, 0, 0);
end;   

procedure TFormMain.LMStartup(var Msg: TLMessage);
begin
  // After show code
end;

QueueAsyncCall

{$mode delphi} 

procedure TFormMain.FormShow(Sender: TObject);QueueAsyncCall 
begin
  // On on show code
  Application.QueueAsyncCall(ApplicationStart, 0);
end;

procedure TFormMain.ApplicationStart(Ptr: IntPtr);
begin
  // After show code
end;

TTimer

You can use TTimer instance for delayed execution of startup code. Create instance of TTimer component and set its property Enabled to False and Interval to 1. Then from the end of FormShow handler enable timer manually. It will be executed as soon as possible. You need to disable timer at the start of timer handler.

procedure TFormMain.FormShow(Sender: TObject);QueueAsyncCall 
begin
  // On on show code
  Timer1.Enabled := True;
end;

procedure TForm1.Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False;
  // After show code
end;

See also