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));

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM