Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a a dialog box happen directly after my app's main form is visible?

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?

like image 252
X-Ray Avatar asked Jun 01 '11 14:06

X-Ray


3 Answers

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.

like image 141
andrius Avatar answered Oct 19 '22 23:10

andrius


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:

  • start a short timer.
  • PostMessage() a custom window message to yourself.
  • use TThread.CreateAnonymousThread() or TTask to call TThread.Queue().
  • use TThread.ForceQueue() (10.2 Tokyo and later only).
like image 22
Remy Lebeau Avatar answered Oct 20 '22 00:10

Remy Lebeau


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!

like image 26
X-Ray Avatar answered Oct 19 '22 23:10

X-Ray