简体   繁体   中英

Change “Override high DPI scaling behavior” in c#

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?

在此输入图像描述

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM