How to set PerMonitor DpiAwareness for .NET 6 Windows desktop application?

vb 286 Reputation points
2021-11-27T16:35:58.507+00:00

Hi,

I am having problem setting WPF window based desktop .NET 6 application to work as PerMonitor DpiAware.

System default automatic scaling schema...

  • When user changes Windows 10 display setting scale, WPF-Window is being scaled accordingly.
  • Also, when window is dragged to another display, that has different scale, window is being scaled.

I want to prevent these automatic scaling because I'm using my own scaling schema.

Before .NET 6, when application was using WPF 4.8, following two items together were preventing such automatic behavior:

A) @ app.manifest

...
<application xmlns="urn:schemas-microsoft-com:asm.v3">
    <windowsSettings>
        <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware>
        <dpiAwareness xmlns="http://schemas.microsoft.com/SMI/2016/WindowsSettings">PerMonitor</dpiAwareness>
    </windowsSettings>
</application>
...

B) @ app.config

<?xml version="1.0" encoding="utf-8"?>
<configuration>
    <startup> 
        <supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.8"/>
    </startup>
  <runtime>
    <AppContextSwitchOverrides value="Switch.System.Windows.DoNotScaleForDpiChanges=true"/>
  </runtime>
</configuration>

Now, @ .NET 6, that is not working anymore!

How to tell Windows to stop automatic windows scaling for an desktop Net 6 application?

Thanks in advance!
Vladimir

Windows Presentation Foundation
Windows Presentation Foundation
A part of the .NET Framework that provides a unified programming model for building line-of-business desktop applications on Windows.
2,784 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
11,012 questions
{count} votes

1 answer

Sort by: Most helpful
  1. vb 286 Reputation points
    2022-06-20T22:08:23.437+00:00

    Hi!

    I have found solution... trick was ... app.config file is not NET6 compatible.

    So, AppContextSwitchOverrides … Switches like...

    Switch.System.Windows.DoNotScaleForDpiChanges  
    

    are transferred from

    "appname".config => "appname".runtimeconfig.json

    In order to control these switches ... I had to put new item group into *.csproj

       <Project>  
       ...  
       <ItemGroup>  
          <RuntimeHostConfigurationOption Include="Switch.System.Windows.DoNotScaleForDpiChanges" Value="true" />  
          <RuntimeHostConfigurationOption Include="Switch.System.IO.UseLegacyPathHandling" Value="false" />  
       </ItemGroup>  
       ...  
       </Project>  
    

    So, RuntimeHostConfigurationOption has done the job!

    Thanks for your time!
    V.

    P.S. app.manifest still stands as like it was before.

    2 people found this answer helpful.

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.