How Do I Load Controls Specified at Runtime?

To load controls dynamically, you need to create an "AtlAxWin" hosting window and specify the control that it should host. There are two main ways of doing this:

  1. Use the standard window creation API and the ATL hosting API. This technique is described in the Knowledge Base article, "Adding ATL Control Containment Support to Any Window" (Q192560). You can find Knowledge Base articles on the MSDN Library CD-ROM or at http://support.microsoft.com/support/.

  2. Use the CAxWindow class as described below:
    • Make sure that AtlAxWinInit has been called.
      AtlAxWinInit();
      

      AtlAxWinInit initializes the control hosting code.

    • Create a CAxWindow object:
      CAxWindow wnd;
      

      CAxWindow is a CWindow-derived wrapper for creating and manipulating "AtlAxWin" windows.

    • Create a host window and control by calling CWindow::Create:
      RECT rect = { 0, 0, 100, 100 };
      wnd.Create(m_hWnd, rect, _T("MSCAL.Calendar"), WS_CHILD | WS_VISIBLE | WS_CLIPSIBLINGS | WS_CLIPCHILDREN, WS_EX_CLIENTEDGE);
      

      The window title (the third parameter) passed to the Create function is a string identifying the control to create. This string can be a CLSID (with braces), a ProgID, a URL, or raw HTML (prefixed with MSHTML:). If either a URL or raw HTML is supplied, the Web browser will be loaded with this information.

For information on control lifetimes, see How Do I Destroy a Control?

ATL Control Containment FAQ