HOW TO: Get Width and Height from window.open() Inside a WebBrowser Host by Using Visual Basic .NET

Summary

Visual Basic .NET applications that host the WebBrowser control can handle the NewWindow2 event to catch a window.open call that is generated by script. However, it is not immediately obvious how your application can obtain the width and height values that are passed to the features argument of window.open so that the WebBrowser host can resize the window correctly. This article demonstrates how to obtain the new width and height of the WebBrowser control and how to resize your form accordingly.

Requirements

The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
  • Microsoft Visual Studio .NET
  • Microsoft Internet Explorer 5.5 Service Pack 2 or later

Create the Sample

This section describes how to host the WebBrowser control in a Visual Basic .NET application, how to handle the NewWindow2 event of the WebBrowser control, and then how to handle the WindowSetWidth and the WindowSetHeight events to resize your application.
  1. Create a new Windows application in Visual Basic as follows:
    1. Start Visual Studio .NET.
    2. On the File menu, point to New, and then click Project.
    3. Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application.
  2. In the toolbox, click General, right-click in the toolbox, and then click Customize Toolbox.
  3. On the COM Components tab, select the Microsoft Web Browser check box, and then click OK.
  4. In the toolbox, double-click in the Explorer window.
  5. Add a Button control and a TextBox control to your form.
  6. Double-click the button to view the implementation of the onClick event of the button in the Code window, and then add the following code:
        Private Sub Button1_Click(ByVal sender As System.Object, _
    ByVal e As System.EventArgs) Handles Button1.Click
    AxWebBrowser1.Navigate(TextBox1.Text)
    End Sub
    This code allows you to browse to the URL that you specify in the text box.
  7. Add the following code to write the handler function for 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. Add the following code to write the handler function for 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. Add the following code to write the handler function for 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

Complete Code Sample

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 Sub

End Class

Verify That It Works

  1. Build the application.
  2. Open Notepad. Create a new file named Test.htm, and then add the following code:
    <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. Save Test.htm on your Web server.
  4. Run the application.
  5. Browse to the Test.htm page, and then click the button. Notice that the Microsoft Corporate Web site opens in a new instance of the application. The form is resized according to the features that you passed with the call to window.open.

References

For additional information, click the article numbers below to view the articles in the Microsoft Knowledge Base:
311284 HOW TO: Handle Document Events in a Visual Basic .NET Application
259963 HOWTO: Obtain Width and Height Supplied to Window.open Inside the Visual C++ WebBrowser Host
For more information about the WebBrowser control, as well as the methods, the properties, and the events that it exposes, refer to the following Microsoft Web site:
WebBrowser Control: Reference for Visual Basic Developers
http://msdn2.microsoft.com/en-us/library/Aa752043.aspx
For more information about developing Web-based solutions for Microsoft Internet Explorer, visit the following Microsoft Web sites:
Propriedades

ID do Artigo: 311290 - Última Revisão: 13 de mai de 2007 - Revisão: 1

Comentários