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
#### ActiveX Methods To add a new method to the Writer2 control, select the Add method button in ClassWizard to open the Add method box. Give this new method the External name “Clear” and set the Return type to void, as also shown in Figure 13.8. Then click OK to create this new method.
    void CWriter2Ctrl::Clear()
    {
        // TODO: Add your dispatch handler code here

    }
Adding Parameters to Methods If you want to add parameters to your method, just enter those parameters and their types in the Parameter list box in the Add Method box. If another program creates a member object to our Writer2 ActiveX control, that program can use our Clear() method this way: Writer.2Clear(). To actually clear the text in the text box control, we use the SetWindowText() function.
    void CWriter2Ctrl::Clear()
    {

      SetWindowText(“”);                                                ⇐
    }
Figure 13.8 Adding a method to an ActiveX control. Figure 13.9 Adding an event to an ActiveX control. Now we’ve added both a property and a method to our ActiveX control. Next, we add a new event, the Change event. #### ActiveX Events To add a new event to the Writer2 control, click the ActiveX Events tab in ClassWizard and select the Add Event button to open the Add Event box, as shown in Figure 13.9. Give this new event the External name Change and click OK. This creates a new event, the Change event. We want to make this event occur—called firing the event—when the user types anything into the Writer2 control, so we connect a handler function to the WM_KEYDOWN message.
    void CWriter2Ctrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        // TODO: Add your message handler code here and/or call default

        COleControl::OnKeyDown(nChar, nRepCnt, nFlags);
    }
To fire the Change event, we call the FireChange() function that ClassWizard has added to our program.
    void CWriter2Ctrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {

        FireChange();                                                   ⇐

        COleControl::OnKeyDown(nChar, nRepCnt, nFlags);
    }
Now that we’ve created the Writer2 control and given it a Length property, a Clear() method, and a Change event, it’s time to put this control to work. #### Creating Writer2App We use the Writer2 control in a new program, Writer2App. Create that project, basing its view on the CFormView class. Figure 13.10 Designing the Writer2App program. Next, open the Components and Controls Gallery and add the Writer2 control to the project, accepting the new class Visual C++ proposes for this control: CWriter2. Finally, open the main form window and add a new Writer2 control to the window as well as a text box and a button, giving that button the caption “Clear text,” as shown in Figure 13.10. Now use ClassWizard to add a new member variable to the Writer2 control. Click the Member Variables tab in ClassWizard and select the IDC_WRITER2-CTRL1 entry, adding a new member variable to the Writer2 control with the name m_writer2. Now we can access our control’s methods and properties conveniently. We need to make the text box in Writer2App active first. When the user enters text in the Writer2 control, we can use that control’s Change event to display the new text’s length in Writer2App’s text box. Click the Message Maps tab in Class-Wizard and find the IDC_WRITER2CTRL1 entry. There is one event associated with that control: Change. Add a new handler function to that event now.
    void CWriter2AppView::OnChangeWriter2ctrl1()
    {

        // TODO: Add your control notification handler code here

    }
We want to display the text length in the Writer2 control, and we do that with the control’s Length property. Using ClassWizard again, connect a CString variable to Writer2App’s text box and name that variable m_LengthText. Now when the user types into the Writer2 control, we can get the new length of the text by calling m_writer2’s GetLength() function. (Note that we add 1 to the value returned by GetLength(), since that function returns the current length of the text in the control before the current key is added.)
    void CWriter2AppView::OnChangeWriter2ctrl1()
    {
        char text[30];                                                  ⇐

        wsprintf(text, “Text length = %d”, m_writer2.GetLength() + 1);  ⇐
        .
        .
        .
Then we display the new text length in Writer2App’s text box.
    void CWriter2AppView::OnChangeWriter2ctrl1()
    {
        char text[30];

        wsprintf(text, “Text length = %d”, m_writer2.GetLength() + 1);

        m_LengthText = text;                                            ⇐

        UpdateData(false);                                              ⇐
    }
Finally, we enable the Clear text button in Writer2App. Attach a handler function to that button now.
    void CWriter2AppView::OnButton1()
    {
        // TODO: Add your control notification handler code here

    }
Here, all we have to do is to use m_writer2’s Clear() method to clear the text in the Writer2 control and update the text in the text box.
    void CWriter2AppView::OnButton1()
    {
        m_writer2.Clear();                                              ⇐

        m_LengthText = “Text length = 0”;                               ⇐

        UpdateData(false);                                              ⇐
    }
Figure 13.11 Using the Writer2 control. That completes Writer2App. Run the program now, as shown in Figure 13.11, and type some text into the Writer2 control. Doing so fires the Change event, and that updates the text in the text box, which indicates the text’s length. In addition, clicking the Clear text button invokes the Writer 2 Clear() method and clears the text in the Writer2 control. Through our successful Writer2 control, we’ve been able to support ActiveX properties, methods, and events. That’s the real core of ActiveX programming; with this support, we’re able to let our controls interact with the programs they’re embedded in and do what they are designed to do, when they’re supposed to do it.
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.