Microsoft KB Archive/184876

= How To Use the WebBrowser Control NewWindow2 Event =

Article ID: 184876

Article Last Modified on 7/13/2004

-

APPLIES TO


 * Microsoft Internet Explorer 4.0 128-Bit Edition
 * Microsoft Internet Explorer 4.01 Service Pack 2
 * Microsoft Internet Explorer 5.0

-



This article was previously published under Q184876



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:


 * ppDisp: A pointer to the IDispatch interface of a WebBrowser or InternetExplorer object. You set this pointer equal to the IDispatch interface of a new or existing WebBrowser or InternetExplorer object.
 * Cancel: A pointer to a Boolean value. Setting this value to VARIANT_TRUE causes navigation to stop and no new window is opened.

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.

In addition, set the RegisterAsBrowser property to TRUE for the newly created WebBrowser control in order for it to participate in window name resolution. For example, if this window name is used elsewhere in script, then this control will be used instead of a newly created one because it checks all of the existing window names before opening a new window.

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

frmWB.WebBrowser1.RegisterAsBrowser = TRUE

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:
 * Dialog-based
 * Single document interface (SDI)
 * Multiple document interface (MDI)

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);

m_dlgNewWB->m_webBrowser.SetRegisterAsBrowser(TRUE);

*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);

pWBVw->m_webBrowser.SetRegisterAsBrowser(TRUE);

*ppDisp = pWBVw->m_webBrowser.GetApplication; }

