ATL implements customized IE browser bar, toolbar, and desktop Toolbar

Source: Internet
Author: User

ATL implements customized IE browser bar, toolbar, and desktop Toolbar

Author: Instructor Yang

Download source code

Keywords: Band, desk band, explorer band, tool band, browser bar, toolbar, desktop Toolbar

I. Introduction
Recently, due to work requirements, I need to do some development work on IE. So I read some information on msdn and, according to the description on msdn, I used ATL to successfully complete the task assigned by the "capitalist Boss.
(And while sleeping during the day, I dreamed my boss would pay me ......)
Now, I have translated the original document on msdn and translated it to my friends on vckbase.

  • Concept
  • Principle
    Basic band object
    Required COM interface
    Ipersiststream
    Iobjectwithsite
    Ideskband, idockingwindow, iolewindow
    Select the implemented COM interface
    Band Object Registration
  • ATL implementation

Ii. Concepts
In the process of translation, there are two words that are hard to understand. The first word is the band object, which is translated into the dictionary as "inlaid edge, skirt edge, strap, band ...... "I have a limited level of English, but I really don't know why the translation should be more appropriate. So I decided to use the word "band" in the following discussion! (What? Didn't you understand? I mean, I don't translate this word.) But how should the band object understand it? See Figure 1:


Figure 1

The red circle in Figure 1 is called "vertical browser bar", "Horizontal browser bar", "toolbar", and "desktop toolbar ". The "bar" can be displayed or hidden in IE's "View" menu or context shortcut menu by right-clicking. The implementation of these interface windows is actually to implement a COM interface object, which is called band. This concept can only be discussed. I cannot translate it into an object that is always on the side of the IE main window in my article? Pai_^
In addition, there is also a word called site. This is a good translation, called "Site "!. Haha, I dare to make a pass. If you want to understand the meaning of this translation in Computer Articles, you can only congratulate you. Your wisdom is too high. (They are all computer software learners. Why is the gap between people ?) In this article, the site can be understood as follows: the main frame of IE is like a "Bus Station", and those Band objects are like "cars ". The band car can always be parked at the bus station. Therefore, the site is the site, which is also the object of the COM interface (iobjectwithsite, iinputobjectsite ).

Iii. Principles

3.1 basic band object
Band object, which is supported from shell 4.71 (IE 5.0. Band is a COM Object and must be placed in a container. Of course, using them is the same as using a normal window. IE is a container, and desktop shell is also a container. They provide different function functions, but the basic implementation is similar.
There are three types of band objects: Explorer bands, tool bands, and desk bands: vertical and horizontal. So how does IE and shell differentiate and load these bands objects? The method is: register different component types (catids) in the Registry for different band objects ).

Band Style

Component Type

Catid

Vertical browser bar Catid_infoband 00021493-0000-0000-c000-000000000046
Horizontal browser bar Catid_commband 00021494-0000-0000-c000-000000000046
Desktop Toolbar Catid_deskband 00021492-0000-0000-c000-000000000046

The IE Toolbar does not use the component type registration, but uses the registration method for CLSID during registration. For details, see 3.3.
In the example program, all four types of band objects are implemented. The vertical browser bar (cverticalbar) displays an HTML file, and functions such as browsing the Web page in the IE main window are implemented; the horizontal browser bar (chorizontalbar) is an editing window that synchronously displays the body source file content of the current webpage. The IE Toolbar (ctoolbar) is the simplest, but an empty toolbar is added; the desktop toolbar provides a single-line editing window. You can enter the command line or file name on it. After you press enter, it will execute the shell opening action.

3.2 required com Interfaces
The band object is an IE or shell in-process server, so it is packaged in the DLL. As a COM object, it must implement the iunknown and iclassfactory interfaces. (You may have to worry about it because we use ATL to write programs. We do not have to write code for these two interfaces .) In addition, the band object must implement three interfaces: ideskband, iobjectwithsite, and ipersiststream:
Ipersiststream is a type of persistent interface. When ie loads a band object, it passes the property value to the object through the load method of this interface for initialization, IE calls the Save method of this interface to save the attributes of the object. Using ATL to implement this interface is simple:

Class atl_no_vtable CXXX :...... public ipersiststreaminitimpl, // Add inheritance ...... {public: bool m_brequiressave; // The variable required by ipersiststreaminitimpl ...... begin_com_map (cverticalbar )...... com_interface_entry2 (ipersist, ipersiststreaminit) com_interface_entry2 (ipersiststream, ipersiststreaminit) com_interface_entry (ipersiststreaminit )...... end_com_map () begin_prop_map (CXXX )...... // Add the end_prop_map () attribute to be persistent ()

The above Code actually implements the ipersiststreaminit interface, but it does not matter, because ipersiststreaminit is derived from ipersiststream and instantiated the derived class. Naturally, the base class is instantiated. In the example program, I only added the continuity attribute to the desktop toolbar object to save and initialize the "command line ". In addition, com_interface_entry2 (a, B) indicates that if you want to query the interface pointer, the B interface pointer is provided instead. Why? Because interface B is derived from interface A, the first few functions of interface B must be functions of interface a. The address of interface B is actually the same as that of interface.
Iobjectwithsite is an interface used by IE to manage and communicate with plug-ins. You must implement two functions of this interface: setsite () and getsite (). When ie loads the band object and releases the band object, it must call the setsite () function. In this function, it is exactly where the initialization and release operation code is written:

Stdmethodimp CXXX: setsite (iunknown * punksite) {If (null = punksite) // when the band is released {// when loading, save some interfaces // now: Release it} else // when loading band {m_hwndparent = NULL; // load the band parent window (that is, the frame window with the title) // The handle to this window is obtained by calling iunknown: QueryInterface () to obtain iolewindow // and then calling iolewindow: getwindow. Ccomqiptr <iolewindow, & iid_iolewindow> spolewindow (punksite); If (spolewindow) spolewindow-> getwindow (& m_hwndparent); If (! M_hwndparent) return e_fail; // now, this is the time to create a subwindow. // Note: Do not use the ws_visible attribute when creating a sub-window ...... // In the example program, caxwindow is used to implement an ActiveX-tolerant container window (vertical browser bar) // In the example program, use the createwindow function of win api to implement the standard window (horizontal browser bar and toolbar) // In the example program, use csf-wimpl to implement an inclusive window (desktop toolbar) /*************************************** * *****************/The following section, according to the band object's unique functions, yes, you can choose to implement *********************************** * ********************* // If the subwindow Implements user input, the iinputobject interface must be implemented, // and this interface is the iinputobjectsi of IE Te calls, so in your object //, you should save the iinputobjectsite interface pointer. // In the Class header file, define: // ccomqiptr <iinputobjectsite, & iid_iinputobjectsite> m_spsite; m_spsite = punksite; // Save the iinputobjectsite pointer if (! M_spsite) return e_fail; // do you need to control the main framework of IE? // In the header file of the class, define: // ccomqiptr <iwebbrowser2, & iid_iwebbrowser2> m_spframewb; // then, obtain iserviceprovider first, and then obtain response <iserviceprovider, & iid_iserviceprovider> spsp (punksite); If (! Spsp) return e_fail; spsp-> queryservice (sid_swebbrowserapp, & m_spframewb); If (! M_spframewb) return e_fail; // if you have obtained the iwebbrowser2 pointer of the IE main framework, then when something happens to it, don't you want to know? // Definition: ccomptr m_spcp; ccomqiptr <iconnectionpointcontainer, & region> SPCPC (m_spframewb); If (SPCPC) {SPCPC-> findconnectionpoint (Region, & m_spcp); If (m_spcp) {m_spcp-> advise (reinterpret_cast <idispatch *> (this), & m_dwcookie) ;}// cough ~~~ Don't talk about it. Go to the source code. There are too many things to do here...} return s_ OK ;}

Ideskband is a special band object interface with a method function: getbarinfo ();
Idockingwindow is the base class of ideskbank. It has three methods: showdw (), closedw (), and resizeborderdw ();
Iolewindow is the base class of idockingwindow. There are two methods: getwindow () and contextsensitivehelp ();

First declare ideskband, and then implement six functions of the ideskband interface. These functions are relatively simple. The implementation methods of different types of band objects are basically the same:

Class atl_no_vtable CXXX :...... public ideskband ,...... {...... begin_com_map (CXXX )...... com_interface_entry_iid (iid_ideskband, ideskband )...... end_com_map () // iolewindowstdmethodimp CXXX: getwindow (hwnd * phwnd) {// get the window handle of the band object // m_hwnd is saved when the window is created * phwnd = m_hwnd; return s_ OK;} stdmethodimp CXXX: contextsensitivehelp (bool fentermode) {// context help, refer to the icontextmenu interface return e_notimpl;} // idockingwindowstd Methodimp cverticalbar: showdw (bool bshow) {// display or hide the band window if (m_hwnd): showwindow (m_hwnd, bshow? Sw_show: sw_hide); Return s_ OK;} stdmethodimp cverticalbar: closedw (DWORD dwreserved) {// destroy the band window if (: iswindow (m_hwnd): destroywindow (m_hwnd ); m_hwnd = NULL; return s_ OK;} stdmethodimp progress: resizeborderdw (lpcrect prcborder, iunknown * punktoolbarsite, bool freserved) {// return e_notimpl when the frame window's border size changes ;} // ideskbandstdmethodimp cverticalbar: getbandinfo (DWORD dwbandid, DWORD dwviewmode, Deskbandinfo * pdbi) {// obtain the basic information of the band. You need to enter the pdbi parameter as the return if (null = pdbi) return e_invalidarg; // if you need to call iolecommandtarget in the future :: exec (), you need to save the two parameters m_dwbandid = dwbandid; m_dwviewmode = dwviewmode; If (pdbi-> dwmask & dbim_minsize) {// minimum size pdbi-> ptminsize. X = 10; pdbi-> ptminsize. y = 10;} If (pdbi-> dwmask & dbim_maxsize) {// maximum size (-1 indicates 4G) pdbi-> ptmaxsize. X =-1; pdbi-> ptmaxsize. y =-1;} If (pdbi-> dwmask & dbim_inte Gral) {pdbi-> ptintegral. X = 1; pdbi-> ptintegral. y = 1;} If (pdbi-> dwmask & dbim_actual) {pdbi-> ptactual. X = 0; pdbi-> ptactual. y = 0;} If (pdbi-> dwmask & dbim_title) {// window title wcscpy (pdbi-> wsztitle, l "window title ");} if (pdbi-> dwmask & dbim_modeflags) {pdbi-> dwmodeflags = dbimf_variableheight;} If (pdbi-> dwmask & dbim_bkcolor) {// If the Default background color is used, remove the pdbi-> dwmask & = ~ Dbim_bkcolor;} return s_ OK ;}

3.3 select the implemented COM interface
Two interfaces are not required, but may be useful: iinputobject and icontextmenu. If the band object needs to receive user input, the iinputobject interface must be implemented. IE implements the iinputobjectsite interface. When the container has multiple input windows, it calls the iinputobject interface method to manage users' input focus.
In the browser bar, you need to implement three functions: uiactivateio (), hasfocusio (), and translateacceleratorio ().
When the browser bar is activated or inactive, ie calls the uiactivateio function. When activated, the browser bar generally calls setfocus to set the focus of its own window. When ie needs to determine which window has focus, it calls hasfocusio. When the window in the browser bar or its subwindows have input focus, s_ OK is returned; otherwise, s_false is returned. Translateacceleratorio allows the object to process the acceleration key, which is not implemented in the example program. Therefore, s_false is returned directly.

STDMETHODIMP CExplorerBar::UIActivateIO(BOOL fActivate, LPMSG pMsg){    if(fActivate)        SetFocus(m_hWnd);    return S_OK;}STDMETHODIMP CExplorerBar::HasFocusIO(void){    if(m_bFocus)        return S_OK;    return S_FALSE;}STDMETHODIMP CExplorerBar::TranslateAcceleratorIO(LPMSG pMsg){    return S_FALSE;}      

The band object can use the iolecommandtarget: exec () of the package container to call and execute the command. The iolecommandtarget interface pointer can be obtained by calling the iinputojbectsite: QueryInterface (iid_iolecommandtarget,...) function of the package container. Cgid_deskband is a command group. When the getbandinfo of a band object is called, the package container uses the dwbandid parameter to specify an ID for the band object. The object must save this ID to call iolecommandtarget :: exec. The command ID includes:

  • Dbid_bandinfochanged
    Band information changes. Set the pvain parameter to the band ID, which is the value obtained by the last call of getbandinfo. The container will call the getbandinfo function of the band object to update the request information.
  • Dbid_maximizeband
    Maximize band. Set the pvain parameter to the band ID, which is the last call? Getbandinfo? The obtained value.
  • Dbid_showonly
    Enable or disable other bands in the container. Set the pvain parameter to the vt_unknown type, which can be the following values:
     
    Value Description
    Punk Band objectIunknownPointer, other desktop bands will be hidden
    0 Hide all desktop bands
    1 Show all desktop bands

  • Dbid_pushchevron
    The "v" selection flag is displayed on the left of the menu item. The container sends an rb_pushchevron message. When the band object receives the notification message rbn_chevronpushed, it displays a "v" flag. Set the ncmdexecopt parameter in iolecommandtarget: exec function to band ID, which is the last time getbandinfo is called? Set the pvain parameter in iolecommandtarget: exec to vt_i4. This is a value defined by the application. It is sent back to the band object by notifying lappvalue in the message rbn_chevronpushed.

3.4 band Object Registration
The band object must be registered as a server in the OLE process and supports apartment thread apartment. The default key value in the Registry indicates the text of the menu. For the browser bar, it is added to the "View/browser bar" in the IE menu; For the toolbar band, it is added to the "View/toolbar" in the IE menu; for the desktop band, it is added to the shortcut menu of the system taskbar. In the menu resource, you can use "&" to specify the acceleration key.

Generally, the registry project of a basic band object is:

Hkey_classes_root
CLSID
{CLSID of your band object}

(Default) = menu text
Inprocserver32
(Default) = DLL full path file name
Threadingmodel = apartment

The toolbar bands must also register their CLSID to the IE registry.

InHKEY_LOCAL_MACHINE/software/Microsoft/Internet Explorer/ToolbarCLSID is given as the key name, and its key value is ignored.

HKEY_LOCAL_MACHINE
Software
Microsoft
Internet Explorer
Toolbar

{CLSID of your band object}

There are several optional registry projects (the example program is not implemented in this way ). For example, to display HTML in the browser bar, you must set the Registry as follows:

Hkey_classes_root
CLSID
{CLSID of your band object}
Instance
CLSID
  
(Default) = {4d5c8c2a-d075-11d0-b416-00c04fb90376}

At the same time, if you want to specify a local HTML file, you need to set it as follows:

Hkey_classes_root
CLSID
{CLSID of your band object}
Instance
Initpropertybag
  
URL

In addition, you can specify the width and height of the browser bar. Of course, it depends on whether the column is vertical or horizontal. In fact, this project does not matter, because when you adjust the size of the browser bar, it will be automatically saved in the registry.

HKEY_CURRENT_USER
Software
Microsoft
Internet Explorer
Explorer bars
{CLSID of your band object}
  
Barsize

The barsize key must be of the REG_BINARY type and has eight bytes. The first 4 bytes from the left are the pixel width or height in hexadecimal notation. The last 4 bytes are retained and you should set it to 0. The following is an example of displaying all the registry items of an HTML file in the browser bar. The default width is 291 (0x123) pixels:

Hkey_classes_root
CLSID
{CLSID of your band object}

(Default) = menu text
 Inprocserver32
(Default) = DLL full path file name
Threadingmodel = apartment
Instance
CLSID

(Default) = {4d5c8c2a-d075-11d0-b416-00c04fb90376}
Initpropertybag
Url = your HTML file name

HKEY_CURRENT_USER
Software
Microsoft
Internet Explorer
Explorer bars
{CLSID of your band object}

Barsize = 23 01 00 00 00 00 00

For registry settings, using ATL is actually very simple. Open the XXX. RGS file of the project and edit it manually. The following file source code is the Registry style of the IE Toolbar In the example program. HKLM needs to be manually added because it does not use the component type registration method. For other types of band objects, you only need to add them to the class declaration:

Begin_category_map (CXXX) // register com-type implemented_category (catid_infoband) with the Registry // end_category_map () in the vertical style browser bar ()

The ". RGS" file of the band object of the IE Toolbar type

Hkcr // This project is generated by ATL for you. You only need to manually modify the "text on the menu" to {bands. toolbar.1 = s ''toolbar class'' {CLSID = S' {Your CLSID} ''} bands. toolbar = S' toolbar class'' {CLSID = S' {Your CLSID} ''curver = S' bands. toolbar.1 ''} noremove clsid {forceremove {Your CLSID} = S' text (& T)'' {progid = S' bands. toolbar.1 ''versionindependentprogid = S' bands. toolbar ''forceremove ''' programmable ''' inprocserver32 = S' '% module % ''{Val threadingmodel = S' ''apartment''} ''typelib ''= S ''{ XXXX-xxxxxxxxxxxxxxx} ''}}hklm // This item is unique to the manually added IE Toolbar {software {Microsoft {''internet explorer'' {noremove toolbar {forceremove val {Your CLSID} = s'. Just give an explanatory text string ''}}}}}

Iv. ATL implementation
After downloading the code (VC 6.0 Project), read the code carefully by referring to the previous instructions. The Code also has some key notes. If you want to run it, you can use regsvr32.exe for registration, and then open the IE browser or the resource browser to see the effect. If you want to practice it yourself, you can follow the steps below to construct the project:

4.1 create an atl dll Project
4.2 Add new ATL object... and select Internet Explorer Object. The purpose of selecting this type is to allow the Wizard to add iobjectwithsite support. If you are using a. NET environment, do not forget to support this interface.

4.3 enter the object name. For example, if you want to create a vertical browser bar, call it verbar.

4.4 The thread model must be set to apartment. It doesn't matter if you choose the interface type. Check whether you want to support the idispatch interface function. In the vertical browser bar of the example program, it is necessary to select dual because you want to more easily manipulate IE and accept events (connection points) from IE. Aggregation option. You only need to select only.

4.5 show your infinite wisdom. start inputting the program. If it is compiled in debug mode, a connection error may occur. If the report cannot find _ atlaxcreatecontrol, you need to go to the project/settings... /link. lib connection. Or use # pragma comment (Lib, "ATL") to join the Connection Library.
4.6 If You Want To debug the code, go to project/settings... enter the path name of IE in/debug, for example, "C:/program files/Internet Explorer/iw.e. then you can track the breakpoint debugging. Compiling and debugging the band object in the desktop toolbar is very troublesome, because the computer automatically runs the shell when it starts, and the shell loads the Active Desktop object.

V. Conclusion
Okay, here we are. Wish everyone a happy learning experience ^_^

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.