This article may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist. To maintain the flow of the article, we've left these URLs in the text, but disabled the links.
|
Take Total Control of Internet Explorer with Advanced Hosting Interfaces Scott Roberts |
With the Advanced Hosting Interfaces supported by Internet Explorer, programmers can actually drive the user interface and internals of the WebBrowser control. |
As part of the Internet Client Development group of Microsoft® Developer Support, I'm on the front line of answering questions about applications that use the WebBrowser control. Often, customers call to ask how they can customize parts of the WebBrowser user interface and control file downloading and application execution.
For example, a customer called me a couple of months ago regarding a problem with an app she had written that hosts the WebBrowser control. Her users could right-click in the WebBrowser window and choose View Source from the context menu, but she wanted to be able to hide her source code. Her users could also press Ctrl+O, causing the Open dialog to appear. Then, when they entered a URL in the dialog and pressed Enter, a new instance of the Microsoft Internet Explorer window would be started. She wanted to disable this functionality, so that her application would be used for all Internet and intranet browsing on a particular machine. Customizing the user interface of the WebBrowser control as well as extending the DHTML object model and controlling what is downloaded are all abilities provided by the Advanced Hosting Interfaces of Internet Explorer. These interfaces include IDocHostUIHandler, ICustomDoc, and IDocHostShowUI. A special ambient property is used to restrict what is downloaded and executed when you are hosting the WebBrowser control. Let's discuss the most important aspects of the Advanced Hosting Interfaces, as well as the ambient property used for download control. I will demonstrate these concepts with two samples.
Architecture
IDocHostUIHandler
|
|
Earlier I told you about the developer who didn't want to allow her users to view the source code of her Web pages. She hosted the WebBrowser control in her application and didn't offer a View Source menu item. Therefore, the only way that her users could view the source of her Web pages was by right-clicking on the Web page and choosing View Source from the context menu. To work around this problem, an application that is hosting the WebBrowser control and has implemented IDocHostUIHandler can use the ShowCon-textMenu method. This method is called by MSHTML any time a context menu has been requested (typically with a right mouse click). Your application can cancel the default context menu by simply returning S_OK from this function, which tells MSHTML not to show its own context menu. You can also display your own context menu at this point, if you choose. The following code turns off the default context menu by using the ShowContextMenu method: |
|
It turns out that there is another method of IDocHost-UIHandler that helped that developer work around another problem. Besides viewing her source code, users were pressing Ctrl+O to execute the Open dialog. Then they could type in a URL, causing a new instance of the Internet Explorer window to open. They could also accomplish this by pressing Ctrl+N. Here's where the TranslateAccelerator method comes to the rescue. This method is called from MSHTML's implementation of IOleInPlaceActiveObject::Trans-late-Accelerator or IOleControlSite::TranslateAccelerator. Using your implementation of IDocHostUIHandler::Trans-late-Accelerator, you can turn off all accelerator keys or just certain ones. To turn off all accelerators, simply return S_OK from this method.
If you want to turn off a specific accelerator key, when your TranslateAccelerator is called, check the LPMSG structure you receive for the accelerator key in question. If Translate-Accelerator was called in response to that key, simply return S_OK. The following code shows an implementation of TranslateAccelerator that turns off all accelerators: |
|
The WBCustomizer sample I'll describe later contains code to turn off specific accelerator keys.
In addition to giving you the ability to control the user interface of the WebBrowser control, IDocHostUIHandler lets you extend the Dynamic HTML object model of your Web page. This means that you can access methods and properties of your hosting application from within script on a Web page. For example, if you have a method in your hosting application called SayHello, you can call this method from a Web page that is loaded by your application: |
|
To resolve the SayHello method, the WebBrowser control goes through a few steps. First, it calls your IDocHostUIHandler::
GetExternal method to get a pointer to your implementation of IDispatch. Then, the WebBrowser calls your IDispatch::GetIDsOfNames function to retrieve the ID of the SayHello method. Finally, it calls your IDispatch::Invoke method and passes it SayHello's ID. When implementing Invoke, you need only have a switch that includes a case statement for the SayHello ID. When you receive this ID, you can do whatever you want with it. Figure 2 shows a sample implementation of GetExternal and Invoke. This was taken from the AtlBrowser sample that I'll cover later. Since AtlBrowser is implemented using the Active Template Library (ATL) and it inherits from IDispatchImpl, the Get-IDsOfNames function is taken care of by ATL.
ICustomDoc
|
|
Note that m_spWebBrowser is a smart pointer for the WebBrowser control.
I have since found that this customer's request is very common. A lot of developers who are creating applications that host the WebBrowser control with Visual Basic want to disable context menus and specific accelerator keys. As you know, you can do this by using two methods of IDocHost-UI-Handler: ShowContextMenu and TranslateAccelerator. However, you can't implement IDocHostUIHandler in Visual Basic. That is why I created WBCustomizer, a COM object that provides an IDocHostUIHandler implementation for Visual Basic-based applications. This COM object allows you to enable or disable context menus, all accelerators, or specific accelerators. Source code is available for the WBCustomizer COM object, the COM object itself compiled in release mode, and Visual Basic-based sample code that demonstrates it. WBCustomizer exposes the EnableAccelerator method and three properties: WebBrowser, EnableContext-Menus, and EnableAllAccelerators. The WebBrowser property is used to set WBCustomizer's internal reference to the WebBrowser control you are hosting. This property is crucial because it's through this reference that WBCustomizer is able to connect its implementation of IDocHostUIHandler to the WebBrowser control you are hosting. The EnableCon-textMenus and EnableAllAccelerators properties do just what they say, enabling or disabling context menus and all accelerators. The EnableAccelerator method allows you to enable or disable specific accelerator keys. This method takes the key code of the accelerator key, the extended key code for Ctrl, Alt, or Shift, and the state of the key (True or False depending on whether you want to enable or disable the accelerator key). To use WBCustomizer in your Visual Basic-based application, follow these steps:
|
|
|
|
|
|
|
|
|
Figure 4: VBCustomWB |
IDocHostShowUI
In addition to the user interface features described earlier, the WebBrowser control lets you customize its message boxes and help. The IDocHostShowUI interface gives you control of these items. This is another Advanced Hosting Interface that your WebBrowser hosting application can implement. Whenever the WebBrowser control needs to display a message box, MSHTML will call the ShowMessage method of your IDocHostShowUI implementation. You can then turn off the message box by returning S_OK. This method receives information about the message that you can use to display your own message. For instance, there may be a message displayed by the WebBrowser control that is confusing to your users. You can trap this message in the ShowMessage method and then display your own alternate message that pertains to your users. IDocHostShowUI also gives you control over help that the WebBrowser control displays. For example, when the user presses F1, MSHTML will call the ShowHelp method of your IDocHostShowUI implementation. As before, you can turn off this functionality by returning S_OK and optionally displaying your own help message. A sample implementation of IDocHostShowUI is given in the AtlBrowser sample that I will discuss shortly.
Download Control
AtlBrowser
|
Figure 6: AtlBrowser |
In addition to these menu items, AtlBrowser uses the IDoc-HostUIHandler::GetExternal method to extend the Dynamic HTML object model. AtlBrowser exposes a method I talked about earlier: SayHello. When you run AtlBrowser, navigate to the AtlBrowser.htm file that comes with the sample (see Figure 6). When you press the Say Hello button, the WebBrowser control calls the SayHello method implemented in AtlBrowser.
Summary
|
From the October 1998 issue of Microsoft Interactive Developer.