winforms icon indicating copy to clipboard operation
winforms copied to clipboard

Incorrect Z-Order behavior for popup forms in .NET compared to .NET Framework when using SetWindowPos with HWND_TOPMOST

Open mikhailovalex opened this issue 1 month ago • 9 comments

.NET version

.NET SDK: Version: 10.0.100 Commit: b0f34d51fc Workload version: 10.0.100-manifests.5fb86115 MSBuild version: 18.0.2+b0f34d51f

Did it work in .NET Framework?

Yes

Did it work in any of the earlier releases of .NET Core or .NET 5+?

Didn't check it.

Issue description

When showing a custom dialog-form from a "pop-up" in Windows Forms on .NET, the Z-Order behaves differently compared to .NET Framework. Specifically:

In .NET Framework, calling SetWindowPos with HWND_TOPMOST (-1) works as expected—the popup does not stay topmost permanently. In .NET, the pop-up window remains topmost always, causing UI elements like color pickers or other dialogs to appear behind the pop-up.

This leads to incorrect layering and usability issues in scenarios where multiple pop-ups or dialogs are involved.

Steps to reproduce

WinFormsApp9_ZOrderSample.zip

Image Image

In the attachments, you can find two projects: WinFormsApp9 - it's .NET application WindowsFormsApp1 - it's .NET FW application

To reproduce the issue:

  1. Run the .NET project
  2. Press button1 - it will open a special pop-up window
  3. Press button1 again - it will open Form, using the ShowDialog method. As you can see, this form shows behind the pop-up.

mikhailovalex avatar Dec 03 '25 13:12 mikhailovalex

@Olina-Zhang can you find out if this has regressed and if so, when?

merriemcgaw avatar Dec 03 '25 21:12 merriemcgaw

I was able to repro it with .NET 6.0. @Olina-Zhang can you please try one of the older versions?

John-Hart avatar Dec 03 '25 22:12 John-Hart

@John-Hart @merriemcgaw, the issue does reproduce on .NET 5.0, not reproduce on .NET Core 3.1 as below screenshot.

Image

Zheng-Li01 avatar Dec 04 '25 01:12 Zheng-Li01

Thanks @Zheng-Li01! @LeafShi1 can you investigate changes in this area from way back then? It is definitely something that changed between 3.1 and 5.0.

merriemcgaw avatar Dec 04 '25 19:12 merriemcgaw

Could this be related to #6190 ?

hyperquantum avatar Dec 06 '25 14:12 hyperquantum

and another issue https://github.com/dotnet/winforms/issues/9351

As a workaround, adding form.TopMost = true for pop-up window

Image

LeafShi1 avatar Dec 08 '25 08:12 LeafShi1

In .NET Framework, ShowDialog will propagate the TopMost property from the main form to the popup form.

Image

However, in modern .NET, this behavior has changed — ShowDialog does not automatically inherit the TopMost setting, so the popup form will not be topmost unless explicitly set.

LeafShi1 avatar Dec 12 '25 09:12 LeafShi1

Is this switch available in VB startup process? Is there a way to set it from VB?

paul1956 avatar Dec 12 '25 20:12 paul1956

Is this switch available in VB startup process? Is there a way to set it from VB?

Yes, it can also be used in VB projects, simply set "System.Windows.Forms.MirrorTopMostForModalDialogs": true in the app.runtimeconfig.json file.

LeafShi1 avatar Dec 15 '25 02:12 LeafShi1