C#使用cefsharp开发winform程序版的浏览器

C# WinForm程序使用 CefSharp开发浏览器 

创建一个基础的Winform应用,并使用NuGet引用CefSharp包。使用Nuget添加引用,搜索CefSharp,添加CefSharp.WinForm,CefSharp.Winform依赖好几个包,这块选择这一个安装就可以了,NuGet会自动帮你把其他依赖的包一并下载好的。

null

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

让整个程序支持高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));

直接运行程序,会报错

 error : CefSharp.Common contains unmanaged resoures, set your project and solution platform to x86 or x64. Alternatively for AnyCPU Support see https://github.com/cefsharp/CefSharp/issues/1714

需要指定运行的平台,在解决方案右键,选择配置管理器

在活动解决方案平台选择新建,新建一个x86的平台,再次点击启动就没有报错了

© 版权声明
THE END
喜欢就支持一下吧
点赞5 分享
评论 抢沙发
头像
欢迎您留下宝贵的见解!
提交
头像

昵称

取消
昵称表情代码图片

    暂无评论内容