c# - 是否有任何在 Windows 窗体应用程序中以最少设置使用 CefGlue 或 CefSharp 的示例?

标签 c# winforms webkit cefsharp

我(仍在)使用 Visual Studio 2005 并希望在 c# winforms 应用程序中嵌入 webkit 浏览器,最好是作为 winforms 控件。

我正在寻找一个简单的 CefGlue 或 CefSharp 示例来开始使用,以及最少的必要 dll。我无法理解 GitHub 上的 CefSharp 示例。

最佳答案

这很容易,但很遗憾地记录在案。

为了使其正常工作,我制作了一个新的表单应用程序并向我的表单添加了一个 toolstripContainer。还在我的项目中添加了对 CefSharp.dll 和 CefSharp.WinForms.dll 的引用。

这是我的类(class)代码:

public partial class frmBrowser : Form, IRequestHandler
{
    private readonly WebView web_view;

    public frmBrowser()
    {
        InitializeComponent();
        web_view = new WebView("http://stackoverflow.com", new BrowserSettings());
        web_view.Dock = DockStyle.Fill; 
        web_view.RequestHandler = this;
        tsContainer.ContentPanel.Controls.Add(web_view);
    }

    #region IRequestHandler Members

    bool IRequestHandler.OnBeforeBrowse(IWebBrowser browser, IRequest request,
                               NavigationType naigationvType, bool isRedirect)
    {
        System.Diagnostics.Debug.WriteLine("OnBeforeBrowse");
        return false;
    }

    bool IRequestHandler.OnBeforeResourceLoad(IWebBrowser browser,
                                     IRequestResponse requestResponse)
    {
        System.Diagnostics.Debug.WriteLine("OnBeforeResourceLoad");
        IRequest request = requestResponse.Request;

        if (request.Url.EndsWith("header.png"))
        {
            MemoryStream stream = new System.IO.MemoryStream();

            FileStream file = new FileStream(@"C:\tmp\header.png", FileMode.Open, FileAccess.Read, FileShare.Read);
            byte[] bytes = new byte[file.Length];
            file.Read(bytes, 0, (int)file.Length);
            stream.Write(bytes, 0, (int)file.Length);
            file.Close();

            requestResponse.RespondWith(stream, "image/png");
        }

        return false;
    }

    void IRequestHandler.OnResourceResponse(IWebBrowser browser, string url,
                                   int status, string statusText,
                                   string mimeType, WebHeaderCollection headers)
    {
        System.Diagnostics.Debug.WriteLine("OnResourceResponse");
    }

    #endregion
}

带有请求处理程序的区域是可选的,那是您想要影响调用的时候。在我的示例中,我将对标题图像的调用重新路由到我的 C 驱动器上的图像。

这就是您需要的代码。您还需要将以下文件添加到可执行文件的文件夹中:

  • avcodec-54.dll
  • avformat-54.dll
  • avutil-51.dll
  • chrome.pak
  • icudt.dll
  • libcef.dll
  • libEGL.dll
  • libGLESv2.dll
  • 语言环境文件夹

其中一些文件是可选的,具体取决于您要对它们执行的操作,但您可以通过谷歌搜索。

关于c# - 是否有任何在 Windows 窗体应用程序中以最少设置使用 CefGlue 或 CefSharp 的示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12957930/

相关文章:

c# - ASP.NET 页面跟踪结果

c# - 使用ajax将值传递给 Controller

c# - Nopcommerce 重写路由以通过 CustomView Engine 和 Routeprovider 否决核心目录 Controller

qt - QWebView 禁用右键单击文本选择

iphone - 是否可以防止浏览器中 iPhone/iPad 方向发生变化?

javascript - 使用 JavaScript 在浏览器中检测 Android 手机的旋转

C#/WPF 文本框错误 : "Value ' ' cannot be converted", TargetNull 不工作

c# - 确定正在运行的应用程序的类型 (.NET)

c# - DataVisualization - ILSpy 中的空函数体

c# - 使用 C# winform 的 zeromq pub/sub 示例