Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Form is hidden behind other forms when ShowModal is called

My application is based on modal forms. Main form opens one form with ShowModal, this form opens another with ShowModal, so we have stacked modal forms. There is sometimes a problem that when we call ShowModal in new form, it hides behind previous forms, instead of showing on top. After pressing alt+tab, form comes back to the top, but this is not good solution. Did You meet this problem and how did you handle it?

EDIT:

I use Delphi 7.

like image 755
LukLed Avatar asked Oct 28 '09 18:10

LukLed


2 Answers

You didn't mention which version of Delphi...

Newer Delphi versions have added two new properties to TCustomForm: PopupMode and PopupParent. Setting PopupParent of your modal dialog to the form that's creating that dialog makes sure that the child form stays on top of it's parent. It usually fixes the problem you're describing.

I think this pair of properties were added in Delphi 2006, but it may have been 2005. They're definitely there in Delphi 2007 and up.

EDIT: After seeing you're using Delphi 7, the only suggestion I have is that, in the code that displays your modal form, you disable the form creating it, and re-enable on return. That should prevent the creating window from receiving input, which may help keep the Z-order correct.

Something like this may work (untested, as I'm no longer using D7):

procedure TForm1.ShowForm2; begin   Self.Enabled := False;   try     with TForm2.Create(nil) do     begin       try         if ShowModal = mrOk then           // Returned OK. Do something;       finally         Free;       end;     end;   finally     Self.Enabled := True;   end; end; 

If Form2 creates a modal window (as you've mentioned), just repeat the process - disable Form2, create Form3 and show it modally, and re-enable Form2 when it returns. Make sure to use try..finally as I've shown, so that if something goes wrong in the modal form the creating form is always re-enabled.

like image 56
Ken White Avatar answered Oct 04 '22 11:10

Ken White


Sorry for adding a separate answer, but I have done a bit more research, and some of it indicates that my previous answer (DisableProcessWindowsGhosting) doesn't help. Since I can't always reproduce this issue, I cannot say for sure.

I found a solution that appears to appropriate. I referenced the code in Delphi 2007 for the CreateParams method and it matches pretty close (without having all of the other code that handles PopupMode).

I created the unit below which subclasses TForm.

unit uModalForms;  interface  uses Forms, Controls, Windows; type   TModalForm = class(TForm)   protected     procedure CreateParams(var params: TCreateParams); override;   end;  implementation  procedure TModalForm.CreateParams(var params: TCreateParams); begin   inherited;    params.WndParent := Screen.ActiveForm.Handle;    if (params.WndParent <> 0) and (IsIconic(params.WndParent)     or not IsWindowVisible(params.WndParent)     or not IsWindowEnabled(params.WndParent)) then     params.WndParent := 0;    if params.WndParent = 0 then     params.WndParent := Application.Handle; end; 

What I do then is include this unit in with a form unit, and then change the form's class (in the .pas code file) from class(TForm) to class(TModalForm)

It works for me, appears to be close to CodeGear's solution.

like image 28
Jim Gilmartin Avatar answered Oct 04 '22 11:10

Jim Gilmartin