文章来源: http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/overview/overview.asp?frame=true
关于浏览器
这部分说明Microsoft Internet Explorer 4.0及其后续版本结构的信息,在你重新使用这些部分时将十分有用.
包含以下主题.
结构概述
Internet Explorer 使用 Microsoft ActiveX 控制和动态文档接口连接不同部分.以下的图表提供了一个总体概括.
IExplore.exe 在顶层;举个例子当 Internet Explorer 装载时它只是一个小应用程序.应用程序执行时使用Internet Explorer部分完成导航,历史管理,收藏夹管理,HTML解析和翻译等等,当它提供工具条和框架支持时就成为一个卓越的浏览 器.IExplorer.exe直接面对Shdocvw.dll部分.
Shdocvw.dll运转在Mshtml.dll部分上,同样的其它动态文档部分(例如Microsoft Office应用)通过操纵一个指定文档类型,使它们能够适当装载进浏览器. Shdocvw.dll提供功能性关联,像导航,适当的连接,历史管理和PICS支持. 这个DLL同样开放接口,允许个别的ActiveX控制. Shdocvw.dll 部分更多的涉及到 WebBrowser Control. 对于 WebBrowser 控制,适当连接涉及到在HTML里单击一个连接载入文档和载入一个新的HTML文档等上述实例. 如果只有Msthml.dll存在,单击连接将导致一个新的浏览器实例.
Mshtml.dll is the component that performs the HTML parsing and rendering in Internet Explorer 4.0 and later, and it also exposes the HTML document through the Dynamic HTML Object Model. This component hosts the scripting engines, Microsoft virtual machine, ActiveX Controls, plug-ins, and other objects that might be referenced in the loaded HTML document. Mshtml.dll implements the Active Document server interfaces, which allows it to be hosted using standard Component Object Model (COM) interfaces.
As this is an OLE-based architecture, the ambient properties that are commonly used by ActiveX Controls can also be applied to the Internet Explorer components. In this way, a host of the WebBrowser Control can set an ambient property that will filter down to all the frames and controls hosted in the loaded document.
选择恰当的部分
The WebBrowser Control provides a rich set of functionality that a host typically requires, such as that for in-place linking. Therefore, it is much more applicable for most applications to host this control instead of MSHTML for browsing or viewing HTML documents. Hosting MSHTML is recommended only for specialized applications, such as parsing HTML. The WalkAll Sample Source Page demonstrates how to host MSHTML.
It should also be noted that although hosting MSHTML is slightly more lightweight than hosting the WebBrowser Control, the savings rarely justify the extra work involved in implementing functionality that is already available in the WebBrowser Control. It is very likely that the WebBrowser Control will already be loaded in memory, and navigating to a frameset page will also result in the WebBrowser Control being loaded as part of the standard working set.
假如需要额外控制
Hosts of the WebBrowser Control and MSHTML components have control over certain functionality. In the case of the WebBrowser Control, this includes navigation, as well as receiving events when the document is loaded. Hosts of either component can obtain extra control over functionality by implementing the IDocHostUIHandler and IDocHostShowUI interfaces. These interfaces are commonly used to override the context menus that are supplied by default for the browser. Their uses also include setting the 3-D border, overriding the location in the registry where options are stored, and extending the Dynamic HTML Object Model.
The component obtains these interfaces from the host by calling QueryInterface on the IOleClientSite interface implemented by the hosting application.
控制上下文菜单
A common requirement of hosting the WebBrowser Control is the ability to override or add to the context menus that are displayed as the result of a right-click in the browser window. This is of particular interest to applications that are using the WebBrowser Control to view rich content but do not want the user to know that HTML is being viewed. This is also advantageous for applications that do not want the user to be able to view the HTML source for the content.
There are two techniques available to achieve this. The first involves the use of the IDocHostUIHandler interface and allows an application to disable or replace the context menus. The second technique involves the use of the registry and allows the existing context menus to be extended.
Overriding the Context Menus
The WebBrowser Control's context menus can be overridden entirely by implementing the IDocHostUIHandler::ShowContextMenu method. Returning E_NOTIMPL or S_FALSE from this method indicates to the WebBrowser Control that it should display its own standard context menu. However, returning S_OK causes the WebBrowser Control not to display its menus, and it assumes that the host has performed the appropriate action. The host can disable all context menus or bring up its own context menus. The parameters supplied to the host that implements this method allow that host to identify which of the default menus will be displayed by the WebBrowser Control, as well as the coordinates where the menu will be displayed. This provides the host the full context for the menu. For example, the host can choose to override only the image context menus and not the standard context menus.
Adding to the Standard Context Menus
Items can be added to the existing context menus of the WebBrowser Control by placing entries in the registry and linking these to URLs that execute script. To add items to the standard WebBrowser Control context menus, create or open the following key:
- HKEY_CURRENT_USER
- Software
- Microsoft
- Internet Explorer
- MenuExt
Under this key, you create a key whose name contains the text you want displayed in the menu. The default value for this key contains the URL that will be executed. The key name can include the ampersand (&) character, which will cause the character immediately following the & to be underlined. The URL will be loaded inside a hidden HTML dialog box, all the inline script will be executed, and the dialog box will be closed. The hidden HTML dialog's menuArguments property (on the external object) contains the window object of the window on which the context menu item was executed.
The following registry entry adds an item with the title "My Menu Item" to the WebBrowser Control context menu and executes the inline script contained in the file c:"myscript.htm.
- HKEY_CURRENT_USER
- Software
- Microsoft
- Internet Explorer
- MenuExt
- My &Menu Item
- (Default) = file://c:"myscript.htm
The contents of c:"myscript.htm are as follows:
<SCRIPT LANGUAGE="JavaScript" defer>
var parentwin = external.menuArguments;
var doc = parentwin.document;
var sel = doc.selection;
var rng = sel.createRange();
var str = new String(rng.text);
if(str.length == 0)
rng.text = "MY INSERTED TEXT";
else
rng.text = str.toUpperCase();
</SCRIPT>
This script obtains the parent window object from external.menuArguments. The parent window object is the WebBrowser Control in which the context menu item was executed. The script then obtains the current selection and, if no selection is present, inserts the text "MY INSERTED TEXT" at the point where the context menu was executed. If there is a selection present, the selected text is changed to uppercase.
Optional keys
Under the item registry key created earlier, there are a couple of optional values. One of these specifies on which context menus this item will appear. The other specifies that the script should be run as a dialog box.
The "Contexts" DWORD value specifies the context menus in which an item will appear. This value is a bit mask consisting of the logical OR of the following values (defined in Mshtmhst.h). These values correspond to the constant passed in an IDocHostUIHandler::ShowContextMenu call.
Value | Constant Name | Description |
---|---|---|
0x01 | CONTEXT_MENU_DEFAULT | Shown on all context menus. |
0x02 | CONTEXT_MENU_IMAGE | Context menu of images only. |
0x04 | CONTEXT_MENU_CONTROL | Context menu of form controls only. |
0x08 | CONTEXT_MENU_TABLE | Context menu of tables only. |
0x10 | CONTEXT_MENU_TEXTSELECT | Context menu of selected text only, including images in a selected region. |
0x20 | CONTEXT_MENU_ANCHOR | Context menu of links only. Does not include linked images or image maps. |
0x40 | CONTEXT_MENU_UNKNOWN | Right-click on none of the above. |
So if, for example, you want this simple extension to appear only in the default menu and the text selection menu, you could create a DWORD value in the registry under the My Menu Item key called "Contexts" and set it to 0x11. From C/C++ code, this can be expressed as:
CONTEXT_MENU_DEFAULT | CONTEXT_MENU_TEXTSELECT
The other optional registry DWORD value is "Flags". There is only one valid bit (0x1) for this registry value; it is defined as MENUEXT_SHOWDIALOG in Mshtmhst.h. When this bit is set, the script is run just as if it had been called through the IHTMLWindow2::showModalDialog method. The window that runs the script is not hidden, and the dialog box is not automatically closed after inline and onload script finishes. The external.menuArguments value still contains the window object where the user selected the menu item.
The context menu event
Whenever a context menu extension is triggered, the event object off the main window (external.menuArguments.event) contains information about where the user clicked and which context menu was shown. The mouse coordinates are valid along with srcElement. The type value contains one of the following strings, indicating which context menu was shown to the user:
- MenuExtDefault
- MenuExtImage
- MenuExtControl
- MenuExtTable
- MenuExtTextSelect
- MenuExtAnchor
- MenuExtUnknown
Another example
This example creates a new menu item on the default context menu. This item, called Show In New Window, can be used to launch a new window that displays only a specific portion of the current document. If something is deeply nested in a frameset, you can easily launch a specific frame in its own window.
Here are the contents of a .reg file that can be run to insert the correct registry settings. Call this file Example2.reg. Double-clicking this file in Windows Explorer will insert the settings in your registry.
REGEDIT4
[HKEY_CURRENT_USER"Software"Microsoft"Internet Explorer"MenuExt"Show in New Window]
@="file://c:""example2.htm"
"Contexts"=dword:00000001
Here are the contents of c:"example2.htm:
<SCRIPT LANGUAGE="JavaScript" defer>
window.open(external.menuArguments.location.href);
</SCRIPT>
For more information, see Adding Entries to the Standard Context Menu.
扩展动态HTML对象模型
It is possible for the hosting application to extend the Dynamic HTML Object Model so that scripts can refer to functionality implemented by the host. Such scripts refer to the host by specifying the external object that is available from the window object. For example, a reference to "window.external.speech" will call the host to resolve the name "speech." All standard script within the document will be executed normally.
This extension mechanism is implemented in the host by providing an IDispatch interface for the object model extension that will have IDispatch::GetIDsOfNames and IDispatch::Invoke called on it to resolve any references to the external object. The IDispatch that the host provides is obtained by the WebBrowser Control or MSHTML component by calling the host's IDocHostUIHandler::GetExternal method.
For an example of how to extend the Dynamic HTML Object Model, see the Driller Sample Source Page .
下载控制
Hosts can control certain aspects of downloadingframes, images, Java, and so onby implementing both IOleClientSite and an ambient property defined as DISPID_AMBIENT_DLCONTROL. When the host's IDispatch::Invoke method is called with dispidMember set to DISPID_AMBIENT_DLCONTROL, it should place zero or a combination of the following values in pvarResult.
DLCTL_BGSOUNDS | The browsing component will play background sounds associated with the document. |
DLCTL_DLIMAGES | The browsing component will download images from the server. |
DLCTL_DOWNLOADONLY | The browsing component will download the page but not display it. |
DLCTL_FORCEOFFLINE | The browsing component will always operate in offline mode. This causes the BINDF_OFFLINEOPERATION flag to be set even if the computer is connected to the Internet when making requests through URLMON. |
DLCTL_NO_BEHAVIORS | The browsing component will not execute any behaviors. |
DLCTL_NO_CLIENTPULL | The browsing component will not perform any client pull operations. |
DLCTL_NO_DLACTIVEXCTLS | The browsing component will not download any ActiveX Controls in the document. |
DLCTL_NO_FRAMEDOWNLOAD | The browsing component will not download frames but will download and parse the frameset page. The browsing component will also ignore the frameset, and will render no frame tags. |
DLCTL_NO_JAVA | The browsing component will not execute any Java applets. |
DLCTL_NO_METACHARSET | The browsing component will suppress HTML Character Sets reflected by meta elements in the document. |
DLCTL_NO_RUNACTIVEXCTLS | The browsing component will not execute any ActiveX Controls in the document. |
DLCTL_NO_SCRIPTS | The browsing component will not execute any scripts. |
DLCTL_OFFLINE | Same as DLCTL_OFFLINEIFNOTCONNECTED. |
DLCTL_OFFLINEIFNOTCONNECTED | The browsing component will operate in offline mode if not connected to the Internet. This causes the BINDF_GETFROMCACHE_IF_NET_FAIL flag to be set if the computer is connected to the Internet when making requests through URLMON. |
DLCTL_PRAGMA_NO_CACHE | The browsing component will force the request through to the server and ignore the proxy, even if the proxy indicates that the data is up to date. This causes the BINDF_PRAGMA_NO_CACHE flag to be set when making requests through URLMON. |
DLCTL_RESYNCHRONIZE | The browsing component will ignore what is in the cache and ask the server for updated information. The cached information will be used if the server indicates that the cached information is up to date. This causes the BINDF_RESYNCHRONIZE flag to be set when making requests through URLMON. |
DLCTL_SILENT | The browsing component will not display any user interface. This causes the BINDF_SILENTOPERATION flag to be set when making requests through URLMON. |
DLCTL_URL_ENCODING_DISABLE_UTF8 | The browsing component will disable UTF-8 encoding. |
DLCTL_URL_ENCODING_ENABLE_UTF8 | The browsing component will enable UTF-8 encoding. |
DLCTL_VIDEOS | The browsing component will play any video clips that are contained in the document. |
安全管理
Hosts of the browsing components can implement their own security management and override the settings that exist for the WebBrowser Control. This is achieved by implementing the IInternetSecurityManager interface. The browsing component will obtain this interface by calling the host's IServiceProvider::QueryService method with SID_SInternetSecurityManager. For more information on security management, see About URL Security Zones.