Delphi onshow main form / modal form

I have a project that has a basic form and some other forms. When the application loads, it needs to complete some tasks and show the results in a modal form on top of the main form. The problem is that if I put a function call to do the tasks / create and show the modal form in the main forms of the onshow event, the modal form will appear, but the main form will not be closed until the modal form is closed, which is what I expect. To counter this, I added a timer to the main form and launched it in the main forms of the onshow event, the timer calls a function to perform tasks / create and display a modal form. So now the main form appears before the modal form.

However, I don’t see this being the best solution, and I was wondering if anyone could offer a better option.

I am using Delphi 7

Colin

+7
source share
3 answers

One of the most commonly used options is to post on OnShow . Like this:

 const WM_SHOWMYOTHERFORM = WM_USER + 0; type TMyMainForm = class(TForm) procedure FormShow(Sender: TObject); protected procedure WMShowMyOtherForm(var Message: TMessage); message WM_SHOWMYOTHERFORM; end; ... procedure TMyMainForm.FormShow(Sender: TObject); begin PostMessage(Handle, WM_SHOWMYOTHERFORM, 0, 0); end; procedure TMyMainForm.WMShowMyOtherForm(var Message: TMessage); begin inherited; with TMyOtherForm.Create(nil) do begin try ShowModal; finally Free; end; end; end; 
+9
source

Why don't you use the MainForm OnActivate event MainForm OnActivate this?

 procedure TMyMainForm.FormActivate(Sender: TObject); begin //Only execute this event once ... OnActivate := nil; //and then using the code David Heffernan offered ... with TMyOtherForm.Create(nil) do begin try ShowModal; finally Free; end; end; 

Setting the event to nil ensures that this code runs only once at startup.

+2
source

The OnShow event is OnShow just before the ShowWindow Windows API function is ShowWindow . It is this ShowWindow call ShowWindow actually causes a window to appear on the screen.

So, ideally, you need to start something right after calling ShowWindow . It turns out that the VCL code that controls all this is inside the TCustomForm message TCustomForm for CM_SHOWINGCHANGED . This message handler fires the OnShow event, and then calls ShowWindow . Thus, a great solution is to show your modal form immediately after starting the CM_SHOWINGCHANGED handler. Like this:

 type TMyMainForm = class(TForm) private FMyOtherFormHasBeenShown: Boolean; protected procedure CMShowingChanged(var Message: TMessage); message CM_SHOWINGCHANGED; end; ..... procedure TMyMainForm.CMShowingChanged(var Message: TMessage); begin inherited; if Showing and not FMyOtherFormHasBeenShown then begin FMyOtherFormHasBeenShown := True; with TMyOtherForm.Create(nil) do begin try ShowModal; finally Free; end; end; end; end; 
0
source

All Articles