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
## Chapter 13 Creating ActiveX Controls In this chapter, we build ActiveX controls in Visual C++. ActiveX controls are being supported in more and more Microsoft software and, thanks to Microsoft’s efforts, are now in fairly wide use on the Internet. Here, we use the Visual C++ ActiveX ControlWizard to create our controls. We create two controls in this chapter: Writer, an ActiveX control that lets the user type text into it; and Writer2, an improved version of the Writer control based on a text box control and incorporating a lot more text support than the Writer control. As we build these controls, we see how to support ActiveX properties, methods, and events. Properties are data members you can set or read, methods are member functions of ActiveX controls available to the program in which they’re embedded, and events are familiar to us now. We start with our first ActiveX control: the Writer ActiveX control. ### The Writer ActiveX Control The Writer ActiveX control is a fairly simple one. When the user embeds it in a program, he or she can type text into that control as shown in the following example:
                 ---------------------------------------------
                |                                         |
                |---------------------------------------------      |
                |                                           |
                |                                           |
                |           ---------------------           |
                |          |Here is the text     |          |
                |           ---------------------           |
                |                                           |
                |                                           |
                |                                           |
                |                                           |
                 ---------------------------------------------
We use the Visual C++ ActiveX ControlWizard to create the Writer program, which saves us a lot of time. #### Using the ActiveX ControlWizard Select the New item in Visual C++ and choose the ActiveX ControlWizard item in the New dialog box, giving this new project the name Writer, as shown in Figure 13.1. There are only two steps in this Wizard—accept all the defaults—and it creates the Writer project for us. For our purposes, the important files are WriterCtl.h and WriterCtl.cpp, which support our ActiveX control. In our Writer control, we let the user type text, so we set aside a CString object in WriterCtl.h to store that text.
    // WriterCtl.h : Declaration of the CWriterCtrl ActiveX Control class.

    /////////////////////////////////////////////////////////////////////////////
    // CWriterCtrl : See WriterCtl.cpp for implementation.
    class CWriterCtrl : public COleControl
    {
        DECLARE_DYNCREATE(CWriterCtrl)
        .
        .
    // Implementation
    protected:
        ~CWriterCtrl();

        DECLARE_OLECREATE_EX(CWriterCtrl)    // Class factory and guid
        DECLARE_OLETYPELIB(CWriterCtrl)      // GetTypeInfo
        DECLARE_PROPPAGEIDS(CWriterCtrl)     // Property page IDs
        DECLARE_OLECTLTYPE(CWriterCtrl)        // Type name and misc status

        CString text;                                                   ⇐
             .
             .
             .
Figure 13.1 Creating a new ActiveX control. We also initialize that text in the CWriterCtl class’s constructor.
    CWriterCtrl::CWriterCtrl()
    {
        InitializeIIDs(&IID_DWriter, &IID_DWriterEvents);

        text = “”;                                                      ⇐
    }
Now that we’ve set up storage space for our ActiveX control’s data, let’s get some data by accepting keystrokes from the user in our code. #### Accepting Keystrokes in an ActiveX Control The WM_CHAR message, which is artificially generated by applications when the user presses keys, won’t work here; instead, we use the WM_KEYDOWN message. Add a WM_KEYDOWN handler function to the CWriterCtrl class with Class-Wizard now.
    void CWriterCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        // TODO: Add your message handler code here and/or call default
    }
Because this message is in WM_KEYDOWN and not WM_CHAR, we have to do a little character interpretation before we add the new character to our text string. For example, the character stored in the nChar parameter sent to us is always in capitals in OnKeyDown(), and we need to fix that. To work with nChar, we first copy it into a CString object named temp.
    void CWriterCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CString temp(nChar);                                            ⇐
            .
            .
            .
To avoid trying to display control characters like the Shift key, we only display letters and spaces, so we return from OnKeyDown() if nChar falls outside this range.
    void CWriterCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CString temp(nChar);

        if((temp <  ‘A’ || temp >  ‘Z’) && temp !=  ‘ ’)                                    ⇐
            return;                                                     ⇐
                 .
                 .
                 .
We also make the newly struck key lowercase, using the CString function MakeLower() unless the Shift key was down.
    void CWriterCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CString temp(nChar);

        if((temp <  ‘A’ || temp >  ‘Z’) && temp !=  ‘ ’)
            return;

        if(GetKeyState(VK_SHIFT) >= 0)                                  ⇐
            temp.MakeLower();                                           ⇐
                .
                .
                .
Now that we’ve done a little preparation, we can add the new character to the text we’ve already stored.
    void CWriterCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CString temp(nChar);

        if((temp <  ‘A’ || temp >  ‘Z’) && temp !=  ‘ ’)
            return;

        if(GetKeyState(VK_SHIFT) >= 0)
            temp.MakeLower();

        text += temp;                                                   ⇐
           .
           .
           .
Then, just as we might with a standard Visual C++ program, we invalidate the ActiveX control so it’s redisplayed with our new text.
    void CWriterCtrl::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags)
    {
        CString temp(nChar);

        if((temp <  ‘A’ || temp >  ‘Z’) && temp !=  ‘ ’)
            return;

        if(GetKeyState(VK_SHIFT) >= 0)
            temp.MakeLower();

        text += temp;

        Invalidate();                                                   ⇐
        COleControl::OnKeyDown(nChar, nRepCnt, nFlags);
    }
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.