Solved

WebBrowser, Navigate and NewWindow2

Posted on 1998-06-28
6
3,102 Views
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.
0
Comment
Question by:cjs
  • 4
  • 2
6 Comments
 
LVL 1

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.
0
 

Author Comment

by:cjs
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.

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
 
LVL 1

Accepted Solution

by:
Murali Devarakonda 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;
      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
Migrating Your Company's PCs

To keep pace with competitors, businesses must keep employees productive, and that means providing them with the latest technology. This document provides the tips and tricks you need to help you migrate an outdated PC fleet to new desktops, laptops, and tablets.

 
LVL 1

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.

0
 

Author Comment

by:cjs
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.

0
 
LVL 1

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.
0

Featured Post

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
twoTwo  challenge 35 103
fix34  challenge 9 121
wordappend challenge 8 179
Trying to run powershell  in a batch file. How do I do this? 8 92
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
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…
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.
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…

803 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