HOWTO: Use the WebBrowser Control NewWindow2 Event

ID: Q184876


The information in this article applies to:


SUMMARY

This article describes how to use the NewWindow2 event, fired by the Microsoft WebBrowser control provided with Microsoft Internet Explorer 4.0, to specify that your browser application should be used in all cases where a new browser window is opened.

This article will describe this procedure using both Visual Basic 5.0 (VB) and the Microsoft Foundation Classes (MFC) that are part of Visual C++ 5.0 (VC).


MORE INFORMATION

The NewWindow2 event occurs when a new window is to be created for displaying a resource. This event precedes the creation of a new window from within the WebBrowser control, for example, in response to a navigation targeted to a new window or to a scripted window.open method.

Event handlers for this event receive two out parameters:

In order to specify that your browser application should be used when a new window is opened, you set ppDisp equal to a new WebBrowser object that is contained in a new window created by your application. In this scenario, if a user chooses to open a Web page in a new window, the new window in your application will be used to display the new Web page.

Here is some sample Visual Basic code to accomplish this:


Private Sub WebBrowser1_NewWindow2(ppDisp As Object,
                                   Cancel As Boolean)
   Dim frmWB As Form1
   Set frmWB = New Form1

   Set ppDisp = frmWB.WebBrowser1.Object
   frmWB.Visible = True
End Sub 
Using MFC, you may wish to do this in one of three types of applications: Here is some sample MFC code that would accomplish this task in a dialog- based application:

   void CYourDlg::OnNewWindow2(LPDISPATCH FAR* ppDisp, BOOL FAR* Cancel)
   {
      m_dlgNewWB = new CYourDlg;
      m_dlgNewWB->Create(IDD_WBDLG_DIALOG);

      *ppDisp = m_dlgNewWB->m_webBrowser.GetApplication();
   } 
Here is some sample MFC code that would accomplish this task in an SDI or MDI application. This code creates a new frame that contains a WebBrowser control. In an SDI application, this frame would appear to the user to look like another instance of the application. In an MDI application, this frame is the same as if the user had chosen to open a new child window.

 void CYourView::OnNewWindow2(LPDISPATCH FAR* ppDisp,
                              BOOL FAR* Cancel)
 {
    // Get a pointer to the application object
    CWinApp* pApp = AfxGetApp();

    // Get the correct document template
    CDocTemplate* pDocTemplate;
    POSITION pos = pApp->GetFirstDocTemplatePosition();
    pDocTemplate = pApp->GetNextDocTemplate(pos);

    ASSERT(pDocTemplate);

    // Create the new frame
    CFrameWnd* pNewFrame = pDocTemplate->CreateNewFrame(GetDocument(),
                                   (CFrameWnd*)AfxGetMainWnd());
    ASSERT(pNewFrame);

    // Activate the frame and set its active view
    pDocTemplate->InitialUpdateFrame(pNewFrame, NULL);

    CYourView* pWBVw = (CYourView*)pNewFrame->GetActiveView();
    ASSERT(pWBVw);

    *ppDisp = pWBVw->m_webBrowser.GetApplication();
 } 


REFERENCES

"Reusing the WebBrowser and MSHTML" in the Internet Client SDK Help:

http://www.microsoft.com/msdn/sdk/inetsdk/help/
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Scott Roberts, Microsoft Corporation

Additional query words: window.open


Keywords          : kbIE400 kbIE401 kbWebBrowser kbIE500 
Version           : WINDOWS:4.0,4.01
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 27, 1999