注意:需要另外从NuGet上安装CefSharp.Offscreen
的包包
Imports CefSharp
Imports CefSharp.OffScreen
Public Class offscreen
Private browser As ChromiumWebBrowser
Public Sub New()
InitializeComponent()
CheckForIllegalCrossThreadCalls = False
Dim settings As New CefSettings() With {
.Locale = "zh-CN"
CefSharp.Cef.Initialize(settings)
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
browser = New ChromiumWebBrowser("mail.qq.com")
AddHandler browser.LoadingStateChanged, AddressOf doneLoad
Catch ex As Exception
TextBox1.Text = ex.Message
End Try
End Sub
Private Async Sub doneLoad(ByVal sender As Object, ByVal e As LoadingStateChangedEventArgs)
If Not e.IsLoading Then
RemoveHandler browser.LoadingStateChanged, AddressOf doneLoad
Threading.Thread.Sleep(1000)
Dim html As String = Await browser.GetSourceAsync()
TextBox1.Text = html
End If
End Sub
End Class
复制代码