Write c ++ code with better reusability -- band object and comtoys (3)

Source: Internet
Author: User

Compile/Zhao Xiangning

Original: Paul dilascia

MSJ November 1999 & December 1999

Keywords: bands object, desk bands, info/Comm bands, explorer bar, and tool bands.

This document assumes that you are familiar with C ++, COM, and IE.

Download the source code of this article: mybands.zip (KB)
Testeditsrch.zip (75kb)

Part 1: Introduction to band objects
Part 2: bandobj class hierarchy and mybands service program Registration

The third part goes deep into the band to unveil the band.

The previous two sections discussed the creation and registration of the mybands object. Now mybands has been properly registered and has the class factory to create them. It is time to expose the real purpose of the band object.
The band object is a window located in the taskbar or IE. However, I also want you to realize that the band object is also a COM object that must implement the following three interfaces: ideskband, iobjectwithsite, and ipersiststream. If you want to accept user input, you can choose to implement the iinputobject interface. If you want the context menu, you can choose to implement the icontextmenu interface. Figure 10 shows all the interfaces of the band object, including the required and selectable interfaces.

Figure 10

Among these interfaces of the band object, the most important is the ideskband interface, which is derived from idockingwindow, and idockingwindow is derived from iolewindow. These two interfaces are required. What are their functions?
The best way to understand a band object or any COM object like this is to check a series of events that occur when the object exists-for example, to monitor the activities that the user selects from the toolbar until the band is closed. The best way to do this is to use the sample code that can be run and add trace diagnostics to the code. Bandobj has built-in diagnostic functions, which are implemented by a self-compiled diagnostic tool called tracefn. Tracefn uses a special class and a custom version of afxtrace to generate the expected diagnostic output. You can use it to view the stack content. Figure 11 shows the diagnostic output during mybands runtime, select the Web search box from the menu bar to start step by step.
Windows (desktop band is used for resource manager, and browsing bar is used for IE). You can find the band object by searching the COM Object of catid_deskband, catid_infoband, or catid_commband, add the band name to the toolbar menu (4 ).
When you select band from the menu, Windows calls cocreateinstance or its equivalent function. Com calls the dllgetclassobject output function in the DLL, while dllgetclassobject calls afxdllgetclassobject. MFC searches for a class factory with the correct ID and returns it. Then com calls iclassfactory: createinstance to perform a series of general com processing.

In Windows, query the ideskband and iobjectwithsite interfaces. Cbandobj implements these interfaces using common MFC methods-using nested classes, interface ing, and begin/end_interface_part, and then MFC returns the correct pointer.
Windows calls iobjectwithsite: setsite to give an (iunknown *) type pointer pointing to the object container. Cbandobj: xobjectwithsite: getsite call the cbandobj: onsetsite virtual function to convert the nested class method to the virtual function call of the parent class, so that you can easily reload it. By default, the field m_spsite is stored in. You can query (QueryInterface) interfaces implemented by any container in m_spsite. Cbandobj uses it to obtain the hwnd of its parent window:

CComQIPtr
  spOleWin = m_spSite;if (!spOleWin)   return E_FAIL;HWND hwndParent = NULL;spOleWin->GetWindow(&hwndParent);if (!hwndParent)   return E_FAIL;      

When Windows calls setsite, it wants you to create your own window. It is difficult for me to see this from the relevant instructions. Cbandobj: onsetsite calls the virtual function oncreatewindow to do this. By default, you can register and create a common invisible window.

Bool cbandobj: oncreatewindow (cwnd * pparent, const crect & rc) {static bool bregistered = false; static ccriticalsection Cs; // protection ctlockdata lock (CS ); // register the window class if (! Bregistered) {afxregisterclass (...); bregistered = true;} return cwnd: Create (bandobjclass ,...);}

The thread model of the band object is "apartment", so it is very important to protect the full-process variables. It is best to keep full variables closely related to the functions using them. In this example, oncreatewindow is the only function that uses bregistered, so it is a static function. You can overload oncreatewindow to create your own window class, and/or overload precreatewindow to change some properties of the window. Keep in mind that the window you created must be invisible and do not use ws_visible to create a window.
Next, Windows calls iolewindow: getwindow to obtain the hwnd of the window. (This is why you must create a window in setsite ). M_hwnd is returned by cbandobj.
Next, Windows calls ideskband: getbandinfo to request information about the band, such as the size, variable height or fixed height, and the background color and title. Cbandobj uses the default value to fill in the wide bandinfo structure-these values can be modified in the band object constructor-some information comes from your resource file. For example, obtain the title from the resource string. Next, Windows calls idockingwindow: showdw to display the created window. Cbandobj calls cwnd: showwindow to complete display.
If you have input or right-click to access the context menu, Windows needs to query the corresponding iinputobject and icontextmenu interfaces respectively. If you implement these interfaces, Windows will use them. By default, cbandobj obtains the keyboard acceleration key and menu information from the resource file. You only need to add resources to the project. The menu itself is stored in m_contextmenu, a data member, and can be accessed at any time.
Cmyuncband does not have a resource menu. Instead, it dynamically creates a menu by reading the user attribute setting file (12 ).

Figure 12

Before adding a menu to the container (when the container calls icontextmenu: querycontextmenu), cbandobj routes the menu through the on_update_command_ui command processor of MFC. 12. cmyuncband implements the check mark next to the search engine in this way.
If you use the menu accelerator key or select a menu item to call a command, Windows will call icontextmenu: invokecommand. Cbandobj adopts the same approach as initializing this menu, so this command usually arrives at the on_command command processor.
When you disable band, Windows calls idockingwindow: closedw. Cbandobj sends the wm_close message, so that the window disappears. However, the cbandobj object remains alive until Windows releases it. As long as you use Trace, you will understand the logic process described above. (To be continued)

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.