Search code examples
delphiwindows-vistamodal-dialogdelphi-2007

Delphi MainFormOnTaskBar Modal windows bug


HI

I'm using Delphi 2007 and have set the MainFormOnTaskBar property to true.

The issue that I'm having is this.

If you open a child window from the main form and then you show a message dialog from the child window you just opened. When you close the message dialog and then close the child window, the main form will be sent to the back of any other application you have on the screen.

This happens under windows Vista and Windows 7. Does anyone know why this is happens and how can I fix it?


Solution

  • I've fixed this in two ways.

    Firstly by adding stdcall to the end of DoFindWindow in Forms.pas as described by Andreas Hausladen. This handles when a child form is hidden (CloseAction = caHide) instead of released when closing the form.

    Secondly - copied the code from TCustomForm.CMShowingChanged that calls FindTopMostWindow and then activates the window that was returned into TCustomForm.CMRelease.

    (Edit: code block needs to be indented by 4 spaces)

    procedure TCustomForm.CMRelease;
    var
      NewActiveWindow: LongInt;
    begin
      if Application.MainFormOnTaskbar then
      begin
        NewActiveWindow := 0;
    
        if (GetActiveWindow = Handle) and not IsIconic(Handle) then
        begin
          NewActiveWindow := FindTopMostWindow(Handle);
        end;
    
        if NewActiveWindow <> 0 then
        begin
          SetActiveWindow(NewActiveWindow);
        end;
      end;
    
      Free;
    end;
    

    This seems to have done it, I'll continue testing to make sure.