[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now


WebBrowser, Navigate and NewWindow2

Posted on 1998-06-28
Medium Priority
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
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2

Expert Comment

by:Murali Devarakonda
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

Murali Devarakonda earned 450 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 http://support.microsoft.com/support/kb/articles/q185/5/38.asp 
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 = "";


What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.


Expert Comment

by:Murali Devarakonda
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

by:Murali Devarakonda
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

Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

Question has a verified solution.

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

Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
Please read the paragraph below before following the instructions in the video — there are important caveats in the paragraph that I did not mention in the video. If your PaperPort 12 or PaperPort 14 is failing to start, or crashing, or hanging, …
Suggested Courses

656 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