Summary
When hosting the Internet Explorer 4.x or later WebBrowser control in a Visual Basic application, you may want to have the navigation always occur in your application and not other Internet Explorer windows. If you handle the NewWindow2 event and set the Cancel flag equal to True, navigation is canceled completely. Since NewWindow2 does not provide you with the URL to navigate to as the Internet Explorer 3.x NewWindow event did, there doesn't appear to be any way to have the navigation occur in the same window.
Fortunately, Internet Explorer 4.x or later provide the WebBrowser_V1 object for compatibility with Internet Explorer 3.x. Using the WebBrowser_V1 object, you can have your application receive events from version 3.x, 4.x, and 5.x. That means that you can handle the version 3.x NewWindow event and then have the navigation occur in the current window.
More Information
In order to implement this functionality in your Visual Basic application, follow these step:
Create a form with a WebBrowser control on it.
In the declarations section of that form, add the following:
Dim WithEvents Web_V1 as SHDocVwCtl.WebBrowser_V1
This will declare a WebBrowser_V1 variable that can receive events WebBrowser_V1 provides you with the NewWindow event.
In the Form_Load event, add the following:
Set Web_V1 = WebBrowser1.Object
WebBrowser1.Navigate2 "http://www.microsoft.com/"
This sets the WebBrowser_V1 object to the existing Internet Explorer WebBrowser object.
After the NewWindow2 event fires, the Web_V1_NewWindow event will fire with the linked URL as one of its input arguments. Remember not to set Cancel to True in NewWindow2. Also, set the Processed variable to True in the NewWindow event handler so that a new instance of Internet Explorer will not be created. The following code shows this event handler and the code necessary to navigate within the current window:
Private Sub Web_V1_NewWindow(ByVal URL As String, _
ByVal Flags As Long, _
ByVal TargetFrameName As String, _
PostData As Variant, _
ByVal Headers As String, _
Processed As Boolean)
Processed = True
WebBrowser1.Navigate URL
End Sub
Right-click a link and select "Open in New Window" and you will find the link will still open inside your WebBrowser Control.
Please note that Internet Explorer does not fire a NewWindow or NewWindow2 event when the user presses CTRL+N or points to New under the File menu and clicks Window.
References
For additional information, please see the following article in the Microsoft Knowledge Base:
Q184876 HOWTO: Use the WebBrowser Control NewWindow2 Event
For more information, see the MSDN Online Web Workshop:
http://msdn.microsoft.com/workshop/
(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Scott Roberts, Microsoft Corporation
I am developing a custom browser using the Microsoft Foundation Class (MFC) CHtmlView class. I want to make all windows open within my custom browser, and not in Microsoft Internet Explorer.
Dhiren Vyas
The Web Team replies:
As you probably already know, developing your own custom browser gives you complete control over your user interface whilst making use of the incredible technology that Internet Explorer has to offer. You get to choose what your browser looks like and what it can do for your customers. Microsoft Visual C++ makes this task even easier by providing an MFC class, CHtmlView, that implements a view based on the WebBrowser control—a reusable component of Internet Explorer.
There are obstacles, though. When a Web page opens a window (by calling window.open, for example) an Internet Explorer object is created. This means that the Web page will appear in an Internet Explorer window. If you want all windows to appear in your application, you can use the NewWindow2 event, available on the DWebBrowserEvents2 interface. We'll demonstrate how this event can be used in a multiple document interface (MDI) MFC application. We'll be brief because this subject is covered in detail in the Knowledge Base article HOWTO: Use the WebBrowser Control NewWindow2 Event (Q184876).
First let's create an MDI MFC application using the AppWizard. During the final page of the wizard, select a view class that is derived from CHtmlView. Then use the ClassWizard to add an event handler for the NewWindow2 event. Finally, add code to your NewWindow2 event handler function:
void CMyHtmlView::OnNewWindow2(LPDISPATCH* ppDisp, BOOL* Cancel)
{
// Get a pointer to the application object.
CWinApp* pApp = AfxGetApp();
// Get the correct document template.
POSITION pos = pApp->GetFirstDocTemplatePosition();
CDocTemplate* pDocTemplate = pApp->GetNextDocTemplate( pos );
// Create a new frame.
CFrameWnd* pFrame = pDocTemplate->CreateNewFrame(
GetDocument(),
(CFrameWnd*)AfxGetMainWnd() );
Navigate to a Web page that opens a new window and you'll see the Web page displayed in your application. This works because your application intercepts the NewWindow2 event, creates a new document/frame/view combination, and passes the IDispatch for the WebBrowser object. This causes your CHtmlView-derived class to be used to display the Web page. Note that the WebBrowser object must be created each time and not have navigated to a URL, or this won’t work.
You'll probably want to make this more useful by obtaining the window information, such as the height and width, so that you can modify your view accordingly. A simple approach to this is to handle the BeforeNavigate2 event that is fired after the new window is created. Add the following code to your application to resize the view to the size of the new window. Note that this is an overridden method that allows access to the WebBrowser object, and that an additional Boolean member, m_bResizeWindow, is required. Set this to false in the constructor and true in the NewWindow2 event handler to ensure this code is only called for new windows.