IWebBrowser2 issues - how to open documents in new windows?
Asked Answered
O

2

1

I have IWebBrowser2 ctrl embedded into my own dialog. I want to simply display a promo banner within it from my url. How to disable all popup menu items from the control and force it to open links in new window (currently when I click on link in the banner, it is being opened within the same control). Regards Dominik

Orethaorferd answered 22/9, 2009 at 12:44 Comment(0)
V
2

Have a look at the following article:

WebBrowser Customization

Vallie answered 22/9, 2009 at 23:45 Comment(1)
The MSDN article is a good resource, but it doesn't answer this question. MSDN explains how to prevent new windows, but not how to force new windows to be opened. The docs imply you could do this by hooking the DISPID_BEFORENAVIGATE2 event, but that event doesn't have enough context to differentiate the user clicking a link from script-based activity. For example, if you load amazon.com, the initial page load will trigger a bunch of other requests that result in BeforeNavigate events of their own.Leakage
I
2

I don't know if there is a more convenient way of doing this - but you could always intercept BeforeExplorerNavigate2(), set the out-parameter cancel to true and from there either do a new Navigate() with a different target frame name or open a new window.

As Rob pointed out, there might be problem with filtering out navigate events originating from scripts, see this question.

Interrupt answered 22/9, 2009 at 14:35 Comment(8)
How to register for IWebBrowser2 events? I have only IWebBrowser2 object.Orethaorferd
You implement DWebBrowserEvents2 and register as an event sink for this. Through Remys article you'll find the NewWindow3() event, which should give you what you need.Interrupt
Ok. Thanks, I've found it althought in WTL it is not as easy as in MFC.Orethaorferd
Georg, see my comment above-- this question is still not answered. I'm trying to do this same thing and so far I have yet to find a reliable solution. The key problem with trapping DISPID_BEFORENAVIGATE2 is that you can't tell if it's user-initiated or script-initiated.Leakage
@Rob: Obviously the question was answered for the OP... Anyway, you can identify the frame from which the request originated via the pDisp parameter - if you don't know how open a new question about it.Interrupt
Thanks Georg! I didn't mean to imply the answers weren't helpful for the OP, but the question is interesting to more than just the OP. This post is one of the top hits on Google and it would be great if there were a complete answer for the other hundreds of programmers researching the issue. pDisp doesn't help. With some URLs e.g. Amazon you'll get BeforeNavigate2 events for scripts and user events on the same frame. IHlinkFrame may work: msdn.microsoft.com/en-us/library/aa767939%28v=VS.85%29.aspxLeakage
@Rob: Alright, you could have mentioned that right away, i didn't stumble upon that. You should open a new question about that specific problem which i'm happy to link to prominently.Interrupt
Georg, see #2925779Leakage
V
2

Have a look at the following article:

WebBrowser Customization

Vallie answered 22/9, 2009 at 23:45 Comment(1)
The MSDN article is a good resource, but it doesn't answer this question. MSDN explains how to prevent new windows, but not how to force new windows to be opened. The docs imply you could do this by hooking the DISPID_BEFORENAVIGATE2 event, but that event doesn't have enough context to differentiate the user clicking a link from script-based activity. For example, if you load amazon.com, the initial page load will trigger a bunch of other requests that result in BeforeNavigate events of their own.Leakage

© 2022 - 2024 — McMap. All rights reserved.