WPF Web Browser Control and DPI Scaling

后端 未结 4 1814
星月不相逢
星月不相逢 2021-02-07 06:50

I\'m working with a WPF application that uses the Web Browser control and I\'m having issues with High DPI scaling.

It looks like the Web Browser control is not properl

4条回答
  •  一生所求
    2021-02-07 07:18

    The solution for me eventually was to use a later version of .NET - 4.6.2 has improved DPI support and so this issue in the application I mentinoed resolved itself when High DPI settings are applied in the application manifest.

    If you're targeting .NET 4.6.2 or later DPI Scaling is implicitly enabled. You shouldn't need anything else.

    If you target earlier versions either add to the manifest:

    
        
          true
        
    
    

    and enable DPI Awareness in your AssemblyInfo.cs:

    [assembly: DisableDpiAwareness]
    

    or (as I was doing for various reasons) using code that has to be call from app.xaml.cs:

        public static bool SetPerMonitorDpiAwareness(ProcessDpiAwareness type = ProcessDpiAwareness.Process_Per_Monitor_DPI_Aware)
        {
            try
            {
                // for this to work make sure [assembly: DisableDpiAwareness]
                ProcessDpiAwareness awarenessType;
                GetProcessDpiAwareness(Process.GetCurrentProcess().Handle, out awarenessType);
                var result = SetProcessDpiAwareness(type);
                GetProcessDpiAwareness(Process.GetCurrentProcess().Handle, out awarenessType);
    
                return awarenessType == type;
            }
            catch
            {
                return false;
            }            
        }
    

    To call somewhere in App.xaml.cs startup code:

            try
            {   // Multi-Monitor DPI awareness for screen captures
                // requires [assembly: DisableDpiAwareness] set in assemblyinfo
                bool res = WindowUtilities.SetPerMonitorDpiAwareness(ProcessDpiAwareness.Process_Per_Monitor_DPI_Aware);
            }
            catch {  /* fails not supported on Windows 7 and older */ }
    

    Again all of this was no longer necessary after targeting .NET 4.6.2 or later and things just work. The explicit code allowed more control over exactly what profile to use.

    .NET 4.6.2 introduces a host of improvements with DPI scaling including multi-monitor scaling support (previously only the main monitor was supported) and is automatically doing the right thing with most hosted controls including the Web browser control. Given that most machines these days are either on .NET 4.7.x or 4.6.2 based on Windows update targeting 4.6.2 should be considered a baseline for WPF IMHO.

    Note: If you switch DPI settings in Windows while your app is running, there are also events you can trap that will tell you of the DPI change. Not much that you can do with this other than restart as the app doesn't actually pick up the changes but at least you can let the user know that the DPI has changed and they have to restart to adjust to the new DPI settings.

提交回复
热议问题