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 = "";


Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.


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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Excel Use VBA to get user's Mac Address for their computer 5 115
difference between String.subString() and String.subSequence() 6 177
java ^ examples 8 62
wordmultiple challenge 12 121
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

867 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

22 Experts available now in Live!

Get 1:1 Help Now