WinFrom嵌入使用CefSharp(浏览器)

提示:CefSharp自5.2版本开始就支持AnyCpu,CefSharp需要安装的版本可对照自己.net版本安装。对照网址:GitHub - cefsharp/CefSharp: .NET (WPF and Windows Forms) bindings for the Chromium Embedded Frameworkicon-default.png?t=N7T8https://github.com/cefsharp/CefSharp

推荐vs2015以上

一、首先安装CefSharp.WinForms,如下图:

二、安装CefSharp.Common,安装此包后会自动安装cef.redist.x64和cef.redist.x86,如果没有请手动安装。

以上的包都必须得装。

三、主要代码,下

面的代码只能执行一次,多次执行会报错。所以可放在program中。

先修改csproj文件

<PropertyGroup>
    <CefSharpAnyCpuSupport>true</CefSharpAnyCpuSupport>
</PropertyGroup>

 然后在program中添加此代码,也不是必须放在这里。只需要确保此代码在同一个线程中只能执行一次,随便放在哪里均可。

[STAThread]
        static void Main()
        {
            Application.EnableVisualStyles();
            #region 加载chrome浏览器,只需要加此部分代码

            AppDomain.CurrentDomain.AssemblyResolve += Resolver;
            LoadApp();

            #endregion

            Application.SetCompatibleTextRenderingDefault(false);
            Application.Run(new srLogin());
        }
        private static void LoadApp()
        {// 构建 x86 或 x64 子目录的路径
            string architecture = Environment.Is64BitProcess ? "x64" : "x86";
            string browserSubprocessPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase, architecture, "CefSharp.BrowserSubprocess.exe");

            CefSettings settings = new CefSettings
            {
                Locale = "zh-CN",
                BrowserSubprocessPath = browserSubprocessPath
            };
            Cef.Initialize(settings, performDependencyCheck: false, browserProcessHandler: null);
        }

        // Will attempt to load missing assembly from either x86 or x64 subdir
        private static Assembly Resolver(object sender, ResolveEventArgs args)
        {
            if (args.Name.StartsWith("CefSharp"))
            {
                string assemblyName = args.Name.Split(new[] { ',' }, 2)[0] + ".dll";
                string archSpecificPath = Path.Combine(AppDomain.CurrentDomain.SetupInformation.ApplicationBase,
                                                       Environment.Is64BitProcess ? "x64" : "x86",
                                                       assemblyName);

                return File.Exists(archSpecificPath)
                           ? Assembly.LoadFile(archSpecificPath)
                           : null;
            }
            return null;
        }

四、控件使用

在Nuget中安装包之后,工具箱中会出现Cefsharp的控件ChromiumWebBrowser。直接拖入自己的窗体中即可。如下图:

添加好窗体,使用控件的Load属性即可设置访问的路径。chromiumWebBrowser.Load(url);

特殊情况:运行时若报错,找不到文件则需要将debug文件下面的x86和x64两个文件夹复制到项目跟目录下,属性中需要选择嵌入,发布后运行时才可以找到文件。

注:转载请注明出处。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
WinForm嵌入VLC播放器可以通过以下步骤实现: 首先,你需要下载并安装VLC播放器到你的计算机上。确保安装的版本与你的WinForm项目兼容。 接下来,创建一个新的WinForm应用程序。打开Visual Studio并选择创建一个新的WinForm项目。 在你的WinForm设计窗口中,确保工具箱窗格是可见的。如果不可见,可以在视图菜单中选择工具箱。 在工具箱中找到“Elements”选项,并将其拖动到你的WinForm窗口上。这是嵌入VLC播放器所需要的控件。 在代码视图中,找到你的Form的构造函数,并在其中添加代码,将VLC控件添加到你的WinForm窗口上。 使用以下代码: ``` VlcControl vlcControl = new VlcControl(); this.Controls.Add(vlcControl); vlcControl.Dock = DockStyle.Fill; ``` 然后,你需要设置VLC播放器的路径。使用以下代码,将VLC播放器库的路径设置为你所安装的位置: ``` vlcControl.VlcLibDirectoryNeeded += (sender, args) => { vlcControl.VlcLibDirectory = new DirectoryInfo(@"VLC安装路径"); }; ``` 在以上代码中,将“VLC安装路径”替换为您所安装的VLC播放器的实际路径。 现在,你可以通过设置`vlcControl.Source`属性来加载媒体文件到VLC播放器中。例如: ``` vlcControl.Source = new Uri("媒体文件路径"); ``` 将“媒体文件路径”替换为你要加载的媒体文件的实际路径。 最后,编译并运行你的WinForm应用程序。现在,你的VLC播放器已成功嵌入WinForm窗口中。 请注意,为了正确运行VLC播放器,你的计算机上必须安装有VLC播放器。如果其他用户在没有安装VLC的计算机上运行你的应用程序,他们需要自行安装VLC播放器。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值