Browser Integration Teaching Custom Browser

zhaozj2021-02-08  286

Herein translation

Http://msdn.microsoft.com/workshop/browser/hosting/wbcustomization.asp

This article is located

Http://blog.9cbs.net/jiangsheng/archive/2004/11/07/170742.aspx

Custom browser

This tutorial provides some ways to customize the behavior and appearance of your browser control. 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 Requirements Introduction Browser Custom Architecture IdochostuiHandler IdochostuiHandler2 GetoptionKeyPath and GetOverrideKeypath comparison Control Navigation IDOCHOSTSHOWUI Control Download and Execute IHostDialoghelper Control New Window Conclusion

Premise and demand

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 here at http://www.microsoft.com/msdownload/platformsdk/sdkupdate/)

Many custom features can be used in IE5 or 5.5, but there are several features that require IE6. Before using a feature, you should check the reference document to get version information.

Introduction

Integrated browser controls are 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 Creative way

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 control, 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 may look like this:

example

/ / Omitted the error check for the sake of clear

CComptr spoleobj;

// Create webbrowser - Save pointer in class member variable m_spwebbrowser

CoCreateInstance (CLSID_WebBrowser, Null, Clsctx_inProc, IID_IWEBBROWSER2, (Void **) & m_spwebbrowser;

/ / Query the IoleObject interface of WebBrowser

m_spwebbrowser-> queryinterface (IID_ID_IOLOBJECT, (Void **) & SPoleObj);

/ / Set the user site

Spoleobj-> setClientSite (this);

// Local activation browser control

Rect rcclient

GetClienceRect (& rcclient);

SpoLeobj-> Doverb (Oleiverb_inplaceActivate, Null, this, 0, GetTopy (), & rcclient);

/ / Container intercept browser event registration

Atladvise (M_SpWebBrowser, getunknown (), diid_dwebbrowserevents2, & m_dwcookie;

// Navigate to the startup page

m_spwebbrowser-> navigate (l "res: //webhost.exe/startpage.htm", null, null, null, null; however, if your application does not have a IoleClientSite interface, you have not lost all hopes. IE provides an icustomed interface so you can pass your IDOCHOSTUIHANDLER interface to your browser. You can't use the IDochostuiHandler2 and IDochostShowui interface without providing a browser control host's IoleClientSite interface.

Translator Note:

The class ColeControlContainer and a large pile of DHTML classes in the MFC7 have gotten their dizzy. In the end, I have to give up my implementation of IoleClientSite, and explicitly set the IDOCHOSTUIHANDLER interface through ICUSTOMDOC. This must be able to start a custom browser after the first page download is completed, because the object exposed to the ICustomDoc interface is only available after the first page is downloaded. An example of an icustomDoc can be found at the 9CBS document center, the URL is http://www.9cbs.net/develop/read_article.asp?id=8813

When the browser control gets a pointer to any of these interfaces, the method of the interface is called in the life of the browser control when appropriate. For example, when the user right-click anywhere in the client area of ​​the browser control, your IDOCHOSTUIHANDLER :: ShowContextMenu's implementation will be called before IE displaying its default shortcut menu. This gives you an opportunity to display your own shortcut menu and cancel the shortcut menu of IE.

Translator Note: Some examples of screen shortcut menu can be found at the 9CBS document center, the URL is http://www.9cbs.net/develop/Article/18/18541.shtm

When you initialize the browser control, you can remember several key points. Your application should use oleinitialize instead of COINITIALIZE to launch COM. Oleinitialize enables scrapbook support, drag and drop, object connections and embedded (OLE) and local activation. Use OleunInInitialize to close the COM library when your app is over.

The ATL COM wizard uses Coinitialize instead of oleinitialize to open the COM library. 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 (Translator Note: Originally, the document of the interface seems to pass the variant_false), avoid anything in your browser 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. IDOCHOSTUIHANDALER

IdochostuiHandler 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.

IDOCHOSTUIHANDAL :: gethostinfo

You use idochostuihandler :: gethostinfo to tell MSHTML About your application's ability 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 automatic function of your browser

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: #ffcccc}";

Wchar * szns = l "IE; mytags; mytags2 = 'www.microsoft.com'";

#define cchmax 256

SIZE_T CCHLENGTHCSS, CCHLENGTHSZNS;

HRESULT HR = Stringcchlengthw (SZCSS, CCHMAX, & CCHLENGTHCSS)

// TODO: Handling an error here.

OLECHAR * PCSSBuffer = (OLECHAR *) CotaskMemalloc (cchlengthcss 1) * sizeof (OLECHAR));

// TODO: The error is handled here, determines 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: The error is handled here, determines 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 don't need to tell MSHTML, you can return E_NOTIMPL in this method.

IDOCHOSTUIHANDLER :: ShowContextMenu

By implementing this method, 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.

example

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 SPCT;

CComptr spwnd;

Menuiteminfo mii = {0};

CComvariant Var, Var1, Var2;

HR = pcmdtarget-> queryinterface (IID_IOLECOMMANDTARGET, (VOID **) & SPCT);

HR = pcmdtarget-> queryinterface (IID_IOLEWINDOW, (Void **) & SPWND);

HR = SPWND-> getWindow (& hwnd);

Hinstshdoclc = loadingLibrary ("shdoclc.dll"); if (hinstshdoclc == null)

{

// Load module error - failure to fail safely

Return;

}

HMENU = LoadMenu (HinstshdoClc,

MakeintResource (iDR_Browse_Context_Menu);

HMENU = GetSubmenu (HMENU, DWID);

// Get the language submenu

HR = SPCT-> EXEC (& CGID_SHELLDOCVIEW, SHDVID_GETMIMECSETMENU, 0, NULL, & VAR);

mii.cbsize = sizeof (mii);

mii.fmask = miim_submenu;

Mii.hsubmenu = (hmenu) var.byref;

// Add language submenu to encoding context menu

SetMenuiteminfo (HMENU, IDM_LANGUAGE, FALSE, & MII);

// Insert the shortcut menu extension from the registry

V_vt (& var1) = VT_INT_PTR;

V_byref (& var1) = HMENU;

V_vt (& var2) = vt_i4;

V_i4 (& var2) = dwid;

HR = SPCT-> EXEC (& CGID_SHELLDOCVIEW, SHDVID_ADDMENUEXTENSIS, 0, & VAR1, & VAR2);

// Delete View source code

DeleteMenu (HMENU, IDM_VIEWSOURCE, MF_BYCOMMAND);

/ / Show shortcut menu

INT ISELECTION = :: TRACKPOPUPMENU (HMENU,

TPM_LEFTALIGN | TPM_RightButton | TPM_RETURNCMD,

PPT-> X,

PPT-> Y,

0,

HWnd,

(RECT *) NULL);

// Send the selected shortcut menu project command 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.

IDOCHOSTUIHANDLER :: getExternal: Extension Document Object Model

IdochostuiHandler provides a method of expanding your own objects, methods, and features that you implement in your own application. Method (DOM). 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;

Return S_OK;

}

As long as MSHTML has a pointer to your IDispatch, MSHTML will pass the automation method of call to your application on any external object: