打印包含在其中的Web浏览器控件后关闭表单

时间:2015-05-06 13:32:59

标签: vb.net winforms printing webbrowser-control

我正在通过vb.net winforms中的html和web浏览器控件进行随机打印 这是我的代码

Public Class Form1

  Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    Dim myWebBrowser As New WebBrowser
    AddHandler myWebBrowser.DocumentCompleted, AddressOf DocumentCompleted
    myWebBrowser.Navigate("http://www.bing.com")

  End Sub

  Private Sub DocumentCompleted(ByVal sender As Object, ByVal e As WebBrowserDocumentCompletedEventArgs)

    With DirectCast(sender, WebBrowser)
      If .ReadyState = WebBrowserReadyState.Complete Then
        .Print()
      End If
    End With
  End Sub

End Class

我希望打印后关闭表单。现在,如果我在Me.Close()之后写下.Print(),则不会打印任何内容。我该怎么做才能实现这个目标?

感谢任何帮助。

::更新::

在@ Noseratio的建议之后,我尝试在html中处理事件onafterprint并尝试使用设置为我的表单的Me.Close()来调用ObjectFoprScripting。但这就是在没有任何印刷品的情况下触发封闭方法。

这是我的代码

  我的html页面中的

脚本标记

<script>
    function window.onafterprint() {
        window.external.Test('called from script code');
    }
</script>
  

VB.net我的表格代码

Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.AllowWebBrowserDrop = False
        WebBrowser1.IsWebBrowserContextMenuEnabled = False
        WebBrowser1.WebBrowserShortcutsEnabled = False
        webBrowser1.ObjectForScripting = Me
        WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
    End Sub

    Public Sub Test(ByVal message As String)
        MessageBox.Show(message, "client code")
        Me.BeginInvoke(DirectCast(Sub() Me.Close(), MethodInvoker))
    End Sub

    Private Sub WebBrowser1_DocumentCompleted(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        WebBrowser1.Print()
    End Sub
End Class

1 个答案:

答案 0 :(得分:1)

找到我的解决方案

无需在javascript中处理onafterprint 这是我做的,

第1步
在我的项目中添加了对SHDocVw.dll的引用。这可以在c:\windows\system32文件夹中找到。

Step2

  

我的新更新代码

Imports System.IO
Imports Microsoft.Win32
Imports System.Security.Permissions

<PermissionSet(SecurityAction.Demand, Name:="FullTrust")> _
<System.Runtime.InteropServices.ComVisibleAttribute(True)> _
Public Class Form1
    Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
        WebBrowser1.AllowWebBrowserDrop = False
        WebBrowser1.IsWebBrowserContextMenuEnabled = False
        WebBrowser1.WebBrowserShortcutsEnabled = False
        WebBrowser1.DocumentText = File.ReadAllText("localprint.htm")
    End Sub

    Private Sub WebBrowser1_DocumentCompleted_1(sender As System.Object, e As System.Windows.Forms.WebBrowserDocumentCompletedEventArgs) Handles WebBrowser1.DocumentCompleted
        Dim wb As WebBrowser = TryCast(sender, WebBrowser)

        Dim ie As SHDocVw.InternetExplorer = DirectCast(wb.ActiveXInstance, SHDocVw.InternetExplorer)
        AddHandler ie.PrintTemplateInstantiation, AddressOf IE_OnPrintTemplateInstantiation
        AddHandler ie.PrintTemplateTeardown, AddressOf IE_OnPrintTemplateTeardown

        'Just to get reference of  the webBrowser1 control in ie events, uncomment the below line
        'ie.PutProperty("WebBrowserControl", DirectCast(wb, Object))

        wb.Print()
    End Sub

    Private Sub IE_OnPrintTemplateInstantiation(pDisp As Object)
        ' The PrintTemplateInstantiation event is fired when the print job is starting.
    End Sub

    Private Sub IE_OnPrintTemplateTeardown(pDisp As Object)
        ' The PrintTemplateTeardown event is fired when the print job is done.

        'Just to get reference of  the webBrowser1 control, uncomment the below line
        'Dim iwb2 As SHDocVw.IWebBrowser2 = TryCast(pDisp, SHDocVw.IWebBrowser2)
        'Dim wb As WebBrowser = DirectCast(iwb2.GetProperty("WebBrowserControl"), WebBrowser)

        Me.Close()

    End Sub
End Class