Change "Override high DPI scaling behavior" in c#
Asked Answered
S

1

14

We have a control inside a WinForm (CefSharp control) that suffers from graphical artifacts when a users screen is set to 125% on windows. Its not just the control, stand alone Chrome does it to an extent. The only way we have been able to fix the artifacts is by changing the exe setting pictured below. Is there a way to change it in code?

enter image description here

edit: This is not a duplicate. As making an app DPI aware is not the same as DPI scaling heavior

Sooth answered 18/8, 2017 at 15:4 Comment(6)
@hans-passant This is not a duplicate. As making an app DPI aware is not the same as DPI scaling heaviorSooth
Are you looking to force device scale factor? The following setting to prevent the browser from scaling when using a higher DPI settings.CefCommandLineArgs.Add("force-device-scale-factor", "1");Helico
@Helico Thanks for the suggestion, but the issue still persist.Sooth
@Sooth Did you find a solution? I'm having the exact same problem.Militiaman
@Militiaman No, unfortunately we just started building our controls in xamlSooth
Original dup link for programmers that look for the correct solution: https://mcmap.net/q/130636/-how-to-configure-an-app-to-run-correctly-on-a-machine-with-a-high-dpi-setting-e-g-150Dermatology
R
8

Way 1. How Override high DPI scaling (check checkbox) with registry

Start up Registry Editor and navigate to this key:

HKEY_CURRENT_USER\­Software\­Microsoft\­Windows NT\­CurrentVersion\­AppCompatFlags\­Layers

Now add a string value (REG_SZ) whose name is the full path to the application executable and whose value is HIGHDPIAWARE

Code example:

string appPath = string.Format(@"{0}\{1}.exe", My.Application.Info.DirectoryPath, My.Application.Info.AssemblyName);       
My.Computer.Registry.SetValue(@"HKEY_CURRENT_USER\Software\Microsoft\Windows NT\CurrentVersion\AppCompatFlags\Layers", appPath, "HIGHDPIAWARE");

Read more: High DPI Settings in Windows


Way 2. How to change DPI-aware in assembly manifest?

DPI-aware applications are not affected by the OS. Such applications render themselves to fit the actual DPI of a screen and provide a much better visual experience.

Add the <dpiAware> element to the manifest code and set its value to true.

<?xml version="1.0" encoding="utf-8"?>
<assembly xmlns="urn:schemas-microsoft-com:asm.v1" manifestVersion="1.0" xmlns:asmv3="urn:schemas-microsoft-com:asm.v3" >
    <assemblyIdentity version="1.0.0.0" name="MyApplication.app"/>
    <trustInfo xmlns="urn:schemas-microsoft-com:asm.v2">
        <security>
            <requestedPrivileges xmlns="urn:schemas-microsoft-com:asm.v3">
                <requestedExecutionLevel level="asInvoker" uiAccess="false" />
            </requestedPrivileges>
        </security>
    </trustInfo>
    <asmv3:application>
        <asmv3:windowsSettings xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">
             <dpiAware>true</dpiAware> 
        </asmv3:windowsSettings>
    </asmv3:application>
</assembly>

Additional resources:

  1. High DPI Support

  2. High DPI Desktop Application Development on Windows (also Application Manifests)

  3. DPI Awareness - Unaware in one Release, System Aware in the Other [duplicate]

  4. Writing High-DPI Aware Windows Apps

  5. Writing DPI-Aware Desktop and Win32 Applications

Reorientation answered 19/7, 2018 at 20:30 Comment(10)
It did not work. The 'Override high DPI scaling behavior' checkbox is still not checked.Bowden
asmv3 is prefix of target namespace .Reorientation
'Override high DPI scaling behavior' checkbox will not be checked. But effect is same to checkedReorientation
But this check box is exactly what I am trying to accomplish.Bowden
I dont know, it should fix your problem without checking. If it fixes there is no need for checking checkbox, I think. Also for correct testing you need unckeck that checkboxReorientation
Ok. I will try to test it and let you know what happens. Thank you so much!Bowden
Read github.com/cefsharp/CefSharp/wiki/…Fonteyn
@Los Pollos Hermanos - FYI great name by the way. The change in the manifest file did not have the same effect as checking the box and then selecting 'system' in the drop down. It didn't work.Bowden
After that box for 'Override High DPI' is checked, the drop down list is available to change. When I check the 'Override High DPI' box, and then select 'system' instead of 'application', I get the desired results. I'm trying to figure out how to change that setting in the manifest file now.Bowden
It works!!! Here is the alteration I made to give the desired effect for my situation: <dpiAware>true/system</dpiAware> Thank you SOO much!!! I'll award the bounty in three more hours when the system will allow me to.Bowden

© 2022 - 2024 — McMap. All rights reserved.