HOWTO: Adding ATL Control Containment Support to Any Window

ID: Q192560


The information in this article applies to:


SUMMARY

This article lists the steps needed to add ATL's generic control containment capability to any window, so that the window can host ActiveX controls. The control containment support conforms to the OCX 96 specification and supports windowless activation and flicker-free drawing.


MORE INFORMATION

Steps for adding control containment:

  1. Add the following header files and pragma directives to your code. If you want link to the containment code in Atl.dll add the following code:
    
          // AtlAxWinInit is implemented in Atl.dll
          #pragma comment(lib, "atl.lib")
          #include <atldef.h>
          #define _ATL_DLL_IMPL
          #include <atliface.h> 
    If you do not want to redistribute Atl.dll and want to link in the containment code statically, add the following.
    
          // link in AltAxWinInit() & containment code statically
          // this require a instance (_Module) of CComModule
          #include <atlbase.h>
          extern CComModule _Module;
          #include <atlcom.h>
          #include <atlhost.h>
          CComModule _Module; 


  2. Add the following code to the application initialization code, for example in the beginning of WinMain():
    
          // The following line is needed only for static linkage.
          //hInstance is module handle returned by ::GetModuleHandle(NULL)
          _Module.Init(NULL, hInstance);
    
          //Initialize ATL control containment code.
          AtlAxWinInit(); 


  3. You can now start creating ActiveX controls using the WIN32 CreateWindow() API, specifying "AtlAxWin" as the class name and either a GUID, ProgID, or URL as the title. For example:
    
          // Create the Calendar control specifying the ProgID.
          // Make sure the module handle you pass to CreateWindow is the same
          // module handle where AtlAxWinInit() was called from.
          HWND hWnd = ::CreateWindow("AtlAxWin", "MSCAL.Calendar",
             WS_CHILD|WS_VISIBLE, 10, 10, 500, 300, hParent, NULL,
             ::GetModuleHandle(NULL), NULL);
    
          // Same as above except CLSID is specified instead of ProgID
          // corresponds to ProgID "MSCAL.Calendar.7"
          HWND hWnd = ::CreateWindow("AtlAxWin",
             "{8E27C92B-1264-101C-8A2F-040224009C02}",
             WS_CHILD|WS_VISIBLE, 10, 10, 500, 300, hParent, NULL,
             ::GetModuleHandle(NULL), NULL);
    
          // Creates the Web Browser control & navigates to specified web page.
          HWND hWnd = ::CreateWindow("AtlAxWin", "<LINK TYPE="GENERIC" VALUE="http://www.microsoft.com",">http://www.microsoft.com",</LINK>
             WS_CHILD|WS_VISIBLE, 10, 10, 500, 300, hParent, NULL,
             ::GetModuleHandle(NULL), NULL);
    
          // Creates an instance of an dynamic HTML document.
          HWND hWnd = ::CreateWindow("AtlAxWin", "mshtml:<H1>Hello World</H1>",
             WS_CHILD|WS_VISIBLE, 10, 10, 500, 300, hParent, NULL,
             ::GetModuleHandle(NULL), NULL); 


  4. If you added _Module.Init(), add the following code in application termination code, for example in WinMain(), after the message loop.
    
    _Module.Term() 


You can get the IUnknown* of the control via AtlAxGetControl(). To get the IUnknown* of the container, use AtlAxGetHost(). The HWND returned from CreateWindow() for the control is passed as the first parameter in both functions.


REFERENCES

(c) Microsoft Corporation 1998, All Rights Reserved. Contributions by Jaganathan Thangavelu, Microsoft Corporation.

Additional query words:


Keywords          : kbActiveX kbATL kbATLWC kbCOMt kbCtrl kbVC600 kbATL300 kbATL300faq 
Version           : WINDOWS:3.0
Platform          : WINDOWS 
Issue type        : kbhowto 

Last Reviewed: April 30, 1999