[delphi] Event After Show

2021-01-11

Event After Show

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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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

There is a way how to execute asynchronous operations in Lazarus using Asynchronous Calls. This is useful mainly for parallel processing but it can be used also for execution of form after show handler.

1
2
3
4
5
6
7
8
9
10
11
12
13

{$mode delphi}

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

procedure TFormMain.AfterShow(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.

1
2
3
4
5
6
7
8
9
10
11
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;

참조

https://wiki.freepascal.org/Execute_action_after_form_is_shown

Tags: delphi