Delphi onshow main form / modal form

前端 未结 3 1575
情歌与酒
情歌与酒 2021-02-04 16:57

I have a project which has a main form and some other forms. When the app loads it needs to carry out some tasks and show the results in a modal form on top of the main form. T

3条回答
  •  渐次进展
    2021-02-04 17:13

    The OnShow event is fired immediately before the call to the Windows API function ShowWindow is made. It is this call to ShowWindow that actually results in the window appearing on the screen.

    So you ideally need something to run immediately after the call to ShowWindow. It turns out that the VCL code that drives all this is inside a TCustomForm message handler for CM_SHOWINGCHANGED. That message handler fires the OnShow event and then calls ShowWindow. So an excellent solution is to show your modal form immediately after the handler for CM_SHOWINGCHANGED runs. 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;
    

提交回复
热议问题