Newly created modal window loses focus and become inacessible in Windows Vista
Asked Answered
C

6

11

Sometimes when I open a modal window in my Delphi application it takes a while to show up, then I notice that the application is kind of blocked, and what happened was that the modal form was open with ShowModal but wasn't displayed and the application became locked as if the Modal Window was in the first layer.

Usually when this happens I have to use Alt + Tab or Windows + Tab to find the "hidden" modal window, but this doesn't work everytime.

This behavior just happens in Vista, but its quite annoying.

Is there some way to prevent this "focus weirdness" from happening?

Thanks.

* EDIT *

Apparently setting Application.MainFormOnTaskbar := True solved the problem, but it is still too early to jump to conclusions because this happens randomly.

* EDIT 2 *

ModalFormOnTaskbar didn't solve the problem, after that I tried setting PopupMode = pmAuto , but that just made the problem worst.

Right now I'm trying to set the PopupParent explicitly and will let you know if the problem is solved.

Corliss answered 13/4, 2009 at 12:9 Comment(0)
N
10

Take a look at the PopupParent property. You may want to set it explicitly for your modal form prior to the ShowModal call. When PopupParent is nil (default) VCL behaves a bit differently depending on the value of the related PopupMode property.

If you set the modal form's PopupParent to the form that's active just before you call ShowModal, that may help.

Neddy answered 13/4, 2009 at 12:52 Comment(1)
What if there is no form that was active before calling ShowModal?Paleolith
E
7

The issue you have started happening when Windows XP introduced the concept of window ghosting. Due to the unusual architecture Delphi uses (all forms are children of a hidden window — TApplication) many Delphi applications experience the same problem.

One way to quickly solve it is to disable window ghosting when initializing the application:

var 
  User32: HMODULE; 
  DisableProcessWindowsGhosting: TProcedure; 
begin 
  User32 := GetModuleHandle('USER32'); 
  if User32 <> 0 then 
  begin 
    DisableProcessWindowsGhosting := GetProcAddress(User32, 'DisableProcessWindowsGhosting'); 
    if Assigned(DisableProcessWindowsGhosting) then 
      DisableProcessWindowsGhosting; 
  end; 
end;

Another possible (more elegant though laborious) solution is to normalize your Delphi application.

A third option would be switching to Delphi 2006 (Delphi 10.0).

Besides the issue you're reporting Delphi's architecture introduces more oddities, among them the different task bar menu and the inability to flash.

Enterprise answered 13/4, 2009 at 15:26 Comment(0)
G
2

I have managed to reduce a lot of these occurences by removing any calls to Application.ProcessMessages that I have in my code, wherever I can.

Garbage answered 25/11, 2010 at 11:30 Comment(0)
E
1

Alt+P+V (.dpr) has Application.MainFormOnTaskbar := True; for default, I don't know why, but if I put Application.MainFormOnTaskbar := False; the problem is solved.

Elan answered 18/2, 2015 at 17:57 Comment(2)
Your answer might be more helpful for you explain how it worksBellwether
Alt+P+V (.dpr) has Application.MainFormOnTaskbar := True; for default, I don't know why, but if I put Application.MainFormOnTaskbar := False; the problem is solved. Sorry.Elan
I
0

You may want to try editing Forms.pas to add the code below into TCustomForm.ShowModal(), just before the call to Application.ModalStarted():

if Assigned(Application) then begin
  while PeekMessage(msg, Application.Handle, CM_ACTIVATE, CM_DEACTIVATE, PM_REMOVE) do begin
    TranslateMessage(msg);
    DispatchMessage(msg);
  end;
end;
Inion answered 5/8, 2009 at 19:6 Comment(0)
I
0

I had the same issue on Windows 10, and I solved it by replacing in the dpr/dproj file :

… Application.CreateForm(TFrmMain, FrmMain);

Application.run; …

By

… Application.CreateForm(TFrmMain, FrmMain);

Try FrmMain.ShowModal; Finally FrmMain.Free; End;

Isolationist answered 1/5, 2019 at 14:26 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.