2

I use a webbrowser control in my application to get data from a specific web page. This web page won't work with the older IE because it specifically checks for the IE version. So I made a registry change that allows my application to work as IE 9 and everything is ok most of the time...

The problem is when a newwindow has to be opened. It won't display anything. I guess that the newwindow is acknowledged as IE 7 and I don't know how to make it disguise itself as IE9.

I also tried the other way round. I thought that if I intercepted the newwindow url then I could just send it to IE9 or open it in another instance of a webbrowser control. But the newwindow event only allows to cancel the event. You can't get any useful information out of it.

I believe that interop services is what I need but I know nothing about them.

So I've got two questions:
(1) Can I make the newwindow identify as IE9? (and how...)
(2) How can I get the newwindow url using interop services (or anything, I wouldn't care)?

John Saunders
  • 160,644
  • 26
  • 247
  • 397
pzogr
  • 424
  • 1
  • 12
  • 30

2 Answers2

1

In the NewWindow event, assuming your first Wb control is named WB1 and the one you want to redirect to is WB2, do the following in your WB1 NW event.

Processed = True ' This cancels the current request.
WB2.Navigate URL ' This redirects it to the second WB2 control.

Otherwise, if you want to use the NW2 (NewWindow2) event instead of the NewWindow (NW) event, do this in the NW2 event of the WB1 control.

Set ppDisp = WB2.object ' Just swaps the objects around to redirect, don't need to issue a cancel.

Also, you can do this via BeforeNavigate2 (of WB1). But slightly different code.

Cancel = True ' Cancel Request.
WB2.Navigate2 URL ' Reissue it to WB2.

Now, as long as you control where it redirects to, you can get the new window URL easily, using WB2.LocationURL or Wb2.Document.URL if i am not mistaken.

Also, if you want to change the rendering engine to IE9 (even if IE9 is installed on your computer, WB control will use IE7 rendering engine for compatibility)... there are articles online and answers on SO (including some of my previous answers) which clarify how you can alter the registry to ensure the rendering engine used by the WB control is the same as that of the installed version (IE9), otherwise, it will always use IE7. And, if you have IE4, 5 or 6 installed on a machine, it will always use IE4 for the rendering engine. I think they update teh rendering version after ever 3-4 version changes. I'm assuming during version 10, WB control rendering version will be version 10 as well.

Let me know if you need more assistance with it and i've love to know how you got along and if this helped answer your question. All my examples are in VB6, but you can transform them easily.

Cheers.

Erx_VB.NExT.Coder
  • 4,838
  • 10
  • 56
  • 92
  • Thanks for the answer, but I don't get it... I can't get the newwindow name or any event. The web page opens it and I don't have any control over it. As I mentioned earlier I did the necessary registry changes for my webbrowser control to be identified as IE9 but the newwindow seems to identified as IE7 although I'm not sure about that - I just think it does so because it won't work! So the question is how to access the newwindow that I've not created... – pzogr Sep 04 '12 at 09:25
  • @pzogr the registry changes to make ur WB control run in IE9 mode is not difficult, we'll get to that later. first of all, there should be a WebBrowser1_NewWindow or WebBrowser1_NewWindow2 event, you can access this from the events list, lets first make sure you can get in there and see the event auto-printed out from your visual studio IDE/Designer. Once you have done this, let me know and we'll continue from there. Of course, once you find this, feel free to inspect the variables or place the code i've given you in there while ensuring your second WebBrowser control is names WB2, let me know – Erx_VB.NExT.Coder Sep 04 '12 at 13:58
  • First of all, I already said that the registry changes are in place. I know what has to be done there and I've done it.About the NewWindow: There is a NewWindow event but as I said you can only cancel the event (e.cancel=true). You have no access to the NewWindow besides that. – pzogr Sep 04 '12 at 15:33
  • @pzogr Please tell me the arguments you see in the NewWindow event, also, try NewWindow2 if you can, when the event method is auto-generated, please copy & paste the arguments that are presented in the auto generated event code for NewWindow2 and NewWindow and we'll take it from there. Also, re the regedit, you said the second window may not be recognized as IE9, which is why I'm still trying to help you with this, as the regeidt should ensure it works on both WB Controls on your form. To know for sure, go to http://www.whatsmyuseragent.com/ on both your WB Controls and you will know for sure. – Erx_VB.NExT.Coder Sep 06 '12 at 06:06
  • Thanks for taking the time to help! About the registry: I wrote a simple html with javascript code that opens a new window which navigates to whatsmyuseragent.com and proved that my initial assumption was wrong! The new window is identified properly as IE9. I cannot figure out why it won't work though, so it seems to me that the best solution is to reject the new window and send the url to the webbrowser control. I have no newwindow2 event because I use the webbrowser control. I'll give it a try with axWebBrowser and tell you the results. – pzogr Sep 06 '12 at 09:42
  • @pzogr you said you don't have a NewWindow2 event, but do you have a NewWindow event? If so, copy/paste the arguments that are auto generated inside the NewWindow event, or whatever NewWindow type event you can find. I ask you because I use the Visual Studio 6.0 version myself (VB6) and don't have VS2012 fired up yet. – Erx_VB.NExT.Coder Sep 06 '12 at 13:08
  • When using the webbrowser control which is integrated with visual studio you only get an e.Cancel argument with the newwindow event... You do get the newwindow2 and newwindow3 event when using axWebBrowser which is not integrated with visual studio. – pzogr Sep 06 '12 at 21:03
  • @pzogr if you could copy/paste the entire argument & line, that would be great. As Im not sure if you are getting it as an "e" or actually just "cancel" or as "e.cancel as something", do you see what i mean? in either case though, if you can access e.Cancel, all you need to do is set it to False, and you do not need anything else in that event, since when you do the redirect, you are doing it to the second WB Controls Navigate or Navigate2 method, so assume WB1 is first windows with your e.Cancel argument, you set it to true, and send URL to WB2's Navigate Method, "WB2.Navigate URL" is enough. – Erx_VB.NExT.Coder Sep 13 '12 at 13:20
  • @pzogr Because Im unsure how you are receiving the argument, Im assuming its "e as System.Object", if so, the URL argument should be in the "e" object, ie: "e.URL" should give you the URL to redirect to. Im certain the NewWindow event will send you the URL, once again, if its declared as Object, you wont get the intellisense and will not be aware of this, if it is declared as something specific ie: "someObjectEventArgs" youd get the intellisense when you type "e." so youll realize what you can play with. Im assuming its declared as "System.Object" since you cant see a URL, pls paste full line! – Erx_VB.NExT.Coder Sep 13 '12 at 13:25
  • @pzogr If worst comes to worst, you can create a publicly visible variable which always holds the last WB1 URL request, and set this variable during each BeforeNavigate/2 event, so this way, youll be able to get the last .Navigate'd URL in your NewWindow event and you can use that for the redirect to WB2 or even to WB1 if you wanted. Of course, this isnt the preferred way to do things, & you are better off getting the URL from the "e" object that you receive from the NW event, its either going to be "e.URL" or "e.LocationURL" or something like that, but ill be able to tell u after u paste it! – Erx_VB.NExT.Coder Sep 13 '12 at 13:34
  • e is CancelEventArgs... I implemented the NewWindow3 event and I CAN get the url by using e.bstrUrl. I CAN open it in another webbrowser object but I CAN'T use e.ppDisp = (webbrowser control) to do it because I get nothing on screen but script error messages. The url won't open on IE. I tried using Process to start a new instance of IE but it won't work. I can't imagine why since it works perfectly fine in my webbrowser control. I guess this is the source of all problems. Something prevents it from working right when the url is sent to IE (btw ppDisp is null) – pzogr Sep 26 '12 at 21:27
  • @pzogr please copy and paste your entire NewWindow3 event, from start to end, including the code you have in it into your "Question" by editing your question, so that I can try the same thing on my machine and solve it. Please dont leave anything out in your paste. Also, just to help remind me, this is happening when you click on a link, correct? Or are you doing a .Navigate2 on this page directly? – Erx_VB.NExT.Coder Sep 27 '12 at 16:13
  • @pzogr Also, i just re-read your question, and noticed you want both browser tabs to be identified as IE9? Check out this answer I provided for someone else, I think it may actually solve your problem without needing to deal with NW events. Let me know if it does or doesn't. http://stackoverflow.com/questions/12490512/script-runs-slower-in-the-dotnet-webbrowser-control/12548037#12548037 – Erx_VB.NExT.Coder Sep 27 '12 at 16:15
  • The NewWindow event only gives u the option to cancel it by using the e.cancel = true/false; If u use axWebBrowser instead of WebBrowser then u also get the NewWindow2 and NewWindow3 events. There u get more options. In NewWindow2 the argument for e is defined as AxSHDocVw.DWebBrowserEvents2_NewWindow2Event. Substitute 2 for 3 and u get the NewWindow3 event. In NewWindow2 e parameter holds the following properties: cancel, ppDisp and in NewWindow3 it holds: bstrUrl, bstrUrlContext, dwFlags, cancel, ppDisp. More can be found in http://msdn.microsoft.com/en-us/library/aa768337%28v=vs.85%29.aspx – pzogr Oct 03 '12 at 07:55
  • Oh, I forgot... The problem arises when I click on the button "Print" which opens a new window and loads a pdf document. After all the searching I found out that the new window is identified as IE 9.0 indeed (I've done the appropriate registry changes) but it won't open the pdf. It won't open the pdf even if I get the Url by using the axWebBrowser and the more detailed NewWindow2 & 3 events and send it to a new instance of IE. It does work when I send the url to an instance of a webbrowser control! What could be wrong here? – pzogr Oct 03 '12 at 08:00
1

It is quite strange (from my perspective) that the WebBrowser control doesn't surface the much-more-useful NewWindow3 event.

This CodeProject article describes a remarkably simple way to make it available.

J.Merrill
  • 1,233
  • 14
  • 27