Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

RE: Prevent WebBrowser from launching IE

199 views
Skip to first unread message

Linda Liu [MSFT]

unread,
Jan 22, 2007, 1:52:26 AM1/22/07
to
Hi Lance,

The managed WebBrowser class is a wrapper for native COM object 'Microsoft
Web Browser' and the managed WebBrowser class does not expose all
properties or events of the original COM object.

You're in the right direction handling the NewWindow event of the managed
WebBrowser class to control at which target the new link will be opened.
Unfortunately, the NewWindow event of the managed WebBrowser class does not
bring us the new link information.

To solve this problem, I suggest that you use native COM object 'Microsoft
Web Browser' instead. If the COM object has not added to the Toolbox, add
it first to the Toolbox. To do this, right-click the Toolbox and select
'Choose Items'. In the 'Choose Toolbox Items' window, switch to 'COM
Components' tab and select the checkbox before the 'Microsoft Web Browser'
option. Press OK.

Drag the 'Microsoft Web Browser' from Toolbox onto your form and VS IDE
will generate a managed class for the COM object automatically and then you
could use the wrapped COM object like other managed controls.

'Microsoft Web Browser' provides NewWindow2 and NewWindow3 events, both of
which fire when a new window is to be created. NewWindow3 event extends
NewWindow2 with additional information about the new window, including the
new link to be opened. I suggest that you handle the NewWindow3 event and
get the new link from the event args and then open it in a new web browser
control.

The following is a sample.

public Form1()
{
InitializeComponent();
this.axWebBrowser1.NewWindow3 += new
AxSHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(axWebBrowser1_NewWindow3
);
}

void axWebBrowser1_NewWindow3(object sender,
AxSHDocVw.DWebBrowserEvents2_NewWindow3Event e)
{
string url = e.bstrUrl;
// navigate the new link in a new web browser control in your
program
....
e.cancel = true;
}

Hope this helps.
If you have any question, please feel free to let me know.


Sincerely,
Linda Liu
Microsoft Online Community Support

==================================================
Get notification to my posts through email? Please refer to
http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif
ications.

Note: The MSDN Managed Newsgroup support offering is for non-urgent issues
where an initial response from the community or a Microsoft Support
Engineer within 1 business day is acceptable. Please note that each follow
up response may take approximately 2 business days as the support
professional working with you may need further investigation to reach the
most efficient resolution. The offering is not appropriate for situations
that require urgent, real-time or phone-based interactions or complex
project analysis and dump analysis issues. Issues of this nature are best
handled working with a dedicated Microsoft Support Engineer by contacting
Microsoft Customer Support Services (CSS) at
http://msdn.microsoft.com/subscriptions/support/default.aspx.
==================================================

This posting is provided "AS IS" with no warranties, and confers no rights.

ljlevend2

unread,
Jan 22, 2007, 11:46:01 PM1/22/07
to
Hi Linda,

Thanks a lot. That was very helpful. I was not aware of the NewWindow3
event although I was aware of the 'Microsoft Web Browser' (which I will refer
to as AxWebBrowser). The reason why I do not use AxWebBrowser is that I
utilize many of the events, properties and methods in
System.Windows.Forms.WebBrowser (i.e., WebBrowser). If I convert from using
a WebBrowser to a AxWebBrowser then I would need to find a way to implement
all of those features.

Is it possible to access the underlying AxWebBrowser that is associated with
a WebBrowser control? If so, then I would have the best of both worlds
because I could handle the AxWebBrowser.NewWindow3 event while still
utilizing features that are exposed by the WebBrowser control.

Thanks again for your help!
Lance

Linda Liu [MSFT]

unread,
Jan 23, 2007, 4:25:18 AM1/23/07
to
Hi Lance,

Thank you for your response.

I have spent some time doing more research on the problem and found that we
could convert the ActiveXInstance property of the WebBrowser to the type
of SHDocVw.WebBrowser, and then subscribe the NewWindow3 event of the
converted object.

SHDocVw.WeBrowser is an interface which is auto-generated by VS IDE when we
drag an ActiveX Web Browser control onto a form. I suggest that you add the
'Microsoft Web Browser' COM component to the Toolbox and the drag it onto
your form, which gets the COM wrapper classes generated by VS and the
delete the AxWebBrowser control from the form.

The following is a sample code.

private void Form1_Load(object sender, EventArgs e)
{
this.webBrowser1.Url = new Uri(some url path);
SHDocVw.WebBrowser wb = this.webBrowser1.ActiveXInstance as
SHDocVw.WebBrowser;
if (wb != null)
{
wb.NewWindow3 += new
SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(wb_NewWindow3);
}
}

void wb_NewWindow3(ref object ppDisp, ref bool Cancel, uint dwFlags, string
bstrUrlContext, string bstrUrl)
{
// navigate the new link (bstrUrl) in a new web browser control
in your program
....

Cancel = true;
}

Note that only after we navigate a web page in the WebBrowser control, the
ActiveXInstance property returns a value; otherwise it returns null. So be
sure to subscribe the NewWindow3 event of the conveted object after you
navigate a web page for the first time.

Please try my suggestion and let me know the result.

ljlevend2

unread,
Jan 25, 2007, 9:41:01 PM1/25/07
to
Hi Linda,

You're so cool. What a great workaround. Now I can have the best of both
worlds. Thank you so much for taking the time to help me out.

Lance

Tara H

unread,
Apr 24, 2007, 7:38:02 AM4/24/07
to
Hi Linda,

Hope you'll forgive me butting in on this old thread but I have exactly the
same issue. However, I'm doing this in j#, and I can't seem to get it to
work correctly. I have interpreted your example as equivalent to

...
this.flashBrowser.set_DocumentText(s);
SHDocVw.WebBrowser wb = this.flashBrowser.get_ActiveXInstance();
if(wb!=null){
wb.add_NewWindow3(new
SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(wb_NewWindow3));
}

...

private void wb_NewWindow3(Object ppDisp, boolean Cancel, System.UInt32
dwFlags, String bstrUrlContext, String bstrUrl){
String url = bstrUrl;
System.out.println(url);
Cancel = true;
}

But that gives me the following errors:
Type 'Object' is not assignable to 'SHDocVw.WebBrowser'
and
Signature of method 'wb_NewWindow3' doesn't match with the signature of
'SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler' Delegate

If it's not too much of an imposition, I'd really appreciate your input on
this.

Many Thanks,
Tara H

0 new messages