Skip to main content

ATL COM Controls

This Article is used to bulid a full fledged ATL COM explorer Control. This article is divided into six steps.

Step 1: Creating the Project
Step 2: Adding a Control to Project
Step 3. Changing Control's Drawing Code
Step 4: Adding a Methods to Control
Step 5: Adding an Event to the Control
Step 6: Adding a Property Page to Your Control

Creating the project :

First we have to create the initial ATL project using the ATL COM AppWizard. click New on the File menu, then choose the Projects tab. Select the ATL COM AppWizard. Type 'MultiControls' as the project name. In step2 check 'Support MFC' check box and click on Finish.

Adding a Control :

To add an object to an ATL project, you use the ATL Object Wizard. Click New ATL Object on the Insert menu, and the ATL Object Wizard appears. In the first ATL Object Wizard dialog box, select the category of object you want to add to your current ATL project. select the category as Controls on the left, then on the right select Full Control. Finally, click Next. A set of property pages is displayed that allows you to configure the control you are inserting into your project. Type "Explorer" as the short name. The other fields are automatically completed. Now check support for rich error information and connection points for your control. On the Attributes tab,Click the Support Connection Points check box. This will create support for an outgoing interface in the IDL file. Click on Miscellaneous tab, from Add Controls combo box select 'SysTreeView32' and in the others division check 'insertable' check button.You are finished selecting options for your control. Click OK. When you created your control, several code changes and additions were made. Changing Control's Code : Since we wish to display 'treectrl' (to display directory tree structure) on the left side of the control and 'listctrl' (to display the files present in the selected directory from the treectrl)on the right side. For initializing listctrl add the following code at the constructor m_ctllist(_T("SysListView32"),this,2) where m_ctlist is the member variable that we have added. we have added code in OnCreate( ) to cretae the listctrl manually.

m_ctllist.Create(m_hWnd, rc, NULL, WS_BORDER|WS_CHILD|WS_VISIBLE|LVS_SMALLICON|LVS_SHOWSELALWAYS ) ;
We have added OnSize( ) handler to resize both the controls. Important thing is we must add the following line
ALT_MSG_MAP(2)
between
BEGIN_MSG_MAP ( CExplorer )
END_MSG_MAP( )

Adding properties to the control : A property 'format' is added to the controls interface,to change the formats of the files to be displayed at runtime.

Adding Events to the control :

Two event GetPathFromTree( ) and GetPathFromList( ) are added to the control . For knowing how to adding Events in ATL COM refer to article ' Connection Points' in ATLCOM section of this website. GetPathFromTree( ) event is fired from onselchanged( ) handler to get the full directory path when we select a directory from the treectrl. GetPathFromList( ) is fired from onitemchanged( ) handler to get the full path of the filename that was selected from the listctrl.

Adding a property pages to the control :

Property pages are implemented as separate COM objects, which allow property pages to be shared if required. To add a property page to the control we can use the ATL Object Wizard.Start the ATL Object Wizard and select Controls as the category on the left. Select Property Page on the right, then click Next.we again get the dialog box allowing to enter the name of the new object. Call the object 'PropPage' and enter that name in the Short Name edit box. Notice that the Interface edit box is grayed out. This is because a property page doesn't need a custom interface. Click on the Strings tab to set the title of the property page. The title of the property page is the string that appears in the tab for that page. Type '&Format' as the title. Click OK and the property page object will be created. A dialog box template is added to the project resource for the property page. Now add the fields that we want to appear on the property page and write the appropriate code that we required in Apply( ) function . At Last Build the control,and test it usinig Activex Text Container.




Client

This program uses 'Explorer Control. Creation of these controls has been discussed in the previous article of the section 'ATL COM' in this same site. We have created the a dialog-based project called "ExplorerDemo'' using MFC AppWizard(exe). To use the ActiveX controls they have to be registered with the Windows registry. If you want to register these controls on your machine you will have to download "Explorer' control . Once registered these controls can be inserted in the dialog box. using the following procedure:

From the 'Project' menu select 'Add To Project' then select 'Components and Controls'. A dialog box will be popped up. Open 'Registered ActiveX Controls' folder. Search for the control to be inserted.(in our case 'Explorer' control). Double click on both the controls to insert them in the project. The bitmap of these controls would now be visible in the 'Controls' toolbar of the dialog editor. Pick it and paste on the dialog box. You can see the preview of both in the dialog box. The dialog that we created is shown in Figure . Run the application to see the effect. Combo box is added to the dialog for changing the files format at runtime. A handler OnSelchangeCombo1( ) is added to the dialog class which get called whenever we have changed the contents in the combo box. Inside this handler a call to SetFormat( ) will update the control present on the the dialog with appropriate File Format. Using ClassWizard a handler corresponding to the event GetPathFromList( ) is added and a inside the handler appropriate code is added such that when ever we clicked on the files present in the list control the file name with full path is displayed in an edit box.


Comments

Popular posts from this blog

Explain Polymorphism and Flavors of Polymorphism...

Polymorphism is the ability of different objects to react in an individual manner to the same message. This notion was imported from natural languages. For example, the verb "to close" means different things when applied to different objects. Closing a door, closing a bank account, or closing a program's window are all different actions; their exact meaning is determined by the object on which the action is performed. Most object-oriented languages implement polymorphism only in the form of virtual functions. But C++ has two more mechanisms of static (meaning: compile-time) polymorphism: Operator overloading. Applying the += operator to integers or string objects, for example, is interpreted by each of these objects in an individual manner. Obviously, the underlying implementation of += differs in every type. Yet, intuitively, we can predict what results are. Templates. A vector of integers, for example, reacts differently from a vector of string objects when it receives ...

MFC - Microsoft Foundation Classes Design Patterns

1 Introduction This paper describes the use of object-oriented software design patterns, as presented in Design Patterns: Elements of Reusable Object-Oriented Software by Gamma et al., within the Microsoft Foundation Class Library (MFC). MFC is used for implementing applications for Microsoft Windows operating systems. Because of the size of the MFC library, a complete analysis would have been beyond the scope of this assignment. Instead, we identified various possible locations for design patterns, using the class hierachy diagram of MFC, and studied the source carefully at these locations. When we did not find a pattern where we expected one, we have documented it anyway, with examples of how the particular problem could have been solved differently, perhaps more elegantly, using design patterns. We have included a brief introduction to MFC in Section 2 , as background information. The analysis has been split into three parts, with one section for each major design pattern ca...

• Why might you need exception handling be used in the constructor when memory allocation is involved?

Your first reaction should be: "Never use memory allocation in the constructor." Create a separate initialization function to do the job. You cannot return from the constructor and this is the reason you may have to use exception handling mechanism to process the memory allocation errors. You should clean up whatever objects and memory allocations you have made prior to throwing the exception, but throwing an exception from constructor may be tricky, because memory has already been allocated and there is no simple way to clean up the memory within the constructor.