Override of WebBrowserSite IDocHostUIHandler using WebBrowser.CreateWebBrowserSiteBase doesn
According tohttp://msdn2.microsoft.com/en-us/library/system.windows.forms.webbrowser.createwebbrowsersitebase.aspx I should be able to customize the behavior of the System.Windows.Forms.WebBrowser control as follows:
"To use this feature, implement classes that inherit from theWebBrowser andWebBrowser.WebBrowserSite classes. The unmanagedWebBrowser ActiveX control uses the protectedCreateWebBrowserSiteBase method to retrieve extensibility interfaces implemented by theWebBrowser.WebBrowserSite class. Override theCreateWebBrowserSiteBase method to return an instance of your own class that inherits from theWebBrowser.WebBrowserSite class. TheWebBrowser.WebBrowserSite class provides default implementations of the OLEIDocHostUIHandler. You can provide your own implementation of this interface or implement any otherWebBrowser ActiveX control interface in order to customize the behavior of the control. If you provide your own implementation for anyIDocHostUIHandler members, you must implement all the members of that interface."
I've implemented my own classes and confirmed in debug that my version of CreateWebBrowserSiteBase is being called and creates my class. However, none of my IDocHostUIHandler methods are being called. I suspect that the problem may be in my constructor, which calls the WebBrowser.WebBrowserSite constructor, which creates a competing implementation of IDocHostUIHandler.
The documentation implies that this should work. Am I missing anything? Any suggestions or workarounds?
Thanks.
[2193 byte] By [
DonW] at [2007-12-28]
I'm currently in the problem "Disable Image Download (more specifically Download Control)" for the webbrowser control.
I read an article "Webbrowser customization" and found out that the methods there can only be implemented in C. I know a little about C, actually I know close to nothing about C. Now, I found another article about CreateWebBrowserSiteBase but don't understand understand how to use it.
Can I see your code?
By the way, I found a solution using a DLL(activex) I found on the internet that was designed and works just fine with VB6. But when I inserted the control in VB8 a get an error: "......[something not so long here]...... Make sure that the type is registered!"
I am sure that the type is registered! I ran "regsvr32 <dllfile>" and saw the msgbox that says "<dllfile> is now registered!" (or something like that).
So, I created an ActiveX Control in VB6. It's just a simple wrapper control that forwards all the events of the dll to the host application and all methods from the host application to the dll. But this approach is limited, as I have to forward all events, properties, functions and so on for the dll. And I get double Navigated event, and no "istoplevel" property in the Navigating event. If you want to take a look at it drop me a mail nihknihk a@t hotmail d.t com.
So the only solution I see is use the .Net Framework 2.0's WebBrowser control and its methods (i.e. CreateWebBrowserSiteBase), but as I've said, I can't make something out from that certain article.
So, Can I see your code?
In fact, you can not provide your own implementation of this IDocHostUIHandler interface class - it is an internal class in System.Windows.Forms.UnsafeNativeMethods and you can not derive from it. I also found my own IDocHostUIHandler implementation is not the same as the System.Windows.Forms.UnsafeNativeMethods implementation. I think this is the reason that the methods in your derived class are not being called.
The UnsafeNativeMethods::IDocHostUIHandler interface is reflected from System.Windows.Forms.
ref class WebBrowserEx:public WebBrowser
{
public
:void test(){
WebBrowserSiteBase^ site1 =
gcnew WebBrowserSite(this);UnsafeNativeMethods::IDocHostUIHandler^ pTest1 = (UnsafeNativeMethods::IDocHostUIHandler^)site1;
}};
[STAThread]
int
_tmain(int argc, _TCHAR* argv[]){
WebBrowserEx^ we=
gcnew WebBrowserEx();//An unhandled exception of type 'System.InvalidCastException' occurred in test1.exe
//Additional information: Unable to cast object of type 'WebBrowserSite' to type 'IDocHostUIHandler'.
we->test();return 0;}
You could implement IDocHostUIHandler2, in which case it will indeed be used from your class.
However, I haven't managed to provide such an implementation that won't fail the app with Access Violation by now.