views:

1089

answers:

6

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.

+1  A: 

Make sure that the popup form has the other form as owner.

Lars D
Actually, that's wrong. Owner just means that the form is responsible for freeing it's children. It has nothing to do with the way it displays; that would be the Parent, and it won't work to do what's needed here. Setting Form2.Parent to Form1 would result in Form2 being embedded in Form1. Setting Form2.Owner to Form1, as in "Form2 := TForm2.Create(Form1);" means that, when Form1 closes, it's responsible for freeing Form2. That's all.
Ken White
Owner takes care of freeing owned objects. Does it really work? I don't see any reference to owner in ShowModal or Show of Forms unit.
LukLed
@LukLed: Owner frees the objects it owns, but that has nothing to do with the problem you're describing. (Owner has to do with freeing allocated resources, not with controlling anything to do with displaying them.)
Ken White
... besides poOwnerFormCenter which therefore seems an unfortunate idea).
Ulrich Gerhardt
A: 

I have found that using the "Always On Top" flag on more than one form causes problems with the Z order. And you may also find the need for the BringWindowToTop function.

When launching a message box using the built-in WinAPI (MessageBox), I have found that passing the calling window's handle is necessary in order to make sure that the the prompt appears on top all the time.

Scott W
See my response to Lars D. Owner (or Parent) have nothing to do with the problem being described. The problem is an after-effect of TAppication's hidden window being removed from the Taskbar; it caused the problem with child forms losing their proper place in the Z-order, which is what PopupParent and PopupMode were created to fix.
Ken White
Fair enough... on further inspection, I realize that I was thinking about using the Windows.MessageBox function and passing the calling window's handle to make sure that the MessageBox shows up on top of the caller -- which I was considering to be the "parent". Will edit my answer to reflect this.
Scott W
+5  A: 

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.

Ken White
Sorry, this is Delphi 7. There is no PopupMode and PopupParent, but is it good to know they exist.
LukLed
I could try this solution, but we have a lot of modal forms in project, message boxes are modal, so it may be sometimes hard to implement. But I'll try to do that where possible.
LukLed
+1  A: 

From this link it appears that the problem is with the "Ghosting window" that was introduced in 2000/XP. You can disable the ghosting feature by calling the following code at startup.

procedure DisableProcessWindowsGhosting;
var
  DisableProcessWindowsGhostingProc: procedure;
begin
  DisableProcessWindowsGhostingProc := GetProcAddress(
    GetModuleHandle('user32.dll'),
    'DisableProcessWindowsGhosting');
  if Assigned(DisableProcessWindowsGhostingProc) then
    DisableProcessWindowsGhostingProc;
end;

The only issue that I can see is that it will cause problems with the feature that allows for the user to minimize, move, or close the main window of an application that is not responding. But in this way you do not have to cover each call with the Self.Enabled := False code.

Jim Gilmartin
If it really works, it would be great. Thank you. This problem bothers me for a very long time. What intrigues me is 'However, if the form had the WS_POPUP style and the "owner" was the correct window, even a "ghosted" form would not be allowed to switch Z-order to under it’s owner, thus there is no chance for a modal dialog to suddenly disappear.'. I didn't see any reference to Owner in Forms unit.
LukLed
+1  A: 

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.

Jim Gilmartin
I'll try with DisableProcessWindowsGhosting first and check if it works. Then I'll look at this. Thanks.
LukLed
A: 

Just set the Visible property of the form, that you want to open modal, to False. Then you can open it with .ShowModal(); and it will work.

H. Gokhan Mamaci