Compile C ++ code with better reusability -- band object and comtoys (1)

Source: Internet
Author: User
Tags knowledge base

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)

Section 1 Introduction to band objects

Note:
It is difficult to match the word "band" with a Chinese Word. Therefore, this article does not intend to translate the word "band" into Chinese. You only need to understand that band is an application in the Windows Shell extension (Shell. If you need to translate it, I prefer to translate it into "area with objects ". Let's take a look at the catid in the registry. If you have installed a bandapp similar to desk band or explorer barrier on your machine, run the program catview.exe (this is an example program, you can download the title of Microsoft from the article "Understanding the" category "concept in COM programming in vckbase online magazine 11th, in the "category name" column, desk bands is called the "desktop area" and explorer bars is called the "browser area ". The following table item value at the entry to the Registry is also used to understand the band name:

Hkcr/component categories/{00021492-0000-0000-c000-000000000046} -- desktop Zone
Hkcr/component categories/{00021493-0000-0000-c000-000000000046} -- Internet Explorer
Hkcr/component categories/{00021494-0000-0000-c000-000000000046} -- Internet Explorer communication tape

No matter what band is called, the key is to understand the role of band in shell applications.

Overview:
There are three types of band objects: Desk, info, comm, and tools bands. Here, desk bands is used for the taskbar; Info and comm -- also known as explorer bar -- are used for IE and resource manager. Tools bands is used for the resource manager or IE Toolbar. The sample code mybands. dll in this article implements the above two types of band object applications, and introduces how to use bands objects in your own programs. Tools bands are new features that are available only after ie5. For details about its implementation, I will introduce it in another article. For the basic concepts of bands objects, refer to the other msdn translation "Creating and customizing windows bands ".
Not long ago, a friend asked me how to add edit box control in the taskbar. The idea is to get the hwnd of the taskbar, and then add the edit box as a subwindow to the taskbar. I broke his idea at the time: the reason is that he could not program the current system using the previous method called zookeeper. Today's operating systems are highly integrated with COM. The correct method should be to use the features or interfaces provided by com to communicate with the operating system in a friendly manner.

"Could you give me the menu ?"
"Oh, of course ,"
"Please take the menu ."
"Can I execute this command now ?"
"No problem. Let's use icontextmenu ."
......

To do this, you only need one of the various endless small interfaces in the shell extension.
But among these interfaces, I did not find a ready-made interface that can add windows to the taskbar !. At that time, I told the poor friend that his idea was hard to implement. Later I found the band object and ideskband (these two items were provided in ie4.0 and ie5.0 shortly after they came out ). I immediately thought it was the interface I dreamed. I can see that there is only one method in the ideskband interface. So I decided not to write a desktop band myself to implement my friend's ideas? I thought it was not the only function. What's the problem?
A few months later, thousands of lines of code were generated (excluding Code notes and additional content of some additional releases). What is the result? I don't think how difficult it is to implement the band object (or actually it is not), but that my project code keeps expanding. Writing a band object is not fun; then we create another framework. With the framework, I still feel that it is not enough; I created another general-purpose COM programming platform. The main reason for this is to try to make your code more reusable. As we all know, reusability is the ultimate in programming. programmers should keep pursuing the reusability of their own code as they do for pretty girls. At the final completion, I established my own bandobj framework and some reusable com code, which is the comtoys to be introduced in the subsequent sections. In this article, we will first discuss the basic content of bandobj.

Implementation logic of mybands DLL
The best way to understand a system is to understand the problems it wants to solve (some systems do not care about solving any problems, such systems are usually not good systems ). This is the origin of mybands DLL.
I hate the demo program most. It displays a bunch of clumsy text on an ugly background, such as "Hello + [Application name]" And mentions it all day long. I think that if I have trouble programming, I may also have some useful things-or at least the problem details. So when I started writing desk bands, the first thing I had to do was to find a reason to write it. To be honest, it was challenging to put a small window on the taskbar, it is also very exciting. Figure 1 shows a web search box in the taskbar.

Figure 1 web search box in the taskbar

Enter the content to be searched in the edit box, and press Enter. The system starts the default browser and searches for the content using the Web search engine specified in the context menu of the edit box. You can also drag the Web search box to the desktop. Then it becomes a common window, as shown in Figure 2:

Figure 2 the Web search box in the taskbar is changed to a normal dialog box

The program implementing the Web search box is mybands. dll, which is a DLL. Smart people will certainly ask, how is the DLL name in the plural form? Because it implements all three types of band applications. The three band objects mentioned above are desk bands, info/Comm, and tools bands. Note: After ie5, Microsoft will collectively name the bands of info/Comm type as explorer bars -- browsing bar -- for IE or resource manager -- It includes vertical and horizontal, IE and Resource Manager "View" History "," favorites "," Search "in the" browsing bar "in the menu (3) they all belong to bands applications of the info or explorer Bar type. Tool bands is a new band type added by ie5-tool bar bands.

Figure 3 other bands

Install mybands. DLL using the following method: Download the source code, copy the DLL to the Windows System directory (system or system32) after compilation, and then register it to the system:

Regsvr32.exe mybands. dll

After the registration is successful, the menu items of bands are displayed in the "View" => "toolbar" and "Browse bar" menus of IE or resource manager, as shown in figure 3. TIPS: You must restart the Resource Manager process (for Windows 9x. Windows 2000, Windows XP is not required ). Press Ctrl + ATL + DEL to delete the Resource Manager process, after restarting, you can view the "desk band" menu item 4 in the "toolbar" of the context menu of the taskbar:


Figure 4

It is easy to use bandobj to compile the band object. Taking mybands as an example, it consists of several modules, but only one module is the focus of compiling the band object, that is, the main module: mybands. CPP. Mybands is derived from cbandobjdll (bandobj. h) and cbandobjdll is derived from the colecontrolmodule of MFC. The derivative relationship between them is:

    CWinApp
COleControlModule
CBandObjDll
CBandObjDll

Like a common MFC application, cbandobjdll has an initinstance function:

BOOL CMyBandsDll::InitInstance()
{
AddBandClass(CLSID_MYINFOBAND,
RUNTIME_CLASS(CMyInfoBand),
CATID_InfoBand,
IDR_INFOBAND);
AddBandClass(CLSID_MYCOMMBAND, ...);
AddBandClass(CLSID_MYDESKBAND, ...);
return CBandObjDll::InitInstance();
}

It is similar to DOC/view, but Doc/view calls adddoctemplate instead of addbandclass. For each band class, you must provide the class ID (guid), MFC runtime class, category, and resource IDs. A type ID is a guid that tells windows which kind of band is your class-Info, comm (Explorer bar) or desk band. From the Code, it is not difficult to see that mybands uses a separate class to process each band. Cmyinfoband, cmycommband, and cmydeskband are derived from cbandobj and both use declare/implement_dyncreate, so that MFC can use its common runtime mechanism and coleobjectfactory to dynamically create instances. They all have constructor to initialize band information and cbandobj: m_dbidefault in the extenbandinfo structure. For example, desk band has a default width (100) and a higher value.

CMyDeskBand::CMyDeskBand() : CBandObj(CLSID_MYDESKBAND)
{
m_dbiDefault.ptActual = CPointL(100,0);
m_dbiDefault.dwModeFlags = DBIMF_VARIABLEHEIGHT;
}

The explorer bar has a fixed height of 30 pixels and has no title.

CMyCommBand::CMyCommBand() : CBandObj(CLSID_MYCOMMBAND)
{
m_strTitle.Empty();
m_dbiDefault.ptMinSize = CPointL(0,30);
m_dbiDefault.ptMaxSize = CPointL(-1,30);
}

Whether you believe it or not, mybands is really related to the band object. The rest is nothing more than band behavior and some common MFC application processing. For example, the oncreate handler in cmyuncband is used to create the edit box control. The wm_paint handler in cmyuncband is used to draw related information:

Void cmycommband: onpaint ()
{
Cpaintdc DC (this );
DC. drawtext ("Welcome to VC knowledge base! ");
}

A band object does not have a top menu like a frame, but you can add a context menu for it if you want. The complicated details are all handled by cbandobj. In this case, mybands must use the same resource ID as the band class to create a menu. The command will magically reach the on_command handler of mybands through all normal channels. If you do not have guids and category IDs, it is hard to understand that mybands is a COM object. Cbandobj hides all the details and leaves a free space for you to write your own band. This is my ultimate goal. (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.