Sunday, August 24, 2008

Calendar ActiveX Control Using ATL

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

Step 1: Creating the Project

Step 2: Adding a Control to Project

Step 3. Adding a Methods to Control

Step 4: Changing Control's Drawing Code

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 'CalendarServer' 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 "Calendar" 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 the Stock Properties tab. You see a list box with all the possible stock properties you can enter. Scroll down the list, then double-click Fill Color to move it to the Supported list.You are finished selecting options for your control. Click OK. When you created your control, several code changes and additions were made.

Adding Methods to the control

Add to Methods GetDate( ), and SetDate( ) to ICalendar interface and add the appropriate code to these methods.For knowing how to add methods in ATL COM refer to article 'Building Client and Servers' in ATLCOM section of this website.

Changing Control's Drawing Code

By Default OnDraw( ) handler is present in the CCalendar class. In this handler code is added to draw calendar.

Adding Events to the control

Add an event NewDay( ) to the control .For knowing how to adding Events in ATL COM refer to article ' Connection Points' in ATLCOM section of this website and event is fired from OnLButtonDown( ) handler.

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 '&Date' 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 'Calendar' Control. Creation of these controls has been discussed in the previous articles of the section 'ATL COM' in this same site. We have created the a dialog-based project called "CalendarClient'' 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 "CalendarServer' 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 'Calendar' control Double click on both the controls to insert them in the project. The bitmaps 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. Two combo boxes are added to the dialog for months and years.a call to Initmonths( ) and Inityears( ) functions in OnInitDialog( ) handler fills the two comboboxe with months and years respectively.a handler OnCombosChanged( ) 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 GetDate( ) and SetDate( ) methods will update the calendar control present on the the dialog with appropriate date. Using ClassWizard a handler corresponding to the event NewDay( ) is added and a inside the handler appropriate code is added such that when ever we clicked on the calendar control the date and day of the grid clicked get displayed in a message box .