Control Properties

Adding properties to control.ocx is easy with Visual C++'s ClassWizard. Let's add a property named Counter, which is like a data member of a C++ object. Counter simply holds a short value that we can initialize to zero and that we can increment as we like (we will count the number of times the button is clicked).

To add Counter to our custom control, open ClassWizard and select the OLE Automation tab. Make sure that the control's class, CControlCtrl, is selected in the Class Name box, as shown in Figure 9.8, and click Add Property, opening the Add Property box.

Figure 9.8  Using ClassWizard, we add a property to CONTROL.

Type the name of the new property, Counter, in the External Name box of the Add Property box, and select short in the Type box, making Counter a short value. Finally, click OK to create the new property. When controls of type CONTROL are inserted in our Web pages, we can reach this new property, Counter, easily, as we'll see in a minute.

Inside the CONTROL project, the new property will be stored in the short variable m_counter (declared by ClassWizard in CONTROLCTL.H). If you want to set the initial value of m_counter to something other than 0, you would look for this line in CONTROLCTL.CPP (do not add this code):

/////////////////////////////////////////////////////////////////////////////
// CControlCtrl::DoPropExchange - Persistence support
void CControlCtrl::DoPropExchange(CPropExchange* pPX)
{
    ExchangeVersion(pPX, MAKELONG(_wVerMinor, _wVerMajor));
    COleControl::DoPropExchange(pPX);
--> PX_Short(pPX, _T("Counter"), m_counter, 0);
    // TODO: Call PX_ functions for each persistent custom property.
}

Here, the program sets up the Counter property; the PX_Short() function is used to initialize short properties. The arguments the program passes to PX_Short() are as follows: pPX is a pointer to the program's internal CPropertyExchange object (maintained by our program automatically), followed by the name of the property (Counter), followed by the name of the internal variable that holds the property's value (m_counter), and its initial value, here set to 0. That's exactly how a counter should start, at 0. We'll leave this line unchanged.

In addition to the new internal variable m_counter, ClassWizard has written a new function, OnCounterChanged() (from CONTROLCTL.CPP):

void CControlCtrl::OnCounterChanged() 
{
    // TODO: Add notification handler code
    SetModifiedFlag();
}

The program calls this handy function when the Counter property is changed. For example, if changing this property is supposed to change other properties as well, we can make those changes in this function. Say that a control displays text and that the control's Text property (assuming the control has such a property) was changed. In this case, the program would call OnTextChanged(), allowing the control to update its display.

To test the new Counter property, create control.ocx using Build control.ocx in the Visual C++ Project menu. Open the control test container using the OLE Control Test Container item in the Tools menu. Insert a new control of type COUNTER in the test container and select Properties in the test container's View menu, opening the Properties box, as shown in Figure 9.9.

Figure 9.9  Setting our OCX control's Counter property.

Select Counter in the drop-down listbox labeled Property; the Counter property is set to 0, as shown in Figure 9.9. Using the Counter property in a Web page is easy, too. If the new control is given the ID Ctrl1, for example, we can reach the Counter property by referring to it as Ctrl1.Counter:

<HTML>
<HEAD>
<TITLE>Control Page</TITLE>
</HEAD>
<BODY LANGUAGE = VBScript ONLOAD = "Page_Initialize">
<CENTER>
<H1>Control Page</H1>
</CENTER>
<!- control.ocx>
<CENTER>
<OBJECT CLASSID="clsid:D96FBCC1-090A-101C-BAC7-040224009C02" HEIGHT=80 
        WIDTH=100 ID=Ctrl1></OBJECT>
<INPUT TYPE = TEXT NAME = Textbox SIZE=20>
</CENTER>
<SCRIPT LANGUAGE = VBScript>
        Sub Page_Initialize
  -->           Ctrl1.Counter = 5
        End Sub
</SCRIPT>
</BODY>
</HTML>

That's all there is to it. We can also increment the Counter property each time the user clicks the control. Return to Visual C++ and use ClassWizard to add a WM_LBUTTONDOWN message handler to the CControlCtrl class. Open that function (from CONTROLCTL.CPP):

void CControlCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    COleControl::OnLButtonDown(nFlags, point);
}

This function is called when the user clicks our custom button, and we can increment the Counter property each time by incrementing the internal variable m_counter:

void CControlCtrl::OnLButtonDown(UINT nFlags, CPoint point) 
{
    // TODO: Add your message handler code here and/or call default
    COleControl::OnLButtonDown(nFlags, point);
--> m_counter++;
}

And that's all there is to it. As shown in Figure 9.10, we've incremented Counter to a value of 5.

Figure 9.10  We increment the Counter property by clicking our control.

It was easy to add properties to our control. Now let's take a look at how to add a method.

© 1996 by Steven Holzner. All rights reserved.