Example: Calendar Control
This example uses an ActiveX control that displays a calendar interface. The control, contained in the file mscal.ocx, is installed along with a typical installation of Microsoft Office 97, and may also be present on your system if you have upgraded to a more recent version of Microsoft Office. If the control is not present on your system (you'll know the control is not present if the example code does not display a calendar similar to the one shown in Figure 4-1), you can download a the control as part of a package of sample ActiveX controls included in the file actxsamp.exe, discussed in Microsoft Knowledge Base Article 165437.
If you download the control, place the file mscal.exe in a known location and execute the file; you will be prompted for a directory in which to place mscal.ocx. Open a command prompt window in the directory you chose and register the control as described in Registering COM Components on a Windows Machine.
The calendar control has the program ID:
and the class ID:
Example Code
This example, ActiveXCal.pro, is included in the examples\doc\bridges\COM subdirectory of the IDL distribution and develops an IDL routine called ActiveXCal that illustrates use of the calendar ActiveX control within an IDL widget hierarchy. Run the example procedure by entering ActiveXCal at the IDL command prompt or view the file in an IDL Editor window by entering .EDIT ActiveXCal.pro.
- Create the ActiveXCal procedure. (Remember that in the
ActiveXCal.profile, this procedure occurs last.) - Create a top-level base widget to hold the ActiveX control.
- Create base widgets to hold labels for the selected month, day, and year. Set the initial values of the labels.
- Instantiate the ActiveX Control, using the control's class ID.
- Realize the top-level base widget.
- Set the top-level base's user value to an anonymous structure containing widget IDs of the month, day, and year label widgets.
- Retrieve the object ID of the IDLcomActiveX object that encapsulates the ActiveX control. Use the GetProperty method to retrieve the current values of the month, day, and year from the control.
- Set the values of the label widgets to reflect the current date, as reported by the ActiveX control.
- Call XMANAGER to manage the widget events, and end the procedure.
- Now create an event-handling routine for the calendar control. (Remember that in the
ActiveXCal.profile, this procedure occurs before the ActiveXCal procedure.) - The ActiveX widget is the only widget in this application that generates widget events, so the ID field of the event structure is guaranteed to contain the widget ID of that widget. Use the GET_VALUE keyword to retrieve an object reference to the IDLcomActiveX object that encapsulates the control.
- The user value of the top-level base widget is an anonymous structure that holds the widget IDs of the month, day, and year label widgets (see step 6 above). Retrieve the structure into a variable named
state. - Use the GetProperty method on the IDLcomActiveX object to retrieve the current values of the month, day, and year from the calendar control.
- Use WIDGET_CONTROL to set the values of the month, day, and year label widgets.
- Call HEAP_FREE to ensure that dynamic portions of the event structure are released, and end the procedure.
wSubBase = WIDGET_BASE(wBase, /ROW) wVoid = WIDGET_LABEL(wSubBase, value = 'Month: ') wMonth = WIDGET_LABEL(wSubBase, value = 'October') wSubBase = WIDGET_BASE(wBase, /ROW) wVoid = WIDGET_LABEL(wSubBase, VALUE = 'Day: ') wDay = WIDGET_LABEL(wSubBase, VALUE = '22') wSubBase = WIDGET_BASE(wBase, /ROW) wVoid = WIDGET_LABEL(wSubBase, VALUE = 'Year: ') wYear = WIDGET_LABEL(wSubBase, VALUE = '1999')
WIDGET_CONTROL, wMonth, SET_VALUE=STRTRIM(month, 2) WIDGET_CONTROL, wDay, SET_VALUE=STRTRIM(day, 2) WIDGET_CONTROL, wYear, SET_VALUE=STRTRIM(year, 2)
WIDGET_CONTROL, state.month, SET_VALUE = STRTRIM(month,2) WIDGET_CONTROL, state.day, SET_VALUE = STRTRIM(day,2) WIDGET_CONTROL, state.year, SET_VALUE = STRTRIM(year,2)
Running the ActiveXCal procedure displays a widget that looks like the following:
