HOWTO: Adding ATL Control Containment Support to Any Window

ID: Q192560


The information in this article applies to:
  • The Microsoft Active Template Library (ATL) 3.0, used with:
    • Microsoft Visual C++, 32-bit Editions, version 6.0


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> 
    Atl.dll needs to be shipped when using ATL containment code irrespective of whether you 'Min Size' or 'Min Dependency' build. Refer to Q244955 for more information.



  2. Add the following code to the application initialization code, for example in the beginning of WinMain():


  3. 
          //Initialize ATL control containment code.
             AtlAxWinInit(); 
  4. 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:


  5. 
          // 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); 

  6. 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 kbATLWC kbCOMt kbContainer kbCtrl kbVC600 kbATL300 kbfaq kbGrpMFCATL
Version : WINDOWS:3.0
Platform : WINDOWS
Issue type : kbhowto


Last Reviewed: January 31, 2000
© 2000 Microsoft Corporation. All rights reserved. Terms of Use.