CEFSharp (Chromium Embedded Framework) in Interop

2020-06-28 08:58发布

I am trying to use the CefSharp CLR bindings for the Chromium Embedded Framework to create an alternative web browser control that we can embed into an application that only supports legacy ActiveX controls (WonderWare InTouch HMI) in an attempt to gain some HTML5 support for some reeaaallly old machines.

So far, I've created an Interop User Control using the Microsoft InteropForms Toolkit and embedded a CefSharp.WinForms WebView onto the control in a pretty bare-bones manner, i.e.

Private Sub WebControl_Load(sender As Object, e As EventArgs) Handles Me.Load

    Dim Settings = New CefSharp.Settings
    If (CEF.Initialize(Settings)) Then
        WebView = New WebView(homepage, New BrowserSettings())
        WebView.Dock = DockStyle.Fill
    End If

    Me.Controls.Add(WebView)

End Sub

When I build the DLL, I register it via the command

regasm /tlb /codebase Cefsharp.WebControl.dll

This seems to work for the most part, but I am having some issues that I do not fully understand. I am testing it with the VBA Forms in Excel (purely out of convenience) and when I am using the Form Builder, it actually initializes the control when it is added to the user form and loads the webpage perfectly:

IDE Example

However, when I try to show the UseForm it seems to either destroy the handle or the instance of the webview (or both) and I can't figure out how to fix it. Once I try to show the form and try something like

Sub Test()
    WebForm.Show
    WebForm.WebControl.Navigate "www.google.com"
End Sub

I get an error that the object doesn't exist. I've messed around a bit and sometimes I get different errors like the pointer is invalid but so far I haven't figured out how to set it up properly. I think it is a problem with handling the User Form events, but I am not sure.

If anyone has any insight on this, I'd greatly appreciate it. Thanks!

1条回答
三岁会撩人
2楼-- · 2020-06-28 09:33

Okay, I figured it out! I think it partly had to do with the handler and partly had to do with how/when I was initializing the webview. The solution I went with ended up using the CefSharp.Wpf instead of the Windows Form, but it's mostly the same. The main difference is adding the initialization of an ElementHost control on the WebControl Designer and create the new WebView as part of the component initialization:

'Do not change this subroutine in the code editor. Use the designer.
<System.Diagnostics.DebuggerStepThrough()> _
Private Sub InitializeComponent()
    Me.WebHost = New System.Windows.Forms.Integration.ElementHost()
    Me.SuspendLayout
    '
    'WebHost
    '
    Me.WebHost.Dock = System.Windows.Forms.DockStyle.Fill
    Me.WebHost.Location = New System.Drawing.Point(0, 0)
    Me.WebHost.Name = "WebHost"
    Me.WebHost.Size = New System.Drawing.Size(404, 244)
    Me.WebHost.TabIndex = 0
    Me.WebHost.Text = "WebHost"
    Me.WebHost.Child = New WebView(homepage, New CefSharp.BrowserSettings)
    '
    'WebControl
    '
    Me.AutoScaleDimensions = New System.Drawing.SizeF(6!, 13!)
    Me.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font
    Me.Controls.Add(Me.WebHost)
    Me.Name = "WebControl"
    Me.Size = New System.Drawing.Size(404, 244)
    Me.ResumeLayout(false)

End Sub

Friend WithEvents WebHost As System.Windows.Forms.Integration.ElementHost
查看更多
登录 后发表回答