Microsoft KB Archive/311290

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

Article ID: 311290

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual Basic .NET 2002 Standard Edition
 * Microsoft Visual Basic .NET 2003 Standard Edition

-



This article was previously published under Q311290





For a Microsoft Visual C# .NET version of this article, see 313966.

IN THIS TASK
SUMMARY
 * Requirements
 * Create the Sample
 * Complete Code Sample
 * Verify That It Works

REFERENCES



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.

back to the top

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

back to the top

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.  Create a new Windows application in Visual Basic as follows:  Start Visual Studio .NET. On the File menu, point to New, and then click Project. Under Project Types, click Visual Basic Projects. Under Templates, click Windows Application.  In the toolbox, click General, right-click in the toolbox, and then click Customize Toolbox.</li> On the COM Components tab, select the Microsoft Web Browser check box, and then click OK.</li> In the toolbox, double-click in the Explorer window.</li> Add a Button control and a TextBox control to your form.</li>  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. </li>  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 </li>  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 </li>  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 </li></ol>

back to the top

Complete Code Sample
Public Class Form1 Inherits System.Windows.Forms.Form

'Omitted
 * 1) Region &quot; Windows Form Designer generated code &quot;
 * 1) 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 & &quot;:&quot; & AxWebBrowser1.Width)

'MessageBox.Show(doc.body.innerHTML) Dim frmWB As Form1 frmWB = New Form1

frmWB.AxWebBrowser1.RegisterAsBrowser = True 'frmWB.AxWebBrowser1.Navigate2(&quot;about:blank&quot;) e.ppDisp = frmWB.AxWebBrowser1.Application frmWB.Visible = True 'MessageBox.Show(frmWB.AxWebBrowser1.Height & &quot;:&quot; & frmWB.AxWebBrowser1.Width) End Sub

Private Sub AxWebBrowser1_WindowSetHeight(ByVal sender As Object, _       ByVal e As AxSHDocVw.DWebBrowserEvents2_WindowSetHeightEvent) _ Handles AxWebBrowser1.WindowSetHeight 'MessageBox.Show(&quot;In SetHeight&quot; & Me.Height & &quot;:&quot; & 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(&quot;In SetWidth&quot; & Me.Width & &quot;:&quot; & e.width) Dim widthDiff As Integer widthDiff = Me.Width - Me.AxWebBrowser1.Width Me.Width = widthDiff + e.width End Sub

End Class back to the top

Verify That It Works
 Build the application.</li>  Open Notepad. Create a new file named Test.htm, and then add the following code: <HTML> <HEAD> <META NAME=&quot;GENERATOR&quot; Content=&quot;Microsoft Visual Studio 6.0&quot;> <TITLE></TITLE> function openWin {       var win; win = window.open(&quot;http://www.microsoft.com&quot;,&quot;blah&quot;,&quot;width=600, height=600&quot;); } </HEAD> <BODY> <button onClick=openWin>Open Window </BODY> </HTML> </li> Save Test.htm on your Web server.</li> Run the application.</li> 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.</li></ol>

back to the top

<div class="references_section">