目录
问题现象
如图所示:在高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和缩放比例的帮助类(因为网上出处太多了,这里就不写引用了)
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
}
(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));