Teach Yourself Visual C++ 5 in 21 Days

Chapter 3: Using ActiveX Controls


The third chapter in the text introduces you to ActiveX controls, add-on controls for Windows applications. The objectives important to this chapter are:

  • understanding ActiveX controls
  • adding an ActiveX control to a project
  • using an ActiveX control to a project

Sometimes the controls that are included in the tool bar of Visual C++ are not quite what you really need in a project. The included tools are not the only ones available. Add-on controls are available from Microsoft and other vendors.

Chapter 3 shows you how to add a new control to the tool bar. You will be adding an ActiveX control, one designed to work with Visual C++. Although the text tells you that this is just one file, whose extension is .OCX, there are other related files you may want as well, including a help file for the ActiveX control. Typically, the files associated with the new control all have the same file name, but with different extensions. Make sure they are all available, and there is no problem? Available? See below.

The next project begins on page 92, explaining that you are building a simple calendar program. Not much coding will be required because you will add an ActiveX control to the project tool bar that already knows how to draw a calendar.

As usual, you are told to create a directory for the new project, calling it CH03. You start the project as before, indicating that it will be a dialog-based application called MyCal. This time you are told to make sure that the check box for ActiveX control support is checked, as on page 94. Otherwise, the preliminary instructions are much like the last project.

On page 95, the instructions begin for adding the new control to the project. The file for the new control is named MSCAL.OCX, marking it as a Microsoft product. In fact, the file should already be installed in the appropriate directory on your system. Since we are using a Windows 95 environment, look for the file in C:\windows\system. Had we been on NT workstations, I would have told you to look for the file in C:\winnt\system32. If the file is not in the proper directory on your computer, the text advises you to look for your DEVSTUDIO directory. From there drill down to devstudio\vc\redist\ocx and look for the file in that directory. If you find it, copy it to the necessary system directory named above.

Just because you have the proper file on your hard drive, don't think the installation is over. The next step is on page 96, Registering the ActiveX Control. The instructions tell you to start from the Tools menu in Visual C++, and to select the ActiveX Control Test Container. As indicated in the note on page 96, this did not work on my system, so I used the alternate instructions: Click Start, Programs, Visual C++ 5, and then choose ActiveX Control Test Container.

Once the Test Container is open, follow the instructions on pages 96 and 97:

  1. Open the File Menu and choose Register Controls
  2. Click the Register button
  3. Drill up or down to find the MSCAL.OCX file, in the system directory noted above, select it and click Open.
  4. If you wish, find the file in the changed Registry list, to verify it is there.
  5. Click the Close button on the Registry window, and Exit the ActiveX Control Test Container

On page 98, you see the procedure to add the new control to the project you started.

  1. Get to the Resource view of the project.
  2. If necessary, expand the Resources object, expand the Dialog object, and select the object for your dialog box.
  3. Open the Project menu and select Add to Project
  4. Select Components and Controls
  5. Double-click the Registered ActiveX Controls object
  6. Select the control you wish to add (Calendar Control 8.0, in this case)
  7. Click OK to tell it you really, really want to do it!
  8. The next screen lets you pick which classes to bring in with the control. Just click OK to add them all.
  9. Click Close.

This takes us to page 102, which shows you the icon for the Calendar control that should now be in your tool bar. You use this control to add an on-screen calendar to the dialog box. Add two buttons as well, using the properties table on page 103 as your guide.

On page 104, you build the project to test it. At this stage, it is a prototype. You have completed the major design steps, an proceed to the coding steps.

On page 106, you add a variable to the project, as you have done before, attaching it to the calendar object. The code that uses this variable is added on pages 108 and 109. Essentially, you are just adding code to the screen initialization that calls a built in function belonging to the CCalendar class: Today(). This function sets the date associated with the calendar object to the current system date. Note the warning in the box on page 109, that failing to do this would allow the calendar object to exhibit its default behavior. Having set the date to the current date, each time the program is run you will see the current date displayed. The default behavior of the control is to display the date that the control was added to this project.

Next, you add code to the two buttons. The Exit button is easy. You just have it call the OnOK() function, closing the application (because it is a dialog box, right?). For the button that resets to screen to Today, you duplicate the code that you added to the initialization process.