banner.jpg (64767 bytes)
Features
Tutorials
F.A.Q.s
Downloads
Articles
Open Architecture
Control Development Kit
RAD Foundation Class
Enhanced IntelliSense
Samples
Migration Center
Links

 

Examining Control Code

Now that you know how to create a skeleton RAD C++ control, it's time to look at the RadVC generated control code that make it function. While you will be developing the control further by adding more features to it, the understanding of the control code will help you to write better controls.

 

RadVC generates 4 files for any skeleton RAD C++ control you create. For our "Circle" control, here is the list of files RadVC generated for us.

Circle.h Header file. The control class "CCircle" is declared in this file.
Circle.cpp Implementation file. "CCircle" is implemented here.
Circle.rc Resource file. Control specific resources (e.g. bitmap, icon, strings etc) are defined here.
Circle.rh Resource header file. Control resource ID's are defined here.
Circle.bmp Control tool bitmap.

The two files that you will be playing most with are the header and the implementation files (circle.h and circle.cpp respectively).  You will be modifying  these files to add more features and attributes (e.g. properties/methods/events) to the control. Here is the listing of these two files.

CCircle Control Code

Header (Circle.h) Implementation (Circle.cpp)
class CCircle : public CWnd
{
    DECLARE_DYNCREATE(CCircle)
// Construction
public:
    CCircle(); // standard constructor
    virtual ~CCircle();
    BOOL Create(void* pParent, UINT nID, RECT rect);

    //Properties
    //{{AFX_CTRL_PROP_DEF_START
    //}}AFX_CTRL_PROP_DEF_END

    //Events
    //{{AFX_CTRL_EVENT_DEF_START
    //}}AFX_CTRL_EVENT_DEF_END

    //Methods
    //{{AFX_CTRL_METHOD_DEF_START
    //}}AFX_CTRL_METHOD_DEF_END

    // Controls
    //{{AFX_CTRL_DECLR_START
    //}}AFX_CTRL_DECLR_END

    void    DrawControl(CDC* pDC, CRect rect);
protected:
    void SetProperties();
    virtual void CreateControls();
    RECT    m_rect;
    UINT    m_nID;
    void*    m_pParent;

    //{{AFX_VIRTUAL(CCircle)
    //}}AFX_VIRTUAL

protected:
    // Generated message map functions
    //{{AFX_MSG(CCircle)
    afx_msg BOOL OnEraseBkgnd(CDC* pDC);
    //}}AFX_MSG
    DECLARE_MESSAGE_MAP()
};
#define CIRCLE_INVISIBLE_RT    0
CCircle::CCircle()
{
    //{{AFX_CTRL_DATA_START
    m_rect = CRect(0, 0, 312, 213);
    //}}AFX_CTRL_DATA_END
    m_nID = 0;
    m_pParent = NULL;
}
CCircle::~CCircle()
{
}
IMPLEMENT_DYNCREATE(CCircle , CWnd)

BEGIN_MESSAGE_MAP(CCircle, CWnd)
    //{{AFX_MSG_MAP(CCircle )
    ON_WM_ERASEBKGND()
    //}}AFX_MSG_MAP
END_MESSAGE_MAP()

void CCircle::CreateControls()
{
    //{{AFX_CTRL_IMPL_START
    //}}AFX_CTRL_IMPL_END
}
void CCircle::SetProperties()
{
    //{{AFX_SETPROPS_IMPL_START
    //}}AFX_SETPROPS_IMPL_END
}

BOOL CCircle::Create(void* pParent, UINT nID, RECT rect)
{
    m_nID = nID;
    ASSERT(pParent);
    m_pParent = pParent;
    BOOL bRet = TRUE;
    bRet = CWnd::Create(NULL, NULL, WS_CHILD | WS_VISIBLE | WS_TABSTOP, rect, (CWnd*)pParent, nID);
    if(bRet)
    {
        CreateControls();
        SetProperties();
    }
    return bRet;
}
BOOL CCircle::OnEraseBkgnd(CDC* pDC)
{
    CRect rect;
    GetClientRect(rect);
    DrawControl(pDC, rect);
    return TRUE;
}

void CCircle::DrawControl(CDC* pDC, CRect rect)
{
    pDC->FillSolidRect(rect, RGB(192, 192, 192));
    pDC->Ellipse(rect);
    pDC->DrawText(_T("C++ Rules!!"), rect, DT_CENTER | DT_VCENTER | DT_SINGLELINE);
}

 

Couple of points to note about the generated code:

(1) Control Initialization: Initialize the any control data in the constructor CCircle::Circle().

(2) Control Creation: The control is created through a function called "Create". It has the following format:

      BOOL Create(void* pParent, UINT nID, RECT rect);

Our "Circle" control is a "CWnd" derived class, thus its "Create" function calls the same of the base class

    CWnd::Create(DWORD dwStyle, CRect rect, CWnd* pWnd, UINT nID);

After the control is successfully created, CCirclle::Create(..) calls "CreateControls()" to create its child controls and "SetProperties()" to set the properties of the control itself and all of its child controls.

CCircle::Create(..) function creates the Windows handle whereas CCircle::Circle() merely initializes control data. Thus any initialization that depends on a valid Windows handle should be made after CWnd::Create(..) function in CCircle::Create() and not in the constructor.

(3) Drawing: Our "Circle" control is drawn in the function ::DrawControl(..) function, which is called from ::OnEraseBkgnd(CDC* pDC) message handler. As you can see, RadVC generates the necessary code for handling WM_ERASEBKGND Windows message in the control files. You will learn more on how to modify the code in ::DrawControl(..) function later in Part 4: Drawing on the New Control

(4) Child Controls: RadVC inserts declaration code for its child controls in the following comment block: (circle.h):

    // Controls
    //{{AFX_CTRL_DECLR_START
    //}}AFX_CTRL_DECLR_END

Controls are created in ::CreateControls() function in circle.cpp.

 

(5) Control Attributes: RadVC inserts code to declare properties, methods and events in the following comment blocks (circle.h):


    //Properties
    //{{AFX_CTRL_PROP_DEF_START
    //}}AFX_CTRL_PROP_DEF_END

    //Events
    //{{AFX_CTRL_EVENT_DEF_START
    //}}AFX_CTRL_EVENT_DEF_END

    //Methods
    //{{AFX_CTRL_METHOD_DEF_START
    //}}AFX_CTRL_METHOD_DEF_END

Later in the following links, you will learn more how you can add these attributes :

Part 5: Adding a Property to the New Control

Part 6: Adding an Event to the New Control

Part 7: Adding a Method to the New Control

 

Next >> Part 4: Drawing on the New Control

Related Links:
Control Development Kit (CDK) Home

 

 

Microsoft Visual C++, Microsoft Visual Basic and Microsoft Visual Studio are registered trademarks of Microsoft Corporation.
Copyright 1998-2000 Capitolsoft Corporation. All rights reserved.
Disclaimer: Capitolsoft Corporation is an independent business organization incorporated with the State Commission of Virginia and hence has no connection with U.S. Capitol or any other federal agency of U.S. government. Opinions expressed by the individuals in this site are of their own alone and hence do not necessarily reflect the views of Capitolsoft Corporation.

 

This page was last updated on November 21, 2005
Hit Counter