How to set PerMonitor DpiAwareness for .NET 8 Windows desktop application in WPF?

MERUN KUMAR MAITY 576 Reputation points
2024-09-03T11:03:57.19+00:00

Hi,

I am having problem setting WPF window based desktop .NET 8 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 8, that is not working anymore!

After a lots of hard work finally I find the solution...trick was ... app.config file is not NET 8 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, now the PerMonitor DpiAwareness is working as expected. But there a little bit problem arise, When I start my Application on a different monitor which have different screen resolution and different screen scaling, my App is unable to re-scale it's UI.

But the app runs perfectly when I change the screen DPI and screen resolutions at the time of running and if I close the app and run again on that different screen DPI and screen resolution it's not working as expected.

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,762 questions
Windows API - Win32
Windows API - Win32
A core set of Windows application programming interfaces (APIs) for desktop and server applications. Previously known as Win32 API.
2,592 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.
10,858 questions
{count} votes

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.