Using VSFlexGrid in ATL projects

Using the VSFlexGrid control in ATL is not much different than using it in MFC projects. You can still use Wizards and a rich set of low-level support classes. What you don't get is the higher-level classes, document/view architecture, and other amenities provided by MFC.

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

1.   Create a new ATL COM project of type "Executable".

2.   Select the Insert | New ATL Object menu option, select the Miscellaneous object type, then choose Dialog. Pick any name for the dialog.

3.   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). You may also want to set the dialog's ClipChildren property to True to make it repaint more smoothly.

4.   Right-click on the control and select the Events option. Then select the grid control from the list on the right and the list on the left will show all the events available for the control. Select the ones you want to handle by double-clicking them, and click OK when you are done. This will automatically insert an #import statement into the dialog header file.

5.   Edit the #import statement and remove all qualifiers except for no_namespace. If you are using the OLEDB/ADO version of the VSFlexGrid control, then you need two #import statements instead of one (this is because the OLEDB/ADO version of the grid relies on the Microsoft DataSource object, which needs to be imported as well):

  #import "c:\windows\system\msdatsrc.tlb" no_namespace

  #import "c:\windows\system\vsflex8l.ocx" no_namespace

6.   Now the control is on the form, but you can't talk to it yet. To get a pointer to the control, open the dialog header file and edit the OnInitDialog function so it looks like this:

  IVSFlexGridPtr m_spGrid;   // pointer to the control

  CAxWindow      m_wndGrid;  // pointer to the host window

  LRESULT OnInitDialog(UINT uMsg, WPARAM wParam,

                       LPARAM lParam, BOOL& bHandled)

  {

    m_wndGrid = GetDlgItem(IDC_VSFLEXGRID1);  // get host window

    m_wndGrid.QueryControl(&m_spGrid);        // get control

    m_wndGrid.SetFocus();           // activate the control

    AtlAdviseSinkMap(this, True);   // hook up events

    return 1;  // let the system set the focus

  }

7.   This code declares a pointer to the control and one to the control's host window. When the dialog initiates, the code makes m_wndGrid point to the host window and queries it for the contained control, which is stored in the m_spControl variable. From then on, you may move and resize the control through its host window (m_wndGrid) and access the control's properties and methods through the control pointer (m_spControl).

8.   The only thing missing is the code that displays the dialog. That needs to be added to the project's main cpp file. Here's the code that you will need:

  #include "MyProject_i.c"

  #include "MyDlg.h"   // add this line

  // wizard-generated code . . .

 

  extern "C" int WINAPI _tWinMain(HINSTANCE hInstance,

      HINSTANCE /*hPrevInstance*/, LPTSTR lpCmdLine, int /*nShowCmd*/)

  {

    // wizard-generated code . . .

    CMyDlg dlg;    // create the dialog

    dlg.DoModal(); // show the dialog

  //MSG msg;       // comment these lines out

  //while (GetMessage(&msg, 0, 0, 0))

  //  DispatchMessage(&msg);

  }

That's about it. You could clean up this project by removing the references to the idl and rgs files, which it doesn't need (it is just an EXE, not a COM server). See the samples on the distribution CD to find out what changes are necessary or refer to the ATL documentation for more details.