ComponentOne VSFlexGrid 8.0
Creating Controls Dynamically in ATL

As in MFC, you can create controls dynamically with ATL. These are the steps required:

  1. Insert the appropriate #import statement in the dialog header file or in the StdAfx.h file.

  2. Add two member variables to your dialog or window class:

    Example Title
    Copy Code
      CAxWindow m_wndGrid;      // host window
    
      IVSFlexGridPtr m_spGrid;  // pointer to control
    
  3. When the dialog or window is created, create the control and its host window, and attach the control to the window:

    Example Title
    Copy Code
      // initialize ATL ActiveX hosting
    
      AtlAxWinInit();
    
      // create the control (will fail if not registered)
    
    
    m_spGrid.CreateInstance(__uuidof(VSFlexGrid)));
    
      ATLASSERT(m_spGrid != NULL);
    
      // create the host window (nID = IDC_GRID1)
    
      // the nID is needed if you want to sink events.
    
      RECT rc;
    
      GetClientRect(&rc);
    
      m_wndGrid.Create(m_hWnd, rc, NULL,
    
                       WS_CHILD | WS_VISIBLE, 0, IDC_GRID1);
    
      // attach the control to the host window
    
      CComPtr spHost;
    
      m_wndGrid.QueryHost(&spHost1);
    
      spHost->AttachControl(m_spGrid, m_wndGrid);
    

As in MFC, this approach requires you to hook up the event handlers manually. You can also copy wizard-generated code, which does two things: it adds an IDispEventImpl declaration to your class so it inherits the ATL event handling mechanisms, and adds an event sink map to your class using BEGIN_SINK_MAP, SINK_ENTRY, and END_SINK_MAP macros. You still need to add the call to AtlAdviseSinkMap in order to start getting the events.

Also in ATL, unless you have a good reason to create the controls dynamically, you should still stick to the resource editor and the Wizard.

 

 


Copyright (c) GrapeCity, inc. All rights reserved.

Product Support Forum  |  Documentation Feedback