Using VSFlexGrid in MFC projects

To use the VSFlexGrid control in MFC projects, you will normally follow these steps:

1.   Create a new dialog-based MFC project.

2.   Go to the resource editor, open the dialog box, right-click on it, select Insert ActiveX control, and pick the VSFlexGrid control from the list (if the grid is not on the list, it hasn't been registered on your computer).

3.   Hold down the CTRL key and double-click on the grid. This will cause Developer Studio to generate wrapper classes through which you can interact with the control. When the wrapper classes are ready, select a name for the control (for example, m_Grid).

4.   From now on, things are pretty much the same as in VB. You can right-click on the control to implement event handlers, and access the controls properties and methods through the m_Grid variable. Most properties are exposed through GetPropertyName and SetPropertyName member functions. Unfortunately, enumerated values get translated into longs instead of their proper enumeration symbols, but that's a relatively minor inconvenience. (And one that can be avoided, read on).

If you take a look at the CVSFlexGrid wrapper generated by Developer Studio, you will see that the class is derived from CWnd. This means you can move, size, show, or hide the control as if it were a regular window. The wrapper class also has a handy Create function that lets you create new instances of the control. For example, if you add this declaration to the main dialog's header file:

  class CMyDlg : public Cdialog

  {

    // Construction

    public:

        // standard constructor

        CMyDlg(CWnd* pParent = NULL);

 

        // new VSFlexGrid

        CvsFlexGrid m_GridDynamic;    

You can create the control by adding the following code to the dialog's OnInitDialog function:

  BOOL CMyDlg::OnInitDialog()

  {

    // Wizard-generated code

 

    // TODO: Add extra initialization here

    // create a second instance of the VSFlexGrid control

    RECT rc;

    GetClientRect(&rc);

    InflateRect(&rc, -5, -5);

    rc.left = (rc.left + rc.right) / 2;

    m_GridDynamic.Create(NULL, WS_VISIBLE, rc, this, 100);

    return TRUE;  // return TRUE

  }

The problem with this second approach is that you have to hook up the event handlers manually. You can do this by copying the code created by the Wizard for the first control (it involves using several macros to define an "event sink"). Hooking up the events manually is not difficult, but it is a tedious and error-prone process. Unless you have a good reason to create the controls dynamically, you should stick to the resource editor and the Wizard.

To use the enumerated symbols to avoid the translation, use the #import directive. For example:

    // add VSFlexGrid support

    #import "..\imports\vsflex8l.ocx" no_namespace

This will cause VS to import the types and methods in the OCX. Enumerations are appropriately imported as symbols which can be used with the MFC wrappers.

This covers most of what you need to know about using ActiveX controls in MFC. There are a couple of issues that deserve additional explanation (at least our tech support department gets many questions on these): handling optional parameters, Picture properties, and dual interfaces.