CefSharp在高DPI的屏幕上出现黑边(winform)

问题现象

如图所示:在高DPI(168) 缩放比例为:175%的配置下,,cefsharp控件的左侧和上部出现黑边

(电脑配置图)

 

(问题图)

解决办法

1.将cefsharp的gpu设置为无效,(后遗症,h5动画会出现卡顿现象,慎用)

var settings = new CefSettings();
settings.Locale = "zh-CN";
settings.CefCommandLineArgs.Add("disable-gpu", "1");

Cef.Initialize(settings);

2.将屏幕的DPI置为96(缩放比例为100%)(后遗症,不可能每个电脑都去配置)

3.支持高DPI(后遗症,winform窗体会缩放)

Cef.EnableHighDPISupport();

4.(采用)让整个程序支持高DPI

(1)为项目添加应用程序清单文件(app.manifest),并取消下面行的注释

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

(2)将Form和UserControl的AutoScaleMode设置为Dpi

如果你做了窗体自适应(根据控件的固定大小,按照拉伸比例来拉伸),还需要以下步骤:

(3)添加获取DPI和缩放比例的帮助类(因为网上出处太多了,这里就不写引用了)

  1 public class PrimaryScreenHelper
  2     {
  3         #region Win32 API  
  4         [DllImport("user32.dll")]
  5         static extern IntPtr GetDC(IntPtr ptr);
  6         [DllImport("gdi32.dll")]
  7         static extern int GetDeviceCaps(
  8         IntPtr hdc, // handle to DC  
  9         int nIndex // index of capability  
 10         );
 11         [DllImport("user32.dll", EntryPoint = "ReleaseDC")]
 12         static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc);
 13         #endregion
 14         #region DeviceCaps常量  
 15         const int HORZRES = 8;
 16         const int VERTRES = 10;
 17         const int LOGPIXELSX = 88;
 18         const int LOGPIXELSY = 90;
 19         const int DESKTOPVERTRES = 117;
 20         const int DESKTOPHORZRES = 118;
 21         #endregion
 22 
 23         #region 属性  
 24         /// <summary>  
 25         /// 获取屏幕分辨率当前物理大小  
 26         /// </summary>  
 27         public static Size WorkingArea
 28         {
 29             get
 30             {
 31                 IntPtr hdc = GetDC(IntPtr.Zero);
 32                 Size size = new Size();
 33                 size.Width = GetDeviceCaps(hdc, HORZRES);
 34                 size.Height = GetDeviceCaps(hdc, VERTRES);
 35                 ReleaseDC(IntPtr.Zero, hdc);
 36                 return size;
 37             }
 38         }
 39         /// <summary>  
 40         /// 当前系统DPI_X 大小 一般为96  
 41         /// </summary>  
 42         public static int DpiX
 43         {
 44             get
 45             {
 46                 IntPtr hdc = GetDC(IntPtr.Zero);
 47                 int DpiX = GetDeviceCaps(hdc, LOGPIXELSX);
 48                 ReleaseDC(IntPtr.Zero, hdc);
 49                 return DpiX;
 50             }
 51         }
 52         /// <summary>  
 53         /// 当前系统DPI_Y 大小 一般为96  
 54         /// </summary>  
 55         public static int DpiY
 56         {
 57             get
 58             {
 59                 IntPtr hdc = GetDC(IntPtr.Zero);
 60                 int DpiX = GetDeviceCaps(hdc, LOGPIXELSY);
 61                 ReleaseDC(IntPtr.Zero, hdc);
 62                 return DpiX;
 63             }
 64         }
 65         /// <summary>  
 66         /// 获取真实设置的桌面分辨率大小  
 67         /// </summary>  
 68         public static Size DESKTOP
 69         {
 70             get
 71             {
 72                 IntPtr hdc = GetDC(IntPtr.Zero);
 73                 Size size = new Size();
 74                 size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES);
 75                 size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES);
 76                 ReleaseDC(IntPtr.Zero, hdc);
 77                 return size;
 78             }
 79         }
 80 
 81         /// <summary>  
 82         /// 获取宽度缩放百分比  
 83         /// </summary>  
 84         public static float ScaleX
 85         {
 86             get
 87             {
 88                 IntPtr hdc = GetDC(IntPtr.Zero);
 89                 int t = GetDeviceCaps(hdc, DESKTOPHORZRES);
 90                 int d = GetDeviceCaps(hdc, HORZRES);
 91                 float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES);
 92                 ReleaseDC(IntPtr.Zero, hdc);
 93                 return ScaleX;
 94             }
 95         }
 96         /// <summary>  
 97         /// 获取高度缩放百分比  
 98         /// </summary>  
 99         public static float ScaleY
100         {
101             get
102             {
103                 IntPtr hdc = GetDC(IntPtr.Zero);
104                 float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES);
105                 ReleaseDC(IntPtr.Zero, hdc);
106                 return ScaleY;
107             }
108         }
109 
110         /// <summary>
111         /// 获取高DPI下屏幕的缩放比例
112         /// </summary>
113         public static float ScalePercent
114         {
115             get { return DpiX / 96.0F; }
116         }
117         #endregion
118     }
View Code

(4)UserControl回复为缩放前的大小

 this.lblClassTime.Location = new System.Drawing.Point((int)(8 * PrimaryScreenHelper.ScalePercent), (int)(75 * PrimaryScreenHelper.ScalePercent));
            this.lblClassTime.Size = new System.Drawing.Size((int)(285 * PrimaryScreenHelper.ScalePercent), (int)(22 * PrimaryScreenHelper.ScalePercent));

 

posted @ 2019-05-14 16:02  翻白眼的哈士奇  阅读(4615)  评论(2编辑  收藏  举报