Sign in with Microsoft
Sign in or create an account.
Hello,
Select a different account.
You have multiple accounts
Choose the account you want to sign in with.

本文內容

摘要

裝載 WebBrowser 控制項的 Visual Basic.NET 應用程式可以處理的NewWindow2事件,以攔截window.open呼叫所產生的指令碼。不過,它不立即您的應用程式要如何取得使 WebBrowser 主應用程式可以調整視窗大小正確, window.open的功能引數傳遞的寬度和高度值。本文將告訴您如何取得新的寬度及高度 WebBrowser 控制項,以及如何適當地調整表單大小。

需求

下面清單列出了建議使用的硬體、軟體、網路基礎結構,以及所需安裝的 Service Pack:

  • Microsoft Visual Studio .NET

  • Microsoft Internet Explorer 5.5 Service Pack 2 或更新版本

建立範例

本章節說明如何將 WebBrowser 控制項,在 Visual Basic.NET 應用程式中裝載、 如何處理NewWindow2 WebBrowser 控制項事件,以及如何處理WindowSetWidth和WindowSetHeight事件調整您的應用程式的大小。

  1. 建立新的 Windows 應用程式在 Visual Basic 中如下所示:

    1. 啟動 Visual Studio.NET。

    2. 在 [檔案] 功能表上指向 [新增],然後按一下專案。

    3. 按一下 [專案類型] 下的 [ Visual Basic 專案]。按一下 [範本] 下的 [ Windows 應用程式]。

  2. 在工具箱] 中,按一下 [一般]、 工具箱] 中以滑鼠右鍵按一下,然後按一下自訂工具箱。

  3. 在COM 元件] 索引標籤中,選取Microsoft Web 瀏覽器] 核取方塊,然後按一下[確定]。

  4. 在工具箱] 中,連按兩下 [總管] 視窗中。

  5. 您可以將一個按鈕控制項和TextBox控制項加入表單。

  6. 按兩下按鈕以檢視按鈕的onClick事件的實作程式碼] 視窗中,然後加入下列程式碼:

        Private Sub Button1_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles Button1.Click            AxWebBrowser1.Navigate(TextBox1.Text)    End Sub

    這段程式碼可讓您瀏覽至您在 [文字] 方塊中指定的 URL。

  7. 加入下列程式碼,為NewWindow2撰寫處理常式函式:

        Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, _        ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) _        Handles AxWebBrowser1.NewWindow2            Dim frmWB As Form1            frmWB = New Form1()            frmWB.AxWebBrowser1.RegisterAsBrowser = True            e.ppDisp = frmWB.AxWebBrowser1.Application            frmWB.Visible = True    End Sub
  8. 加入下列程式碼,為WindowSetHeight撰寫處理常式函式:

        Private Sub AxWebBrowser1_WindowSetHeight(ByVal sender As Object, _        ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEvent) _        Handles AxWebBrowser1.WindowSetHeight            Dim heightDiff As Integer            heightDiff = Me.Height - Me.AxWebBrowser1.Height            Me.Height = heightDiff + e.height    End Sub
  9. 加入下列程式碼,為WindowSetWidth撰寫處理常式函式:

        Private Sub AxWebBrowser1_WindowSetWidth(ByVal sender As Object, _        ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetWidthEvent) _        Handles AxWebBrowser1.WindowSetWidth            Dim widthDiff As Integer            widthDiff = Me.Width - Me.AxWebBrowser1.Width            Me.Width = widthDiff + e.width    End Sub

完整的程式碼範例

Public Class Form1    Inherits System.Windows.Forms.Form#Region " Windows Form Designer generated code "    'Omitted#End Region    Private Sub Button1_Click(ByVal sender As System.Object, _        ByVal e As System.EventArgs) Handles Button1.Click            AxWebBrowser1.Navigate(TextBox1.Text)    End Sub    Private Sub AxWebBrowser1_NewWindow2(ByVal sender As Object, _        ByVal e As AxSHDocVw.DWebBrowserEvents2_NewWindow2Event) _        Handles AxWebBrowser1.NewWindow2            'MessageBox.Show(AxWebBrowser1.Height & ":" & AxWebBrowser1.Width)            'MessageBox.Show(doc.body.innerHTML)            Dim frmWB As Form1            frmWB = New Form1()            frmWB.AxWebBrowser1.RegisterAsBrowser = True            'frmWB.AxWebBrowser1.Navigate2("about:blank")            e.ppDisp = frmWB.AxWebBrowser1.Application            frmWB.Visible = True            'MessageBox.Show(frmWB.AxWebBrowser1.Height & ":" & frmWB.AxWebBrowser1.Width)    End Sub    Private Sub AxWebBrowser1_WindowSetHeight(ByVal sender As Object, _        ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEvent) _        Handles AxWebBrowser1.WindowSetHeight            'MessageBox.Show("In SetHeight" & Me.Height & ":" & e.height)            Dim heightDiff As Integer            heightDiff = Me.Height - Me.AxWebBrowser1.Height            Me.Height = heightDiff + e.height    End Sub    Private Sub AxWebBrowser1_WindowSetWidth(ByVal sender As Object, _        ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetWidthEvent) _        Handles AxWebBrowser1.WindowSetWidth            'MessageBox.Show("In SetWidth" & Me.Width & ":" & e.width)            Dim widthDiff As Integer            widthDiff = Me.Width - Me.AxWebBrowser1.Width            Me.Width = widthDiff + e.width    End SubEnd Class

驗證能否運作

  1. 建置應用程式。

  2. 開啟 [記事本]。建立新的檔名為 Test.htm,,然後加入下列程式碼:

    <HTML><HEAD><META NAME="GENERATOR" Content="Microsoft Visual Studio 6.0"><TITLE></TITLE><script>function openWin(){var win;win = window.open("http://www.microsoft.com","blah","width=600, height=600");}</script></HEAD><BODY><button onClick=openWin()>Open Window</button></BODY></HTML>
  3. 儲存您的 Web 伺服器上的 Test.htm。

  4. 執行應用程式。

  5. 瀏覽至 [Test.htm] 頁面上,,,然後按一下 [[] 按鈕。請注意,Microsoft 公司網站會開啟新的執行個體的應用程式中。表單會根據您對window.open的呼叫與傳遞的功能來調整大小。

參考

如需詳細資訊,按一下下面的文件編號,檢視 「 Microsoft 知識庫 」 中的文件:

HOW TO: 處理在 Visual Basic.NET 應用程式中的文件事件

如何: 取得寬度,並提供給 [Window.open 內部 Visual C++ WebBrowser 主應用程式的高度如需有關 WebBrowser 控制項,以及方法的詳細資訊,屬性和事件公開的話,請參閱至下列 Microsoft 網站:

Visual Basic 開發人員參考 WebBrowser 控制項:如需有關 Microsoft Internet Explorer 開發以 Web 為基礎的解決方案的詳細資訊,請造訪下列 Microsoft 網站:

Need more help?

Want more options?

探索訂閱權益、瀏覽訓練課程、瞭解如何保護您的裝置等等。

社群可協助您詢問並回答問題、提供意見反應,以及聆聽來自具有豐富知識的專家意見。

Was this information helpful?

How satisfied are you with the translation quality?
What affected your experience?
By pressing submit, your feedback will be used to improve Microsoft products and services. Your IT admin will be able to collect this data. Privacy Statement.

Thank you for your feedback!

×