• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 3217
  • Last Modified:

WebBrowser, Navigate and NewWindow2

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.
0
cjs
Asked:
cjs
  • 4
  • 2
1 Solution
 
Murali Devarakonda4WeekStartup.guruCommented:
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.
0
 
cjsAuthor Commented:
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.

Summary:

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


0
 
Murali Devarakonda4WeekStartup.guruCommented:
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;
      m_pBlankDlg->Create(IDD_BLANK_DLG);

      //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 );
            OnOK();

            return;
      }
      // 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 ();
                        throw;            
                  }
                  // if the error code is zero then we just eat it            
                  else
                        pExcept->Delete ();        
            }
            *Cancel = VARIANT_TRUE;
            OnOK();

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

            return;
      }
}

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
Murali Devarakonda4WeekStartup.guruCommented:
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.

0
 
cjsAuthor Commented:
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.

0
 
Murali Devarakonda4WeekStartup.guruCommented:
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.
0

Featured Post

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now