Keep child windows in front!

Delphi error child windows in front

Review Thinfinity VirtualUI
New here? Read more about why software developers are adopting Thinfinity VirtualUI to increase the value of their Windows applications.
Delphi error child windows in frontEarly this year, we shared an article about a Delphi error. As we explained there, this error happens when the Application.MainFormOnTaskBar property is set to False and an application main form is minimized. When this happens the window gets hidden, but it has no way to be restored.
We also found similar behavior in applications where child windows —modal or modeless— were drawn or hidden behind the main window; and not in front, as expected. We know that this can happen under certain circumstances when the project has the Application.MainFormOnTaskBar property set to False. This seems to suggest that Delphi could be the culprit.

Why the error occurs?

All programs send messages to the operative system. Some messages indicate a change in the window “z” order, which defines which form is in the front, and which forms are behind.
When the Application.MainFormOnTaskBar property is set to false, all forms —modal and modeless— are siblings of the mainform. They all share the application window as common parent. Being all siblings, all of them have the chance to go behind the other.
At some point during the display of a modal form, a WM_WINDOWPOSCHANGED message is sent to the mainform, moving it to the top.

How to fix the error and keep the child windows in front

The best solution is to set the Application.MainformOnTaskbar property to True right after the Application.CreateForm statement.

program KeepInFrontExample;
  MainForm in 'MainForm.pas' {FormMain};
{$R *.res}
  Application.CreateForm(TFormMain, FormMain);
  Application.MainFormOnTaskbar := True;

In this way, Delphi will set the mainform as a parent of all the other forms. This workaround leaves no way for any form to go behind the mainform, as it would be their parent element.
That is how you can make sure child windows are always in front of the mainform.

Other functions to bring child windows to top

Need help to activate an inactive child window with the bring the window on front function?
Other languages apply the BringWindowToTop function, but what if BringWindowToTop doesn’t work?
If you need help with C++ or C# examples, contact us.


Thinfinity Solutions for remote desktop, screen sharing, digital workspace and application virtualization.

Thinfinity VirtualUI

As you already know, Thinfinity VirtualUI is a web-enabling SDK to run apps on a browser without rewriting the code.
If you’re a GitHub user, you may want to check our VirtualUI GitHub project.

Remote Access Solutions

Want to learn more about our remote access applications?

We will be happy to assist you and show you our portfolio for remote desktop, screen sharing, and digital workspace.

Explore our other web RDP and online workspace solutions, enjoy our free trials, or book a call and request a custom demo HERE. No commitment!

Comments (4)

I agree, we are moving back to the desotkp application development approach with Javascript. From event-driven design to the MVC architecture, these applications are going to get more and more complex over time. Given that much of the interaction is request-response, it feels more like client-server programming with the added constraint of a stateless server. This means that many patterns common to client-server app development will begin to trickle back into the more complex JS-heavy applications.

Solution provided does not work. Sorry.
This worked for me : add
Application.ModalPopupMode := pmAuto;
in the .dpr after Application.Initialize;

Hi Chris,
The solution proposed actually works with all Delphi forms. When you set Application.MainFormOnTaskbar := True, all the forms that Delphi creates are created with the Mainform as parent instead of TApplication, thus no other window can go behind the mainform because all depends on it.
Application.ModalPopupMode := pmAuto seems to act on common dialogs like OpenFileDialog, PageSetup, etc. which IMHO is complementary to the solution we provided and is actually a great find.
Gustavo RIcardi

Hi Gustavo,
We do not let delphi autocreate the forms in our app, there are too much form to let delphi cretae them all at startup, so we do this ourself manually. matbe we are missing something when whe do the create. What properties should we set ?

Leave a comment

Privacy Preferences
This website uses cookies to improve your experience while you navigate through the website. Out of these, the cookies that are categorized as necessary are stored on your browser as they are essential for the working of basic functionalities of the website. We also use third-party cookies that help us analyze and understand how you use this website. These cookies will be stored in your browser only with your consent. You also have the option to opt-out of these cookies. But opting out of some of these cookies may affect your browsing experience.