This article is translated from http://msdn.microsoft.com/library/default.asp?url=/workshop/browser/hosting/wbcustomization.asp Custom Browser This tutorial provides some behavior and appearance of custom browser controls method. You will see advanced host interfaces, IDochostuihandler, IDochostuihandler2, IDochostshowui, and icustomDoc. This article also discusses other custom methods, such as processing DISPID_AMBIENT_DLCONTROL in the host IDispatch implementation; and uses IhostDialoghelper. This article is divided into the following sections
Premise and demand introduction browser from comparison control navigation defined architecture IDocHostUIHandler IDocHostUIHandler2 GetOptionKeyPath and GetOverrideKeyPath of IDocHostShowUI control to download and execute IHostDialogHelper control the new window displays the certificate dialog box (New!) Information Bar (New!) Premise and conclusion needs to understand and use This tutorial, you need
In-depth understanding of C and COM, familiar with Active Template Library (ATL) installed Microsoft (R) Internet Explorer (IE) 6 or higher developing environment has header files and library files for IE6 or higher; especially MSHTMHST (Translator Note: You can download the latest Internet development SDK in http://www.microsoft.com/msdownload/platformsdk/sdkupdate/) Many custom features can be used in IE5 or 5.5, but There are several characteristics that require IE6. Some features require IE6's Windows XP SP2 version. Before using a feature, you should check the reference document to get version information. Introduction Integrated browser control is a powerful tool for fast software development. By becoming a browser host, you can display information and develop a user interface using Dynamic HTML (DHTML), HTML, and Extensible Markup Language (XML) that is easy to use. However, the behavior of the browser control may not be in line with your needs. For example, the default status allows users to view the source code option of the displayed page through the shortcut menu, you may need to disable or simply remove this option. You may further need to replace the default shortcut menu with your own shortcut menu. In addition to the customization characteristics just mentioned, the advanced host features allow
The buttons and other controls on the displayed page can call the built-in method of your application to effectively expand the DHTML object model (DOM). Changing the drag and drop behavior limits the navigation of the browser, for example, is limited to the specified page / domain, or the site capture user typed and processes when needed. For example, you may need to capture Ctrl O to prevent users from opening a web page in the new IE instead of using your program to open, change the default font and display settings to control the download content, and the processing of browsers after the download is completed. For example, you might disable video playback, script execution, and open a new window when you click on the link, or the Microsoft (R) ActiveX control download and execution. Limit View Source Codes Capture Search Capture Navigation Error Replace / Modify Shortcut Menu or Disable, Replace, Customize, or Add Express Necks to change the registry to change the registry to set control and modify the browser control display message box control new window In the following sections, we will see most, but not all of these possibilities and discuss how to implement them. Browser Custom Architecture Introduction IdochostuiHandler, IdochostuiHander2, IdochostShowui, and ICUSTOMDOC The following three interfaces are custom cores of the browser control user interface: IDOchostuiHandler, IDochostuiHandler2 and IDochostShowui. When you modify your browser control, these are interfaces you implement in your application. There are also some service interfaces. ICUSTOMDOC is implemented by MSHTML and provides a method to customize browser controls in some cases. IHOSTDIALOGHELPER provides a method to open the trusted dialog box, which is not marked for them (the translator's note: on the title bar) like the IE dialog. In addition to using these interfaces, you can also do other two things. First, you can control the download by intercepting the environmental characteristics in the iDispatch implementation; secondly, you can control the creation of the window by intercepting the DISPID_NewWindow2 in the iDispatch implementation. Translator Note: DHTML classes in MFC7, such as ChtmlView and CDHTMLDialog implement these interfaces, but for programmers using other class libraries, these interfaces may be required. How to work When a container provides support for ActiveX controls, the browser control custom mechanism is designed to be automated. When the browser control is instantiated, if possible, it tries to find IDOchostuiHandler, IDOchostuiHandler2, and IDochostShowui implementations from the host. The browser control is found by calling a QueryInterface method of the host's IoleClientSite interface. Translator Note: IE5.5 has a bug without querying the implementation of the IDOCHOSTUIHANDLER2 interface, which makes the host program not override the default parameters. For more information, refer to Microsoft Knowledge Base Article Q272968 BUG: IDochostuiHandler2 is not called in the browser control. This structure is automatically working for an application that implements an IoleClient interface. By calling the browser's IoleObject :: setClientSite method to the browser control an IoleClientSite interface.
A typical instantiation of the browser control might look like this: example // For explicitly, omissions Check CComptr
If you use this wizard to build a runable program, you need to change the Coinitialize and CounInitialize calls to Oleinitialize and OleunInInitialize. For a Microsoft Basic Class (MFC) application, make sure your application calls AFXoleinit, which calls Oleinitialize in its initializer. If you don't need to support drag and drop in your app, you can call iWebBBrowser2 :: registerASDROPTARGET, pass the variant_true (the translator) Drag and drop operation on the control instance. A browser control host application also requires an implementation of IoleInplaceSite, because IoleInPlaceSite is derived from IoleWindow, an application will require an implementation of IoleWindow. You need these implementations that make your application have a window, display browser control, and handling its display settings. The implementation of these interfaces and IoleClientSite may be minimal or absent. All methods of iOleClientSite can return E_NOTIMPL. Some IoleInPlaceSite and IoleWindow methods require an implementation to override the return value. You can view the minimum implementation of IoleInPlaceSite and IoleWindow in the sample code. Since we have completed the preparation of initialization, let's take a look at each interface that the browser control is customized. IdochostuiHandleridochostuiHandler has been available from IE5. It provides 15 methods. In general, some of the more important of these is IDocHostUIHandler :: GetExternal, IDocHostUIHandler :: GetHostInfo, IDocHostUIHandler :: GetOptionKeyPath, IDocHostUIHandler :: ShowContextMenu, and IDocHostUIHandler :: TranslateAccelerator. Of course, the method will depend on your application. IdochostuiHandler :: gethostinfo You use idochostuihandler :: gethostinfo to tell MSHTML About your application's capabilities and needs. Through it, you can control a lot of things, for example:
You can disable the 3D edge of the browser. You can avoid scroll bar or change their appearance. You can disable scripts. You can define the way you double-click processing. You can disable the browser's auto-completion function IDOchostuiHandler :: gethostinfo has a parameter, a pointer to the DochostuiInfo structure allocated by MSHTML. Your job is to populate information you pass to MSHTML in the structure. There are four members of the DochostuiInfo structure. The first member is CBSIZE, which is the size of the structure. You should set it like the sample code below. The second member is DWFLAGS, which is consisting of a numerical bit from the Dochostuiflag enumeration. The third member is a value from DWDouBleClick, a value from the DochostuidBlClk enumeration. The fourth member is Pchhostcss. You can set PChostCSS to a string of a string of a global style sheet (CSS) rule applying the page displayed by the browser control. The last member of DochostuiInfo is Pchhostns. You can set the semicolon-separated namespace list string that you provide. Use this member when you are using a custom label on the page displayed in the browser control. This way you can declare a global namespace list without having to declare them on each displayed page. Determine the use of CotaskMemalloc to assign strings for PchhostCSS or PChostns. (Translator Note: It seems that the caller releases these strings with cotaskmemfree). Example HRESULT GETHOSTINFO (DochostuiInfo * Pinfo) {wchar * szcss = l "body {background-color: #ffccccc}"; wchar * szns = l "ie; mytags; mytags2 = 'www.microsoft.com'"; #define cchmax 256 size_t cchlengthcss, cchlengthszns; HRESULT HR = Stringcchlengthw (SZCSS, CCHMAX, & Cchlength) // Todo: Handling errors here. OLECHAR * PCSSBuffer = (Olechar *) CotaskMemalloc ((cchlengthcss 1) * sizeof (OLECHAR)); // Todo: Handling an error here, determining that the memory is successfully assigned. HR = StringCchlengthw (SZNS, CCHMAX, & Cchlengthszns) // TODO: Handling an error here. OLECHAR * PNSBuffer = (OLECHAR *) CotaskMemalloc (Cchlengthszns 1) * SizeOf (OLECHAR)); // Todo: Handling an error here, determining that the memory is successfully assigned. HR = stringcchcopyw (pcssbuffer, cchlength) // TODO: Handling an error here. HR = stringcchcopyw (PNSBuffer, Cchlengthszns 1, SZNS) // Todo: Handling an error here.
pInfo-> cbSize = sizeof (DOCHOSTUIINFO) pInfo-> dwFlags = DOCHOSTUIFLAG_NO3DBORDER | DOCHOSTUIFLAG_SCROLL_NO | DOCHOSTUIFLAG_ENABLE_FORMS_AUTOCOMPLETE; pInfo-> dwDoubleClick = DOCHOSTUIDBLCLK_DEFAULT; pInfo-> pchHostCss = pCSSBuffer; pInfo-> pchHostNS = pNSBuffer; return S_OK;} If you do not have what it takes Tell MSHTML, you can return E_NOTIMPL in this method. IDOCHOSTUIHANDLER :: ShowContextMenu You get the control of the shortcut menu displayed by the browser control when a user is right-click. You can return the default shortcut menu from IE to the S_OK to block IE in this method. Returns some other values, like S_false or E_NOTIMPL, allowing IE to continue to perform its default shortcut menu behavior. If you only return S_OK in this method, you can avoid the right-click behavior of any browser control. This may be all of you in many occasions, but you can do more. Typically, you use this method to generate and display your own shortcut menu before returning S_OK. If you know the resources of the browser control displayed, how do it choose them, you can also customize the default browser control shortcut menu. Let us see how it works. The browser control is obtained by ShDoclc.dll. It is a shortcut menu resource. This knowledge and some #define give you a chance to manipulate the browser. Let us exist that you are assumed to be satisfied with the default menu, except that you want to remove the source code item. The following code loads the browser control shortcut menu resource from SHDOCLC.DLL. Select the correct menu according to the environment, remove the menu item corresponding to the IDM_VIEWSource command, and display the menu.
Examples HRESULT CBrowserHost :: ShowContextMenu (DWORD dwID, POINT * ppt, IUnknown * pcmdTarget, IDispatch * pdispObject) {#define IDR_BROWSE_CONTEXT_MENU 24641 #define IDR_FORM_CONTEXT_MENU 24640 #define SHDVID_GETMIMECSETMENU 27 #define SHDVID_ADDMENUEXTENSIONS 53 HRESULT hr; HINSTANCE hinstSHDOCLC; HWND hwnd; HMENU hMenu ; CComPtr
Y, 0, hwnd, (rect *) null); // Send the selected shortcut menu item instruction to the housing LRESULT LR = :: SendMessage (hWnd, wm_command, ISELECTION, NULL); freelibrary (hinstshdoclc); return s_ok;} Safety Warning: Incorrectly using LoadLibrary can load the wrong dynamic link library (DLL) to threate your application security. With regard to how to load DLL information on how different versions of Microsoft Windows, refer to LoadLibrary documents. IDOCHOSTUIHANDAL :: getExternal: Extension Document Object Model IdochostuiHandler provides a method that allows you to use your own objects, methods, and features implemented in your own application. Your implementation is to provide an IDispatch interface pointer to your MSHTML, pointing to your custom COM automation object, implementing your custom objects, properties, and methods. These objects, features and methods can be accessed through an external object of the document in any page displayed by the browser control. The implementation of this method can be very simple, assuming your IDispatch interface is on the same object that implements the idochostuihandler. HRESULT CBROWSERHOST :: getExternal (iDispatch ** ppdispatch) {* ppdispatch = this; returnid;} As long as MSHTML has a pointer to your IDispatch, MSHTML will pass the call to any external object to your application on the web page. Automation method: