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
That’s it for handling keys. We still need to add the drawing code to the ActiveX control, which we do in the OnDraw() function. #### Displaying Data in an ActiveX Control The default OnDraw() function placed in our program by the ActiveX Control-Wizard simply draws a white rectangle and displays an ellipse in that rectangle.
    void CWriterCtrl::OnDraw(
                CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {
        // TODO: Replace the following code with your own drawing code.
        pdc->FillRect(rcBounds, CBrush::FromHandle((HBRUSH)GetStockObject(WHITE_BRUSH)));
        pdc->Ellipse(rcBounds);
    }
Here, the rcBounds rectangle holds the dimensions of the control’s bounding rectangle. We make use of those dimensions ourselves as we draw a rectangle surrounding the ActiveX control (that is, to make it visible in case it appears in a white window).
    void CWriterCtrl::OnDraw(
                CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {

        pdc->Rectangle(rcBounds);                                       ⇐
                .
                .
                .
    }
Then we simply draw the text in the CString text object, indenting that text’s position slightly to avoid obscuring the surrounding rectangle.
    void CWriterCtrl::OnDraw(
                CDC* pdc, const CRect& rcBounds, const CRect& rcInvalid)
    {

        pdc->Rectangle(rcBounds);
        pdc->TextOut(2, 2, text);                                       ⇐

    }
That completes the code for the Writer control. Building the control with Visual C++ automatically registers it with Windows and creates the actual ActiveX control itself—Writer.ocx. (The .ocx extension is left over from the days when such controls were called OLE controls, not ActiveX controls.) Now that we have our new ActiveX control, we can test it with the ActiveX Control Test Container, which is found in the Visual C++ Tools menu. #### Testing ActiveX Controls Open the Test container now and select the Insert New Control item from the Edit menu to open the Insert Control box. Select the entry Writer Control and click OK to insert a new Writer control in the Test container, as shown in Figure 13.2. As also shown in Figure 13.2, you can type into the new ActiveX control, resize it, move it, and so on. Our ActiveX control is a success; now we’re creating ActiveX controls in Visual C++. We still haven’t embedded the Writer control into a program yet, however, and ActiveX controls are designed to be used, so let’s put Writer to use now in a new program, WriterApp. We embed a Writer control in WriterApp and type into that new control. Figure 13.2 Testing the new Writer ActiveX control. #### Creating WriterApp Using AppWizard, create a new SDI program named WriterApp. To make it easy to embed the Writer control into WriterApp, base WriterApp’s View class on CFormView. To add the Writer control to the new WriterApp project, select the Project menu’s Add to Project item, which opens the Components and Controls Gallery as shown in Figure 13.3. Double-click the Registered ActiveX Controls folder and find the entry for the Writer control in that folder. Double-click that entry; Visual C++ asks you for a name for the new class corresponding to our control. Accept the default, CWriter (you can use this class to make a member object of the new Writer control in WriterApp). Now open the IDD_WRITERAPP_FORM resource from the Dialog folder in ResourceView, as shown in Figure 13.4. This is the main window for WriterApp. As you can see at right in Figure 13.4, our new ActiveX control appears in the control toolbox at the bottom left, with the letters OCX. Use that new tool to draw a Writer control in the WriterApp form now. Figure 13.3 The Visual C++ Components and Controls Gallery. Setting Your ActiveX Control’s Icon To change your ActiveX control’s icon from the letters OCX to something else, just open the control’s bitmap—that’s IDB_WRITER for Writer—in the Bitmap Editor and make the modifications you want. That’s all it takes because the Writer control is self-contained. Start the program now and type something into the Writer control, as also shown in Figure 13.5. You can see the typed text appear in the new control—Writer is a success. We’re creating our own ActiveX controls.
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.