ActiveX Control Containers: Using Controls in a Non-Dialog Container

OverviewHow Do ISample

In some applications, such as an SDI or MDI application, you will want to embed a control in a window of the application. The Create member function of the wrapper class, inserted by Gallery, can create an instance of the control dynamically, without the need for a dialog box.

The Create member function has the following parameters:

lpszWindowName

A pointer to the text to be displayed in the control’s Text or Caption property (if any).

dwStyle

Windows styles. For a complete list, see .

rect

Specifies the control’s size and position.

pParentWnd

Specifies the control’s parent window, usually a CDialog. It must not be NULL.

nID

Specifies the control ID and can be used by the container to refer to the control.

One example of using this function to dynamically create an ActiveX control would be in a form view of an SDI application. You could then create an instance of the control in the WM_CREATE handler of the application.

For this example, CMyView is the main view class, CCirc2 is the wrapper class, and CIRC2.H is the header (.H) file of the wrapper class.

Implementing this feature is a four-step process.

To dynamically create an ActiveX control in a non-dialog window

  1. Insert CIRC2.H in CMYVIEW.H, just before the CMyView class definition:

    #include "circ2.h"
    
  2. Add a member variable (of type CCirc2) to the protected section of the CMyView class definition located in CMYVIEW.H:

    class CMyView : public CView
    {
    ...
    protected:
    
    CCirc2 m_myCtl;
    ...
    };
    
  3. Add a WM_CREATE message handler to class CMyView.

  4. In the handler function, CMyView::OnCreate, make a call to the control’s Create function using the this pointer as the parent window:

    int CMyView::OnCreate(LPCREATESTRUCT lpCreateStruct)
    {
       if (MyView::OnCreate(lpCreateStruct) == -1)
          return -1;
    
    // ****** Add your code below this line ********** //
    
       m_myCtl.Create(NULL, WS_VISIBLE,
          CRect(50,50,100,100), this, 0);
       m_myCtl.SetCaption(_T(“Control created”));
    
    // ****** Add your code above this line ********** //
    
       return 0;
    }
    
  5. Rebuild the project. A Circ2 control will be created dynamically whenever the application’s view is created.