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:

MSCAL.Calendar.7  

and the class ID:

{8E27C92B-1264-101C-8A2F-040224009C02} 

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.

  1. Create the ActiveXCal procedure. (Remember that in the ActiveXCal.pro file, this procedure occurs last.)
  2. PRO ActiveXCal 
    
  3. Create a top-level base widget to hold the ActiveX control.
  4.    wBase = WIDGET_BASE(COLUMN = 1, SCR_XSIZE = 400, $ 
          TITLE='IDL ActiveX Widget Calendar Control') 
    
  5. Create base widgets to hold labels for the selected month, day, and year. Set the initial values of the labels.
  6.    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') 
    
  7. Instantiate the ActiveX Control, using the control's class ID.
  8.    wAx=WIDGET_ACTIVEX(wBase, $ 
          '{8E27C92B-1264-101C-8A2F-040224009C02}') 
    
  9. Realize the top-level base widget.
  10.    WIDGET_CONTROL, wBase, /REALIZE 
    
  11. Set the top-level base's user value to an anonymous structure containing widget IDs of the month, day, and year label widgets.
  12.    WIDGET_CONTROL, wBase, $ 
          SET_UVALUE = {month:wMonth, day:wDay, year:wYear} 
    
  13. 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.
  14.    WIDGET_CONTROL, wAx, GET_VALUE = oAx 
       oAx->GetProperty, month=month, day=day, year=year 
    
  15. Set the values of the label widgets to reflect the current date, as reported by the ActiveX control.
  16.    WIDGET_CONTROL, wMonth, SET_VALUE=STRTRIM(month, 2) 
       WIDGET_CONTROL, wDay, SET_VALUE=STRTRIM(day, 2) 
       WIDGET_CONTROL, wYear, SET_VALUE=STRTRIM(year, 2) 
    
  17. Call XMANAGER to manage the widget events, and end the procedure.
  18.    XMANAGER, 'ActiveXCal', wBase 
     
    END 
    
  19. Now create an event-handling routine for the calendar control. (Remember that in the ActiveXCal.pro file, this procedure occurs before the ActiveXCal procedure.)
  20. PRO ActiveXCal_event, ev 
    
  21. 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.
  22.    WIDGET_CONTROL, ev.ID, GET_VALUE = oCal 
    
  23. 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.
  24.    WIDGET_CONTROL, ev.TOP, GET_UVALUE = state 
    
  25. Use the GetProperty method on the IDLcomActiveX object to retrieve the current values of the month, day, and year from the calendar control.
  26.    ocal->GetProperty, month=month, day=day, year=year 
    
  27. Use WIDGET_CONTROL to set the values of the month, day, and year label widgets.
  28.    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) 
    
  29. Call HEAP_FREE to ensure that dynamic portions of the event structure are released, and end the procedure.
  30.    HEAP_FREE, ev 
     
    END 
    

Running the ActiveXCal procedure displays a widget that looks like the following:

Figure 4-1: An IDL widget program Using an ActiveX Calendar Control

ActiveXCal.gif