![]() |
![]() |
![]() |
![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() ![]() |
![]() |
![]() |
To access the contents, click the chapter and section titles.
Fast Track Visual C++ 6.0 Programming
Subclassing Controls: The Writer2 ActiveX ControlYou 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 eventa Change eventthat we set up, and the program that contains our Writer2 controlWriter2Appcan make use of that event. When that event occurs, the Writer2App program can check the Length property, which holds the text strings length. We can then display that propertys 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 Writer2Clear()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 | | | --------------------- | | | | | --------------------------------------------- Thats 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).
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 itthe Length property. ActiveX PropertiesTo 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, its safer to restrict access to our controls 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(); }
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 weve subclassed our control from the text box control, we can use that controls member functions directly. For example, to get a text boxs text length, you use GetWindowTextLength(). We do that here in GetLength(). short CWriter2Ctrl::GetLength() { return GetWindowTextLength(); ⇐ } We wont let other programs set the texts 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 weve added a new property, we add a new methodthe Clear() method.
|
![]() |
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. |