我正在从SkyVE s www.GeckoFx -而不是WebBrowser-,但自Michel van Engelens/到这一问题上,我可以在此补充我的想法。 也可适用于网上浏览器。
最重要的部分是将印刷呼吁与文件汇编活动分开——我只是在这里用一个时间。
Private Sub btnPrint_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnPrint.Click
Try
Dim wb As New Skybound.Gecko.GeckoWebBrowser
AddHandler wb.DocumentCompleted, AddressOf PrintWebBrowser_DocumentCompleted
AddHandler wb.HandleCreated, AddressOf PrintWebBrowser_HandleCreated
wb.CreateControl()
Catch ex As Exception
MsgBox(String.Format("Error trying to create GeckoWebBrowser: {0}", ex.Message))
End Try
End Sub
Private Sub PrintWebBrowser_HandleCreated(ByVal sender As Object, ByVal e As System.EventArgs)
Dim wb = DirectCast(sender, Skybound.Gecko.GeckoWebBrowser)
wb.Navigate(GeckoWebBrowser1.Url.AbsoluteUri)
End Sub
Private Sub PrintWebBrowser_DocumentCompleted(ByVal sender As Object, ByVal e As System.EventArgs)
Dim wb = DirectCast(sender, Skybound.Gecko.GeckoWebBrowser)
If wb.Url IsNot Nothing AndAlso wb.Url.AbsoluteUri = "about:blank" Then Exit Sub
Dim tmr As New Timer
tmr.Interval = 200
AddHandler tmr.Tick, AddressOf TimerTick
tmr.Tag = wb
tmr.Start()
End Sub
Private Sub TimerTick(ByVal sender As Object, ByVal e As EventArgs)
Dim tmr As Timer = DirectCast(sender, Timer)
tmr.Stop()
Dim wb As Skybound.Gecko.GeckoWebBrowser = Nothing
Try
wb = DirectCast(tmr.Tag, Skybound.Gecko.GeckoWebBrowser)
wb.Window.Print()
Catch comEx As Runtime.InteropServices.COMException
Dim hresult As Integer = Runtime.InteropServices.Marshal.GetHRForException(comEx)
If hresult = &H80004004 Then // EX_ABORT
MsgBox("Print cancelled")
Else
MsgBox(String.Format("Print failed: {0}", comEx.Message))
End If
Catch ex As Exception
MsgBox(String.Format("Print failed: {0}", ex.Message))
Finally
If wb IsNot Nothing Then wb.Dispose()
End Try
End Sub