Microsoft KB Archive/815719

= How to obtain the width value and the height value from the Window.open method of a WebBrowser control by using Visual C++ .NET or Visual C++ 2005 =

Article ID: 815719

Article Last Modified on 5/13/2007

-

APPLIES TO


 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft Visual C++ 2005 Express Edition

-







For a Microsoft Visual Basic .NET version of this article, see 311290.



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

This article refers to the following Microsoft .NET Framework Class Library namespaces:
 * System::ComponentModel
 * System::Collections
 * System::Windows::Forms
 * System::Windows::Forms::AxHost

IN THIS TASK

 * SUMMARY
 * Requirements
 * Create the sample
 * Verify that the application works
 * REFERENCES



SUMMARY
In a Microsoft Visual C++ .NET or Microsoft Visual C++ .NET application, the WebBrowser control navigates to an .html page that calls the window.open method to open a new window. The WebBrowser control can handle the NewWindow2 event to catch the window.open call that the script generates. Sometimes, you may want to resize the width and the height of the application window according to the width and height values that are passed to the features argument of the window.open method in the script.

This step-by-step article describes how to obtain the new width and height values of the WebBrowser control. It also describes how to resize your form by using these values.

back to the top

Requirements
The following list outlines the recommended hardware, software, network infrastructure, and service packs that you need:
 * Microsoft Visual C++ .NET 2003 or Microsoft Visual C++ 2005
 * Microsoft .NET Framework 1.1
 * Microsoft Internet Explorer 5.5 Service Pack 2 or later

This article assumes that you are familiar with the following topics:
 * Programming by using Visual C++ .NET 2003 or Microsoft Visual C++ 2005
 * Hosting a WebBrowser control (Shdocvw.dll)

back to the top

Create the sample
This section describes how to host the WebBrowser control in a Visual C++ .NET application, how to handle the NewWindow2 event of the WebBrowser control, and how to handle the WindowSetWidth and the WindowSetHeight events to resize your application window.

To do this, follow these steps:  Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005. On the File menu, point to New, and then click Project.

The New Project dialog box appears. Under Project Types, click Visual C++ Projects.

Note In Microsoft Visual Studio 2005, click Visual C++ under Project Types. Under Templates, click Windows Forms Application (.NET). In the Name box, type ResizeForm, and then click OK.

By default, the Form1 form is created. In the toolbox, click the Windows Forms tab.</li> Right-click the toolbox, and then click Add/Remove Items.

The Customize Toolbox dialog box appears.</li> In the Customize Toolbox dialog box, click the COM Components tab.

Note In Microsoft Visual Studio 2005, right-click the toolbox, and then click Choose Items.</li> In the COM components list, locate the component that is named Microsoft Web Browser that points to Shdocvw.dll in your System32 folder.</li> Click to select the Microsoft Web Browser check box, and then click OK.

A new control that is named Microsoft Web Browser appears on the Windows Forms tab in the toolbox. This is the WebBrowser control that you can add to the form.</li> Double-click the Microsoft Web Browser control to add a Microsoft Web Browser control to the Form1 form.</li> Resize the Form1 form and the WebBrowser control.</li> Double-click the Button control to add a Button control to the Form1 form.</li> Double-click the TextBox control to add a TextBox control to the Form1 form.</li> Double-click the button1Button control on the Form1 form.

This step creates a button1_Click event handler in the Form1 class and puts the insertion point at the first line of the event handler.</li>  Add the following code in the button1_Click event handler. Object *Nullobj = NULL; Object *url = textBox1->Text; axWebBrowser1->Navigate2(&url, &Nullobj,&Nullobj, &Nullobj,&Nullobj); </li> In Solution Explorer, right-click the Form1.h node in the Header Files folder, and then click View Designer.</li> Right-click the WebBrowser control on the form, and then click Properties.</li> In the Properties window, click Events.</li> Double-click NewWindow2 in the list of events.

This step creates an axWebBrowser1_NewWindow2 event handler in the Form1 class and puts the insertion point at the first line of the event handler.</li>  Add the following code in the axWebBrowser1_NewWindow2 event handler. Form1 * newWindow = new Form1; newWindow->Show; newWindow->axWebBrowser1->RegisterAsBrowser = true; e->ppDisp = newWindow->axWebBrowser1->Application; newWindow->Visible = true; </li> <li> Repeat steps 17 through 20 to add the WindowSetHeight event handler in the Form1 class, and then add the following code in the axWebBrowser1_WindowSetHeight event handler. int heightDiff; heightDiff = this->Height - this->axWebBrowser1->Height; this->Height = heightDiff + e->height; </li> <li> Repeat steps 17 through 20 to add the WindowSetWidth event handler in the Form1 class, and then add the following code in the axWebBrowser1_WindowSetWidth event handler. int widthDiff; widthDiff = this->Width - this->axWebBrowser1->Width; this->Width = widthDiff + e->width; </li> <li>Press CTRL+SHIFT+S to save the project.</li></ol>

back to the top

Verify that the application works
<ol> <li>Press CTRL+SHIFT+B to build the solution.</li> <li> Start Notepad, and then save the following code as the Test.htm file on your Web server. <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> <li>Press CTRL+F5 to run the program.</li> <li>On the Form1 page that appears, change the text in the text box to the URL of the Test.htm file.</li> <li>Click button1.

The WebBrowser control displays the Test.htm file as a Web page.</li> <li>Click the Open Window button that appears in the WebBrowser control.

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 the window.open method.</li></ol>

back to the top

<div class="references_section">