.NET中app.config与app.manifest的区别(WinForms项目场景)
Hey there! Let's break down the key differences between WinForms and other .NET project types (like WPF) when it comes to high-DPI settings targeting Windows 10 Creators Update, especially around those configuration files you're editing.
Core DPI Awareness Models
- WinForms: Historically had limited high-DPI support, so you need to explicitly opt into modern DPI awareness for the Creators Update. The recommended mode here is Per-Monitor DPI Awareness v2, though System DPI Awareness is also an option. WinForms relies heavily on configuration files + code-level tweaks (like handling
DpiChangedevents) to get right. - WPF/Modern .NET Projects: WPF has built-in DPI awareness by default (using System DPI Awareness out of the box). Enabling Per-Monitor v2 for Creators Update is more streamlined, and its vector-based layout engine means fewer scaling glitches without extra code fixes compared to WinForms.
Key Configuration File Differences
Assuming you're working with the application manifest and app.config (the two most common files for DPI settings):
- Application Manifest:
- For WinForms: You must add specific
<dpiAware>and<dpiAwareness>elements to declare your DPI mode. For Creators Update's Per-Monitor v2, the entry looks like this:
Skip this, and WinForms will fall back to blurry legacy scaling on high-DPI displays.<application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">true/PM</dpiAware> <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitorV2, PerMonitor</dpiAwareness> </windowsSettings> </application> - For WPF: The manifest entry is similar, but WPF respects it natively. You won't need as many workarounds since its layout scales smoothly by default.
- For WinForms: You must add specific
- app.config:
- For WinForms: You need to add the
System.Windows.Forms.ApplicationConfigurationSection(only applicable to WinForms) to enable .NET Framework 4.7+ high-DPI features required for Creators Update support:
This tells WinForms to use modern DPI APIs and auto-resize controls where possible.<configuration> <System.Windows.Forms.ApplicationConfigurationSection> <add key="DpiAwareness" value="PerMonitorV2" /> <add key="EnableWindowsFormsHighDpiAutoResizing" value="true" /> </System.Windows.Forms.ApplicationConfigurationSection> </configuration> - For WPF:
app.configdoesn't require these WinForms-specific sections. WPF uses its own native DPI handling, with most adjustments done via the manifest or targeted code.
- For WinForms: You need to add the
WinForms-Specific Notes for Your Project
Since you're focused on a WinForms project:
- You must target .NET Framework 4.7 or higher to access the modern DPI APIs needed for Creators Update.
- Some legacy controls (like
DataGridView) might still have scaling issues even with config settings—you may need to handleDpiChangedevents manually to adjust sizes or fonts. - Set
AutoScaleMode.Dpion your forms, and test each control's behavior across different DPI levels to catch edge cases.
Quick Recap
The config changes you're making are specific to WinForms. The manifest settings are partially shared with other project types, but the app.config entries for System.Windows.Forms.ApplicationConfigurationSection are exclusive to WinForms projects. You don't need to worry about these affecting non-WinForms codebases.
内容的提问来源于stack exchange,提问作者Chris K

