I have been using TForm
's OnActivate
event to give me the opportunity to show a dialog box as soon as my app has started. I want the main form to already be loaded & visible. What's a good way to do this?
I have found that OnActivate
works fine unless the forms WindowState
is wsMaximized
.
in the past I've accomplished what I want in various ways but I expect there is a better way.
Here's what worked for me:
procedure TForm1.FormCreate(Sender: TObject);
begin
Application.OnIdle:=OnIdle;
end;
procedure TForm1.OnIdle(Sender: TObject; var Done: Boolean);
begin
Application.OnIdle:=nil;
form2:=TForm2.Create(Application);
form2.ShowModal;
end;
Is there a better way?
In the form's OnCreate event handler post a user message and show the dialog in the handler of the message:
unit Unit1;
interface
const
UM_DLG = WM_USER + $100;
type
TForm1 = class(TForm)
...
procedure UMDlg(var Msg: TMessage); message UM_DLG;
...
implementation
procedure TForm1.FormCreate(Sender: TObject);
begin
PostMessage(Handle, UM_DLG, 0, 0);
end;
procedure TForm1.UMDlg(var Msg: TMessage);
begin
form2 := TForm2.Create(Application);
form2.ShowModal;
end;
Although I found timer approach even better: just drop a timer component on the form, set Interval to 100 (ms) and implement OnTimer event:
procedure Timer1Timer(Sender: TObject);
begin
Timer1.Enabled := False; // stop the timer - should be executed only once
form2 := TForm2.Create(Application);
form2.ShowModal;
end;
The difference between the two approaches is:
When user message is posted either from OnCreate or OnShow handler, the message is dispatched with normal priority which means that other window initialization messages might get posted and handled after it. For essence, WM_PAINT messages would be handled after UM_DLG message. If UM_DLG message is taking long time to process without pumping the message queue (for example, opening db connection), then form would be shown blank without client area painted.
WM_TIMER message is a low-priority message and than means that form initialization messages would be handled first and only then WM_TIMER message would be processed, even if WM_TIMER message is posted before the form creation completes.
In the MainForm's OnShow
event, you can do one of the following to show the dialog box after a delay that allows the MainForm to finish fully showing itself first:
PostMessage()
a custom window message to yourself.TThread.CreateAnonymousThread()
or TTask
to call TThread.Queue()
.TThread.ForceQueue()
(10.2 Tokyo and later only).The way I ended up doing it was to use the Application.OnIdle event. It's not a perfect solution but it does work for my situation. Thank you all for your answers!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With