Darthman
Darthman

Reputation: 457

OpenDialog goes behind modal window on alt+tab

My application have several MDI forms and one of this form have child modal form with detailed information. So, when I open this modal form from my MDI form, I click 'browse' button and create OpenFileDialog. Everything works fine, except when I ALT+TAB. When I ALT+TAB and then ALT+TAB back to my application I see that OpenFileDialog (messageboxes too) is BEHIND my modal window, but in fron of MDI window. There is no StayOnTop or something like that. Only way to bring back OpenDialog in front of all windows is to make second ALT+TAB to my application. This causes Dialog to pop in front of all other windows.

What can I do to prevent Dialog from hiding behind my Modal form? Any suggestion?

I use delphi7 and can't use greater version

Upvotes: 2

Views: 1623

Answers (1)

David Heffernan
David Heffernan

Reputation: 613451

I'm hypothesising that the issue is related to window ownership. In Delphi 7, file dialogs have the hidden application window as their window owner. But the window owner really needs to be the window of the active form.

There are plenty of ways to fix this, but perhaps the simplest is to subclass TOpenDialog and override its TaskModalDialog like this:

function TMyOpenDialog.TaskModalDialog(DialogFunc: Pointer;
  var DialogData): LongBool;
var
  hwndOwner: HWND;
begin
  hwndOwner := Screen.ActiveForm.Handle;
  if hwndOwner = 0 then
    hwndOwner := Application.MainForm.Handle;
  if hwndOwner = 0 then
    hwndOwner := Application.Handle;
  TOpenFilename(DialogData).hwndOwner := hwndOwner;
  Result := inherited TaskModalDialog(DialogFunc, DialogData);
end;

I don't have Delphi 7 at hand to test this, but I'm reasonably confident that something along these lines (with perhaps some tweaking of the hwndOwner choice) will sort it out.

Upvotes: 6

Related Questions