Microsoft KB Archive/815713

From BetaArchive Wiki

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

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:

  1. Start Microsoft Visual Studio .NET 2003 or Microsoft Visual Studio 2005.
  2. On the File menu, point to New, and then click Project.

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

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

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

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

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

    Microsoft WebBrowser points to Shdocvw.dll in your system32 directory.
  10. 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.
  11. Double-click the Microsoft WebBrowser control to add a WebBrowser control to Form1.
  12. Resize both Form1 and the WebBrowser control.
  13. In the toolbox, click the Windows Forms tab, and then double-click Button.
  14. In the toolbox, click the Windows Forms tab, and then double-click TextBox.
  15. 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.
  16. Add the following code to the button1_Click event handler:

    Object *Nullobj = NULL;
    Object *url = textBox1->Text;
    axWebBrowser1->Navigate2(&url, &Nullobj,&Nullobj, &Nullobj,&Nullobj);
  17. In Solution Explorer, right-click the Form1.h node in the Header Files folder, and then click View Designer.
  18. Right-click the WebBrowser control, and then click Properties.
  19. In the Properties window, click Events.
  20. 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.
  21. 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:

    1. Click Project, and then click <ProjectName> Properties.

      Note <ProjectName> is a placeholder for the name of the project.
    2. Expand Configuration Properties, and then click General.
    3. 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:

    /clr (Common Language Runtime Compilation)
    http://msdn2.microsoft.com/en-us/library/k8d11d4s.aspx

    These steps apply to the whole article.

  22. Press CTRL+SHIFT+B to build the program.
  23. Press CTRL+F5 to start the application.

    Form1 is displayed, and a blank Web Browser control appears on the form.

back to the top

Verify that the program works

  1. 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>
  2. If the application that you developed in the previous section is not running, start that application.
  3. In Form1, change the text in the text box to the URL of the Test.htm file.
  4. Click button1.

    The WebBrowser control displays the Test.htm page.
  5. 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 top


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

REFERENCES

For 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

Keywords: kbwebbrowser kbhtml kbcontrol kbcominterop kbcollections kbinterop kbinetdev kbevent kbhowtomaster KB815713