User control won't resize properly when run outside Visual Studio

227 views Asked by At

I'm developing a WinForms app using Visual Studio 2022 and .NET 6. My app makes extensive use of user controls, which are part of the same project. Very often, user controls are anchored so that they resize with their parent panel or form. My user controls resize fine when I run the app from within Visual Studio, either in debug mode (F5) or without debugging (Ctrl+F5). However, they fail to resize properly when I run the compiled EXE file from File Explorer.

I wrote a small demo app to reproduce the issue. This is my demo app running from within Visual Studio.

App running from inside Visual Studio

My user control has a black border. The "Some message" label is top-left aligned, and the OK button is anchored to the bottom right. This looks fine.

The following screenshot shows the same app when run from File Explorer.

App running from File Explorer

As you can see, the user control border is resizing fine, but the OK button fails to anchor to the bottom right as it shows when run from within Visual Studio.

I have searched online but I couldn't find any approaches to diagnose or fix this. Any ideas? Thank you.

EDIT. I suspect this may be related to Visual Studio running as a DPI-unaware process (no scaling). I use a high-DPI monitor so the WinForms designer needs that I use Visual Studio with no scaling. I tried to restart Visual Studio as a DPI-aware process, but it won't do it. Every time I restart it, I get the message that it is at 100% scaling. There is no HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\Image File Execution Options\devenv.exe key in the Registry. How can I revert it to the default DPI-aware setting?

EDIT 2. When I set the ApplicationHighDpiMode option in the project file to DpiUnawareGdiScaled, the app scales fine even when run as a stand-alone EXE, but is a bit blurry. When I set the option to PerMonitorV2, the app looks better on screen but fails to scale properly as described in my original question. Switching to .NET 7 has no effect to either option. Of course, I could set ApplicationHighDpiMode to DpiUnawareGdiScaled and forget about the problem, but (1) I want to know why this is failing and (2) I would like my app to be DPI-aware and run nicely on both standard DPI and high-DPI settings.

0

There are 0 answers