CefSharp 在Winform中有黑边的解决方法
存在的问题如下:
解决方法1:将cefsharp的gpu设置为无效,在Program.cs中修改,或者在构造cefsharp之前做如下修改(后遗症,h5动画会出现卡顿现象,慎用):
var settings = new CefSettings { Locale = "zh-CN" }; settings.CefCommandLineArgs.Add("disable-gpu", "1"); Cef.Initialize(settings);
解决方法2:支持高DPI(后遗症,winform窗体会缩放)
Cef.EnableHighDPISupport();
解决方法3:让整个程序支持高DPI(最优解决方案):
(1)为项目添加应用程序清单文件(app.manifest),创建app.manifest程序清单文件
(2)取消下面行的注释
<application xmlns="urn:schemas-microsoft-com:asm.v3"> <windowsSettings> <dpiAware xmlns="http://schemas.microsoft.com/SMI/2005/WindowsSettings">true</dpiAware> </windowsSettings> </application>
(3)将Form和UserControl的AutoScaleMode设置为Dpi
如果你做了窗体自适应(根据控件的固定大小,按照拉伸比例来拉伸),还需要以下步骤:
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));
添加获取DPI和缩放比例的帮助类
public class PrimaryScreenHelper { #region Win32 API [DllImport("user32.dll")] static extern IntPtr GetDC(IntPtr ptr); [DllImport("gdi32.dll")] static extern int GetDeviceCaps( IntPtr hdc, // handle to DC int nIndex // index of capability ); [DllImport("user32.dll", EntryPoint = "ReleaseDC")] static extern IntPtr ReleaseDC(IntPtr hWnd, IntPtr hDc); #endregion #region DeviceCaps常量 const int HORZRES = 8; const int VERTRES = 10; const int LOGPIXELSX = 88; const int LOGPIXELSY = 90; const int DESKTOPVERTRES = 117; const int DESKTOPHORZRES = 118; #endregion #region 属性 /// <summary> /// 获取屏幕分辨率当前物理大小 /// </summary> public static Size WorkingArea { get { IntPtr hdc = GetDC(IntPtr.Zero); Size size = new Size(); size.Width = GetDeviceCaps(hdc, HORZRES); size.Height = GetDeviceCaps(hdc, VERTRES); ReleaseDC(IntPtr.Zero, hdc); return size; } } /// <summary> /// 当前系统DPI_X 大小 一般为96 /// </summary> public static int DpiX { get { IntPtr hdc = GetDC(IntPtr.Zero); int DpiX = GetDeviceCaps(hdc, LOGPIXELSX); ReleaseDC(IntPtr.Zero, hdc); return DpiX; } } /// <summary> /// 当前系统DPI_Y 大小 一般为96 /// </summary> public static int DpiY { get { IntPtr hdc = GetDC(IntPtr.Zero); int DpiX = GetDeviceCaps(hdc, LOGPIXELSY); ReleaseDC(IntPtr.Zero, hdc); return DpiX; } } /// <summary> /// 获取真实设置的桌面分辨率大小 /// </summary> public static Size DESKTOP { get { IntPtr hdc = GetDC(IntPtr.Zero); Size size = new Size(); size.Width = GetDeviceCaps(hdc, DESKTOPHORZRES); size.Height = GetDeviceCaps(hdc, DESKTOPVERTRES); ReleaseDC(IntPtr.Zero, hdc); return size; } } /// <summary> /// 获取宽度缩放百分比 /// </summary> public static float ScaleX { get { IntPtr hdc = GetDC(IntPtr.Zero); int t = GetDeviceCaps(hdc, DESKTOPHORZRES); int d = GetDeviceCaps(hdc, HORZRES); float ScaleX = (float)GetDeviceCaps(hdc, DESKTOPHORZRES) / (float)GetDeviceCaps(hdc, HORZRES); ReleaseDC(IntPtr.Zero, hdc); return ScaleX; } } /// <summary> /// 获取高度缩放百分比 /// </summary> public static float ScaleY { get { IntPtr hdc = GetDC(IntPtr.Zero); float ScaleY = (float)(float)GetDeviceCaps(hdc, DESKTOPVERTRES) / (float)GetDeviceCaps(hdc, VERTRES); ReleaseDC(IntPtr.Zero, hdc); return ScaleY; } } /// <summary> /// 获取高DPI下屏幕的缩放比例 /// </summary> public static float ScalePercent { get { return DpiX / 96.0F; } } #endregion }