How to use the WebBrowser control and the NewWindow2 event in Visual C++ .NET or in Visual C++ 2005 (815713)
The information in this article applies to:
- Microsoft Visual C++ 2005 Express Edition
- Microsoft Visual C++ .NET (2003)
- Microsoft .NET Framework 1.1
- Microsoft Internet Explorer (Programming) 5.5
- Microsoft Internet Explorer (Programming) 5.5 SP2
- Microsoft Internet Explorer (Programming) version 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
SUMMARYYou 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 TASKINTRODUCTIONThis 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 topRequirementsThe
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 projectThe project that you create in this section, directs the WebBrowser control to the
following Microsoft Web site: - 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. - In the toolbox, click the General tab.
- Right-click Toolbox, and then click
Add/Remove Items.
Note In Visual Studio 2005, Add/Remove Items is changed to Choose Items. - In the
Customize Toolbox dialog box, click the COM Components tab.
- In the COM components list, locate Microsoft WebBrowser.
Microsoft WebBrowser points to Shdocvw.dll in your
system32 directory. - 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. - Double-click the Microsoft WebBrowser control to add a WebBrowser control to Form1.
- Resize both Form1 and the WebBrowser
control.
- In the toolbox, click the Windows Forms tab, and then double-click Button.
- In the toolbox, click the Windows Forms tab, and then double-click
TextBox.
- 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. - Add the following code to the
button1_Click event handler:
Object *Nullobj = NULL;
Object *url = textBox1->Text;
axWebBrowser1->Navigate2(&url, &Nullobj,&Nullobj, &Nullobj,&Nullobj); - In Solution Explorer, right-click the Form1.h
node in the Header Files folder, and then click View
Designer.
- Right-click the WebBrowser control, and then click
Properties.
- In the Properties window, click Events.
- 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. - 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:
- Click Project, and then click <ProjectName> Properties.
Note <ProjectName> is a placeholder for the
name of the project. - Expand Configuration Properties, and then click
General.
- 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.
For more information about the common language runtime support compiler option, visit the following Microsoft Web site:These steps apply to the whole article.
- Press CTRL+SHIFT+B to build the
program.
- Press CTRL+F5 to start the
application.
Form1 is displayed, and a blank Web Browser control appears on the form. back to the topVerify that the program works- Start Notepad, put the following code sample in Notepad, and then save the file as Test.htm on your Web server:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML>
<HEAD>
<META HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=iso-8859-1">
<TITLE>Q815713</TITLE>
<SCRIPT type = "text/Jscript">
function openWin()
{
var win;
win = window.open("http://www.microsoft.com");
}
</SCRIPT>
</HEAD>
<BODY>
<button onClick="openWin()">Open New Window</button>
</BODY>
</HTML> - If the application that you developed in the previous section is not running, start that application.
- In Form1, change the text in the text
box to the URL of the Test.htm file.
- Click button1.
The WebBrowser
control displays the Test.htm page. - 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. back to the
topREFERENCESFor additional information about the WebBrowser control and the
methods, the properties, and the events that the WebBrowser control exposes, visit the following Microsoft Developer Network (MSDN) Web
site: back to the top
Modification Type: | Major | Last Reviewed: | 4/21/2006 |
---|
Keywords: | kbWebBrowser kbhtml kbControl kbCOMInterop kbCollections kbinterop kbInetDev kbEvent kbHOWTOmaster KB815713 kbAudDeveloper |
---|
|
|
©2004 Microsoft Corporation. All rights reserved.
|
|