3/30/09

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.


No comments: