How can I create a dialog box immediately after viewing the main form of the application?

I used the event TForm OnActivateto give me the opportunity to show a dialog box as soon as my application starts. I want the main form to be already loaded and visible. What a good way to do this?

I found that OnActivateworks fine if the forms WindowStateare not wsMaximized.

in the past, I did what I want in different ways, but I expect there is a better way.

Here is 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?

+5
source share
4 answers

OnCreate :

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;

: , Interval 100 () OnTimer:

procedure Timer1Timer(Sender: TObject);
begin
  Timer1.Enabled := False; // stop the timer - should be executed only once

  form2 := TForm2.Create(Application); 
  form2.ShowModal;
end;

:

OnCreate, OnShow, , , . WM_PAINT UM_DLG. UM_DLG (, db), , .

WM_TIMER , , , WM_TIMER , WM_TIMER .

+15

, .

OnCreate , -, , :

Self.Show;

.

, .

.

0

Well, I don't know if this is the best rating, but I like it:

In the PROGRAM file (.dpr - I use Delphi 7) I add the following lines:

begin
  Application.Initialize;
  Application.CreateForm(TForm1, Form1);
  Form1.Show;                                // Show Main Form
  Form1.Button1.Click;                       // Call event/function
  Application.Run;
end.

Please someone correct me if this is not the best rating!

Hope to be of service to you!

0
source

The way I did this is to use the Application.OnIdle event. This is not an ideal solution, but it works for my situation. Thank you all for your answers!

-1
source

All Articles