Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.1k views
in Technique[技术] by (71.8m points)

delphi - 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

Just Browsing Browsing

[2] html - How to create even cell spacing within a

1.4m articles

1.4m replys

5 comments

57.0k users

...