Reputation: 3
I'm working on a Software using C# and .Net 4.5. I started the project on an older machine with a dpi scaling of 100%. On my new machine I had to setup a scaling of 200% because of the high res display.
Within the Visual Studio Designer the Gui of the software renders perfectly: Gui within the Designer.
The Gui of the compiled software renders bad: Gui in compiled software.
My expectation is, that because I'm using only .Net Gui Elements/Controls and no self designed gui elements, windows should handle the scaling without any problems. But as I see, windows doesn't.
Do I have to enable a HiDPI Option in the project settings in Visual Studio, or do I have to handle HiDPI within my code?
Upvotes: 0
Views: 1586
Reputation: 1
It worked for me by doing this:
That's it, I hope it works for you too.
Upvotes: 0
Reputation: 63772
By default, most Windows Forms applications aren't DPI aware. In fact, most Windows applications aren't DPI aware. So you need to opt-in to DPI-awareness in your application manifest:
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0"
xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
<asmv3:application>
<asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
<dpiAware>true</dpiAware>
</asmv3:windowsSettings>
</asmv3:application>
</assembly>
Since DPI-awareness is per-process, the VS designer has no choice on the matter - it runs within Visual Studio, which is DPI aware.
Make sure everything works properly with both low-DPI and high-DPI, though. DPI aware means "I'm going to handle all DPI scaling correctly, no need to fake it for me; thanks." Hopefully, you've designed your forms with either proper autoscaling and layouting, or using device independent units rather than pixels :)
When your application doesn't declare being DPI aware, Windows assumes you are a legacy application that didn't handle DPI properly. So to prevent issues, it pretends that you're actually rendering to a display that's smaller - in your case, half the size, and then upscales the resulting surface back to proper size. This causes the blurriness, especially with text, but ensures that everything has the proper size and ratios - e.g. you don't get buttons smaller than the text inside them, or controls that overlap.
Both Windows Forms and WPF have great support for proper handling of DPI, if you use the correct layouting approaches, so .NET is definitely DPI-aware-capable. But both are also quite capable of being DPI-unaware, and most applications still ignore DPI issues, so the default is the same as for any other Windows application. Since there's no "legacy WPF" applications, and the simplest way to do WPF is also DPI aware, the default project template for WPF already includes a manifest that declares DPI awareness, so I assume you created your project in Windows Forms :) In fact, given how Windows evolves nowadays, you probably want to have an assembly manifest anyway, to properly manage all the compatibility features :)
For extra bonus, there's even more issues when you have multiple monitors with different DPI - to declare proper support for that, you'd use <dpiAware>true/PM</dpiAware>
. This is only available on Windows 8.1+ and it can be quite tricky to implement correctly.
Upvotes: 4