共用方式為


HOW TO:使用 WebBrowser 控制項列印

更新:2007 年 11 月

下列程式碼範例會示範如何使用 WebBrowser 控制項,不必顯示 Web 網頁就能直接列印。

範例

Private Sub PrintHelpPage()

    ' Create a WebBrowser instance. 
    Dim webBrowserForPrinting As New WebBrowser()

    ' Add an event handler that prints the document after it loads.
    AddHandler webBrowserForPrinting.DocumentCompleted, New _
        WebBrowserDocumentCompletedEventHandler(AddressOf PrintDocument)

    ' Set the Url property to load the document.
    webBrowserForPrinting.Url = New Uri("\\myshare\help.html")

End Sub

Private Sub PrintDocument(ByVal sender As Object, _
    ByVal e As WebBrowserDocumentCompletedEventArgs)

    Dim webBrowserForPrinting As WebBrowser = CType(sender, WebBrowser)

    ' Print the document now that it is fully loaded.
    webBrowserForPrinting.Print()
    MessageBox.Show("print")

    ' Dispose the WebBrowser now that the task is complete. 
    webBrowserForPrinting.Dispose()

End Sub
private void PrintHelpPage()
{
    // Create a WebBrowser instance. 
    WebBrowser webBrowserForPrinting = new WebBrowser();

    // Add an event handler that prints the document after it loads.
    webBrowserForPrinting.DocumentCompleted +=
        new WebBrowserDocumentCompletedEventHandler(PrintDocument);

    // Set the Url property to load the document.
    webBrowserForPrinting.Url = new Uri(@"\\myshare\help.html");
}

private void PrintDocument(object sender,
    WebBrowserDocumentCompletedEventArgs e)
{
    // Print the document now that it is fully loaded.
    ((WebBrowser)sender).Print();

    // Dispose the WebBrowser now that the task is complete. 
    ((WebBrowser)sender).Dispose();
}

編譯程式碼

這項範例需要:

  • System 和 System.Windows.Forms 組件的參考。

請參閱

工作

HOW TO:使用 WebBrowser 控制項巡覽至 URL

HOW TO:將 Web 瀏覽器功能加入至 Windows Form 應用程式

HOW TO:在 Windows Form 應用程式中建立 HTML 文件檢視器

概念

WebBrowser 安全性

參考

WebBrowser

Print

Url

WebBrowser 控制項概觀