WebBrowser, Navigate and NewWindow2

Posted on 1998-06-28
Last Modified: 2013-11-20
I'm trying to use the WebBrowser control in the following way.  I want to call Navigate or Navigate2 with a path to a directory.  This works fine.  The problem is that I want the navigation to occur in the same window.  I don't want a new window to open.  I have set the flag in navigate to open a new window, so that I will get the NewWindow2 event.  I get this event OK. I then have tried to alter ppDisp so that the same window would be used to no avail. I either get an error, or I get a second window opened.   I've looked for examples of what I am trying to do and have found now.  The June MSJ article was no help either.

Thanks for any help.
Question by:cjs
  • 4
  • 2

Expert Comment

ID: 1318482
Navigate/Navigate2 both open a file in the same window by default. How are you using the WebBrowser control? Are you using VB or Visual C++ (if so, are you using MFC?).

If a new window is being created, you can make the browsing occur in the same window, but it requires a little bit of code, which I can give you. You're in luck if you are using VB- I can show you sample code which is much simpler for that too.

What I don't understand is that you say you don't want a new window to open, yet YOU are the one setting the flag to open a new window! Why? Explain.

Author Comment

ID: 1318483
Sorry, I was a bit misleading in the question.

I'm using MFC.

Yes, if I don't set the flag for opening a new window, then I get the desired action of having the navigation occur in the same window.  It's when I double click on an item in that window that I would like the navigation to again occur in the same window.  Instead, the navigation occurs in a second new window.  When this first happended I thought I would have to set the OpenNewWindow flag and then do something in the NewWindow2 event.


The first navigations OK, its the ensuing navigations that occur due to mouse clicks that I want to occur in the same window.


Accepted Solution

MuraliKrishna earned 150 total points
ID: 1318484
I still don't know if your WebBrowser control is on a Dialog or you create it in your view.
There might be a slight difference in the solution, although I think the following solution
will work for you.

Create a dialog resource with a WebBrowser control on it, and create a class for the Dialog.
Create a member variable "m_WebBrowser" in your dialog class for the WebBrowser control.

In your NewWindow2 event, create the blank dialog.

      m_pBlankDlg = new CBlankDlg;

      //Passing the pointer to the parent of current WebBrowser (need not be a dialog)
      m_pBlankDlg->m_pParent = this;      

      *ppDisp = m_pBlankDlg->m_WebBrowser.GetApplication();
      TRACE( "Modified: *ppDisp=%p\n", *ppDisp );

DO NOT SET "*Cancel to TRUE", as the Knowledge Base article "HowTo Cause Navigation to occcur
in the same WebBrowser Window at
originally suggested (it has since been modified, and explains your choices correctly).

This should cause your second WebBrowser control (the one you created on the Blank dialog) to receive
the BeforeNavigate2 event. HERE, you have to cancel the current Navigation, and call Navigate again.
This event will get called TWICE- the first time in response to what you did in NewWindow2 (save the URL and the frame
information at this point in member variables- make sure you initialize them properly in the Dialog constructor),
and a second time because you canceled navigation ( you can see in the debugger that the second time
the target URL is "about:Navigation Canceled" or something like that.
Please remove the commented out code. I've left them so you can understand better what I'm doing here.

Here's the code:
void CBlankDlg::OnBeforeNavigate2Explorer1(LPDISPATCH pDisp, VARIANT FAR *URL, VARIANT FAR* Flags,
                                                               VARIANT FAR* TargetFrameName, VARIANT FAR* PostData,
                                                               VARIANT FAR* Headers, BOOL FAR* Cancel)
      /* It is fired before Internet Explorer 4.0 navigates to a Web page. This event is fired
      ** any time an action is performed that causes navigation to occur. This event replaces the
      ** IE 3.0 BeforeNavigate and FrameBeforeNavigate events. */
TRACE( "CBlankDlg::OnBeforeNavigate2:\n\t" );
      CString strUrl( URL->bstrVal );
      if ( strUrl )
            TRACE( "IN:  Requested URL = \"%s\".\n", strUrl );
      CString strTargetFrameName( TargetFrameName->bstrVal );
      if ( strTargetFrameName )
            TRACE( "IN:  Target Frame = \"%s\".\n", strTargetFrameName );
      // First pass- save the URL and TargetFrame information here.
//      if ( m_pParent )      {
      if ( ""==m_strNewWindowUrl )      {
            //This is what we need badly ('coz NewWindow2 doesn't tell you- Save it!
            m_strNewWindowUrl = strUrl;
//            m_pParent = 0;   // You might need this- I didn't. Experiment.
            TRACE( "Attempt to create new IE Window Cancelled\n" );
            // Cancel current navigation.
            *Cancel = VARIANT_TRUE;
            CWebBrowser2 *webBrowser2 = (CWebBrowser2 *)GetDlgItem( IDC_EXPLORER1 );

      // Second pass: call Navigate with the Saved URL and Frame info.
      else {
            TRACE( "Navigating to \"%s\" in the \"%s\" Frame\n", m_strNewWindowUrl, m_pParent->m_strMainFrameName );

            COleVariant oleVarFrameName( m_pParent->m_strMainFrameName );
            COleVariant noArg;
            COleVariant varUrl( m_strNewWindowUrl );
            try {
                  m_pParent->m_MyBrowser.Navigate2( varUrl, &noArg, &oleVarFrameName, &noArg, &noArg );
            catch (COleDispatchException* pExcept) {
                  // if the error code is nonzero we want the error to occur
                  if (pExcept->m_wCode) {              
                        pExcept->Delete ();
                  // if the error code is zero then we just eat it            
                        pExcept->Delete ();        
            *Cancel = VARIANT_TRUE;

            //Not really needed as the dialog gets created each time
            m_strNewWindowUrl = "";


How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline


Expert Comment

ID: 1318485
I think I haven't left out anything here, but it's possible.
Check out the knowledge base article, and also the online docuementation for NewWindow2.

Be sure to reset the value (and of course, intialize the first time) the value of the member variable used to hold the value of the URL.


Author Comment

ID: 1318486
I have implemented what you have suggested above.  Things work, and I understand the idea behind the hidden dialog.  The only thing that does not work is when I double click on an item in the explorer display, I don't get NewWindow or BeforeNavigate events.  A new window is created and displayed.  Is this related to the fact that the 'doc' type for a directory is another explorer, so it runs this instead?

Thanks for all your help so far. I feel Its so close now.


Expert Comment

ID: 1318487
That is a totally different problem requiring completely different solution.

I can tell you this much- you have to implement the IDocHostUIHandler interface.

Good luck.

Featured Post

What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

Join & Write a Comment

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now