Groups | Blog | Home
all groups > dotnet windows forms controls > april 2008 >

dotnet windows forms controls : How to pass the IE windows handle out of the WebBrowser?



Sean
4/30/2008 2:16:00 PM
I am using a WebBrowser control to access a third-party website in my
application. I need to intercept the pop up IE window event on the website
and display page in another WebBrowser control so that I can parse the DOM
object in the pop up page.

Private Sub webWebBrowser_NewWindowExtended(ByVal sender As Object, ByVal e
As iQControls.WebBrowserNewWindowExtendedEventArgs) Handles
webWebBrowser.NewWindowExtended
e.Cancel = True

string url = e.Url

... ' display the url in another webbrowser so that we can parse
the page

End Sub

It worked fine. But we suffer changes applied on the website recently. Here
is the JavaScript to open the pop up window I found on the latest website.

url = "blank.html";
w = open(url, windowName,
'width=690,height=600,screenX=0,screenY=0,scrollbars=yes,menubar=yes');
....
w.document.write(s); ' write the page content
w.document.close();
w.focus();

The w returned by open() is always null because we set the e.Cancel to true
in our WebBrowser. And if we set the e.Cancel to false, we get a pop-up IE
window with desired content and a webbrowser window with blank content. But
in this way we can't parse DOM object.

It may be a solution if we can pass the handle of IE instance inside the
WebBrowser control back to JavaScript. I am searching for that. I'd like to
know if there anybody knows how to do it?

Any help is appreciated.
Sheng Jiang[MVP]
4/30/2008 5:33:03 PM
You need to handle the NewWindow2 event and pass your new window's
webbrowser's interface back to the event argument (NewWindow2)
..Net does not provide direct access of this event, however, there are many
open source code on the internet that wrap this event. One example is at
www.codeproject.com/KB/miscctrl/csEXWB.aspx

--
Sheng Jiang
Microsoft MVP in VC++
[quoted text, click to view]

Sean
5/5/2008 8:31:00 AM
Hi Sheng,

Thanks for your reply.

I tried the NewWindow2 with csExWB but it does not work.

Here is the index.html I accessed.

<script language="JScript">
function rewriteServiceAgreement(){
page="RegenerateServiceAgreement";

var windowName = "SA_";

var w = null;

var blankname= "blank.html";
var retry=0;
while (w == null && retry<100) {
if(retry > 0) {
alert('Regenerating service agreement.');
}
alert(retry);
w = open(blankname, windowName,
'width=890,height=600,screenX=0,screenY=0,scrollbars=yes,menubar=yes');
retry++;
alert(retry);
}

if(w == null) {
alert('IE BUG: the dwindow is still null.');
}else if(w.document == null){
alert('IE BUG: the document is null.');
}

var s = "<body><html>test</html></body>";

w.document.close();
w.document.open();
w.document.write(s);
w.document.close();
w.focus();
}
</script>
<body onclick="rewriteServiceAgreement();">
Click this page and window.open() is called.
</body>

Here is my handle code for event NewWindow2.

string url;
private void cEXWB1_NewWindow2(object sender,
csExWB.NewWindow2EventArgs e)
{
Form1 form = new Form1();
form.navigate(url); // We get the blank page.
e.browser = form.Browser; // form.Browser is a default .NET WebBrowser
control.
form.Show();
}

private void cEXWB1_NewWindow3(object sender,
csExWB.NewWindow3EventArgs e)
{
url = e.url;
}

But I never see the second alert(retry). It looks like the javascript is
hung up on the “open” statement once the event handle code has been executed.
And the javascript works if we comment e.browser = form.Browser.

Do you know what reason is and how to resolve it?

Any help is appreciated.

Sean

[quoted text, click to view]
Sheng Jiang[MVP]
5/5/2008 2:09:12 PM
You are pass a reference of a .Net wrapper object, while the event expect an
reference to an WebBrowser ActiveX object.

--
Sheng Jiang
Microsoft MVP in VC++
[quoted text, click to view]

Sean
5/6/2008 3:16:08 PM
Hi Sheng,

Thanks for your reply. I changed the Browser type from default WebBrowser to
AxSHDocVw.AxWebBrowser. But the javascript is still hung up.

string url;
private void cEXWB1_NewWindow2(object sender,
csExWB.NewWindow2EventArgs e)
{
Form1 form = new Form1();
form.navigate(url);

e.browser = form.Browser; // Browser is AxSHDocVw.AxWebBrowser type.

form.Show();
}

Here is the class Form1.
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}

public void navigate(string url)
{
this.axWebBrowser1.Navigate(url);
}

public AxSHDocVw.AxWebBrowser Browser
{
get { return axWebBrowser1; }
set { axWebBrowser1 = value; }
}
}

Do you have any suggestion? Any help is appreciated.


[quoted text, click to view]
Sheng Jiang[MVP]
5/9/2008 10:07:42 AM
If you can get the ActiveX's IWebBrowser2 interface, return its Application
property to NewWindow2.

--
Sheng Jiang
Microsoft MVP in VC++
[quoted text, click to view]

AddThis Social Bookmark Button