Microsoft KB Archive/815713

= How to use the WebBrowser control and the NewWindow2 event in Visual C++ .NET or in Visual C++ 2005 =

Article ID: 815713

Article Last Modified on 11/29/2007

-

APPLIES TO


 * Microsoft Visual C++ 2005 Express Edition
 * Microsoft Visual C++ .NET 2003 Standard Edition
 * Microsoft .NET Framework 1.1
 * Microsoft Internet Explorer 5.5
 * Microsoft Internet Explorer (Programming) 5.5 SP2
 * Microsoft Internet Explorer 6.0

-







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



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

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



SUMMARY
You can use the NewWindow2 event to handle and to customize the new WebBrowser window creation process. The NewWindow2 event occurs before a new window from the WebBrowser control is created. For example, the NewWindow2 event may occur in response to a navigation event that is targeted at a new window or to a scripted Window.Open method.

The RegisterAsBrowser property of the new WebBrowser control must be set to true for the new WebBrowser control to participate in the window-name resolution. For example, if the window name is used elsewhere in the script, the WebBrowser control is used instead of a newly created control. This behavior occurs because the WebBrowser control checks all the existing window names before a new window is created.

IN THIS TASK

 * INTRODUCTION
 * Requirements
 * Create the project, and then add code to the project
 * Verify that the program works
 * REFERENCES



INTRODUCTION
This article describes how to use the NewWindow2 event that is triggered by the WebBrowser control that is included with Microsoft Internet Explorer 4.0 and later. You can use the NewWindow2 event to specify that your browser program must be used whenever a new browser window is opened. This article describes this procedure for Microsoft Visual C++ .NET or for Microsoft Visual C++ 2005.

back to the top

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

This article assumes that you are familiar with the following topics:
 * How to use Visual C++ .NET 2003 or Visual C++ 2005
 * How to host the WebBrowser control (Shdocvw.dll)

back to the top

Create the project, and then add code to the project
The project that you create in this section, directs the WebBrowser control to the following Microsoft Web site:

http://www.microsoft.com

 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 opens. Under Project Types, click Visual C++ Projects.

Note In Visual Studio 2005, Visual C++ Projects is changed to Visual C++. Under Templates, click Windows Forms Application (.NET).

Note In Visual Studio 2005, Windows Forms Application (.NET) is changed to Windows Forms Application. In the Name box, type NewWindow2, and then click OK.

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

Note In Visual Studio 2005, Add/Remove Items is changed to Choose Items.</li> In the Customize Toolbox dialog box, click the COM Components tab.</li> In the COM components list, locate Microsoft WebBrowser.

Microsoft WebBrowser points to Shdocvw.dll in your system32 directory.</li> Click to select the Microsoft WebBrowser check box, and then click OK.

A new control that is named Microsoft WebBrowser appears on the General tab in your toolbox. You can add the WebBrowser control to the form.</li> Double-click the Microsoft WebBrowser control to add a WebBrowser control to Form1.</li> Resize both Form1 and the WebBrowser control.</li> In the toolbox, click the Windows Forms tab, and then double-click Button.</li> In the toolbox, click the Windows Forms tab, and then double-click TextBox.</li> In Form1, double-click button1.

The button1_Click event handler is created in the Form1 class, and then your pointer appears at the first line of the event handler.</li>  Add the following code to 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, and then click Properties.</li> In the Properties window, click Events.</li> In the list of events, double-click NewWindow2.

This creates the axWebBrowser1_NewWindow2 event handler in the Form1 class, and then your pointer appears at the first line of the event handler.</li>  Add the following code to the axWebBrowser1_NewWindow2 event handler: Form1* frmWB; frmWB = new Form1;

frmWB->axWebBrowser1->RegisterAsBrowser = true; e->ppDisp = frmWB->axWebBrowser1->Application; frmWB->Visible = true; Note You must add the common language runtime support compiler option (/clr:oldSyntax) in Visual C++ 2005 to successfully compile the previous code sample. To add the common language runtime support compiler option in Visual C++ 2005, follow these steps: <ol style="list-style-type: lower-alpha;"> <li>Click Project, and then click  Properties.

Note  is a placeholder for the name of the project.</li> <li>Expand Configuration Properties, and then click General.</li> <li>Click to select Common Language Runtime Support, Old Syntax (/clr:oldSyntax) in the Common Language Runtime support project setting in the right pane, click Apply, and then click OK.</li></ol>

For more information about the common language runtime support compiler option, visit the following Microsoft Web site:

/clr (Common Language Runtime Compilation)

http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

These steps apply to the whole article. </li> <li>Press CTRL+SHIFT+B to build the program.</li> <li>Press CTRL+F5 to start the application.

Form1 is displayed, and a blank Web Browser control appears on the form.</li></ol>

back to the top

Verify that the program works
<ol> <li> Start Notepad, put the following code sample in Notepad, and then save the file as Test.htm on your Web server: <!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.0 Transitional//EN&quot;> <HTML> <HEAD> <META HTTP-EQUIV=&quot;Content-Type&quot; CONTENT=&quot;text/html; charset=iso-8859-1&quot;> <TITLE>Q815713</TITLE>

<SCRIPT type = &quot;text/Jscript&quot;> function openWin {         var win; win = window.open(&quot;http://www.microsoft.com&quot;); } </SCRIPT> </HEAD> <BODY>

<button onClick=&quot;openWin&quot;>Open New Window

</BODY> </HTML> </li> <li>If the application that you developed in the previous section is not running, start that application.</li> <li>In Form1, 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 page.</li> <li>Click the Open New Window button that appears in the WebBrowser control.

Notice that the Web page opens in a new instance of the program.</li></ol>

back to the top

Additional query words: SHDocVw AxInterop AxWebBrowser Navigate2 .NET COM Interops WebBrowser Control NewWindow2 event

<div class="references_section">