Add support for proper Windows Forms High DPI scaling
The issue
I was trying to get a Windows Forms PS script working in a multi-monitor setup with different screen DPIs and ran into some issues:
- With the default settings, the application gets bitmap-scaled up by Windows, as expected. This makes it look blurry on high DPI screens.
- When adding the
-DPIAwareoption, the application doesn't get scaled at all. It no longer looks blurry, but it is too small on high DPI screens, making text hard to read.
Starting with .NET Framework 4.7, Windows Forms added proper support for high DPI screens. The article outlines 5 requirements to get this to work:
- [X] The application has to run on Windows 1703 or higher.
- [X] Declare compatibility with Windows 10 in the manifest. (Already possible using the
-supportOSswitch.) - [X] Call
EnableVisualStyles()at the start of the application. (Already done by default for-noConsoleapps.) - [ ] Add a special configuration key in the app.config file. (Not done automatically, but could be done manually or with an additional script.)
- [ ] Be compiled with .NET Framwork 4.7 or higher as the target. (Not currently possible.)
Changes in this pull request
This pull request adds switches to fulfill the last two requirements, and with them I was able to successfully get proper scaling with no blurryness to work:
- Adds
-net48to compile with .NET Framework 4.8 as the target. Also adds this as the minimum required version in the app.config, if one is generated. - Adds
-winFormsDPIAware. This flag adds the required section in the app.config for Windows Forms to enable its High DPI support (therefore it forces the-configFileflag). It also forces-net48and-supportOS, since both are required for the scaling to work.
As a note, the -DPIAware flag (i.e. adding the DPI Aware manifest entry) is not required for -winFormsDPIAware, but it also doesn't conflict with it. Instead, it changes the behaviour when the app is run on Windows versions before 1703. If the flag is set, the application will fall back to the non-scaled, non-blurry (too small) rendering. If it is not set, it will fall back to the bitmap-scaled, blurry rendering. (This is also the fallback behavior you will see if the app.config is missing when the application is run.)
Hello @ReneZeidler,
I'm investigating this.
Greetings
Markus
Hello @ReneZeidler,
thank you for your investigation. I included the new parameter -winFormsDPIAware in the new version 1.0.12 of PS2EXE yesterday and today. There was no way to mege the pull request due to changes and the companion project https://github.com/MScholtes/TechNet-Gallery/tree/master/PS2EXE-GUI
Greetings
Markus