Brief Full Advanced Search Search Tips
|  |  | To access the contents, click the chapter and section titles. Fast Track Visual C++ 6.0 Programming (Publisher: John Wiley & Sons, Inc.) Author(s): Steve Holzner ISBN: 0471312908 Publication Date: 09/01/98 Search this book:
Previous | Table of Contents | Next
### Subclassing Controls: The Writer2 ActiveX Control You can base ActiveX controls on several standard Windows controls, such as text boxes, list boxes, and combo boxes. To see how this works, we base a new ActiveX control, Writer2, on a text box. This allows the user to enter text into our ActiveX control without any special coding on our part (unlike in the Writer project).
                 ---------------------------------------------
                |                                           |
                |---------------------------------------------      |
                |                                           |
                |                                           |
                |           ---------------------           |
                |          |Here is the text     |          |
                |           ---------------------           |
                |                                           |
                 ---------------------------------------------
We can also augment our new control to include events and properties. For example, when the user types into our control, our control can cause a new event—a Change event—that we set up, and the program that contains our Writer2 control—Writer2App—can make use of that event. When that event occurs, the Writer2App program can check the Length property, which holds the text string’s length. We can then display that property’s value in Writer2App, updating it each time there is a Change event.
                 ---------------------------------------------
                |                                         |
                |---------------------------------------------          |
                |                                         |
                |                                         |
                |           ---------------------         |
                |          |Here is the text     |        |
                |           ---------------------         |
                |                                         |
                |           ---------------------         |
                |          |Text length = 16     |        |
                |           ---------------------         |
                |                                         |
                |                                         |
                |                                         |
                |                                         |
                |                                         |
                |                                         |
                 ---------------------------------------------
In addition to events and properties, ActiveX controls can support methods. A method is a function built into an ActiveX control that the containing program can access. For example, we can add a new method to Writer2—Clear()—that clears the text in the control. We can invoke that new method from Writer2App by using a button.
                 ---------------------------------------------
                |                                         |
                |---------------------------------------------   |
                |                                                |
                |                                                |
                |           ---------------------                |
                |          |Here is the text             |       |
                |           ---------------------        |
                |                                        |
                |           ---------------------        |
                |          |Text length = 16             |       |
                |           ---------------------        |
                |                                        |
                |           ---------------------        |
                |          |     Clear text              |       |
                |           ---------------------        |
                |                                        |
                |                                        |
                 ---------------------------------------------
That’s what our new Writer2 control does. We subclass it from a text box and give it a property (Length), a method (Clear()), and an event (Change). Figure 13.6 Subclassing a Windows control in an ActiveX control. Create the new Writer2 project, using the Visual C++ ActiveX ControlWizard. In Step 2 of the ControlWizard, select EDIT in answer to the question “Which window class, if any, should this control subclass?” as shown in Figure 13.6. Click Finish to create the Writer2 project. Our ActiveX control is based on the text box control. We start developing our new control by adding a property to it—the Length property. #### ActiveX Properties To add a new property to our control, open ClassWizard and select the Automation tab. Then, click the Add Property button to open the Add Property box, as shown in Figure 13.7. Give this new property the External name Length and set its type to short (that is, short integer). Next, select the Get/Set methods button in the Implementation box. This allows other programs to access our control with two new functions, GetLength() and SetLength(). If we had clicked the Member variable button, other programs could reach the Length property as a data member of the Writer2 object via Writer2.Length. However, it’s safer to restrict access to our control’s internal data by using the access functions GetLength() and SetLength().
    short CWriter2Ctrl::GetLength()
    {
        // TODO: Add your property handler here

        return 0;
    }

    void CWriter2Ctrl::SetLength(short nNewValue)
    {
        // TODO: Add your property handler here

        SetModifiedFlag();
    }
Figure 13.7 Adding a property to an ActiveX control. In GetLength(), we want to return the length of the text string now in the text box, but how do we reach that text box? Because we’ve subclassed our control from the text box control, we can use that control’s member functions directly. For example, to get a text box’s text length, you use GetWindowTextLength(). We do that here in GetLength().
    short CWriter2Ctrl::GetLength()
    {

        return GetWindowTextLength();                                   ⇐

    }
We won’t let other programs set the text’s length, so we just beep if they try, adding this line to SetLength(). (Being able to restrict access to our internal data is one of the advantages of using Get and Set functions for properties.)
    void CWriter2Ctrl::SetLength(short nNewValue)
    {
        MessageBeep(-1);                                                ⇐

    }
Now that we’ve added a new property, we add a new method—the Clear() method.
Previous | Table of Contents | Next

| Products | Contact Us | About Us | Privacy | Ad Info | Home Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc. All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.