Knowledge Base

FIX: ATL Doesn't Call InitCommonControls in ActiveX Control

Article ID: 191355

Article Last Modified on 12/10/2003


APPLIES TO


This article was previously published under Q191355

SYMPTOMS

An ATL ActiveX control based on one of the common controls may not appear in an ActiveX control container.

CAUSE

The ATL Object Wizard doesn't add a call to InitCommonControls() when you insert an ActiveX control based on a common control. The call to InitCommonControls() is required if you use any of the common controls in your ATL ActiveX control.

NOTE: If you didn't select "Support MFC" in the ATL Object Wizard when creating your project, then you must link with Comctl32.lib in order to call InitCommonControls(). Otherwise, an LNK2001 linker error may occur when calling InitCommonControls().

Among the common controls, the richedit control is an exception. Instead of calling InitCommonControls(), you need to do a LoadLibrary() on Riched32.dll.

RESOLUTION

This problem has been fixed in Visual C++ 6.0. The ATL Object Wizard adds a call to InitCommonControls() for you. For richedit controls, you have to call LoadLibrary() and FreeLibrary() on Riched32.dll. If you're using Visual C++ 5.0 or earlier, just add the call to InitCommonControls() before calling Create() for the common control. The Wizard usually calls Create() in OnCreate():
   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam,
                    BOOL& bHandled)
   {
      RECT rc;
      GetWindowRect(&rc);
      rc.right -= rc.left;
      rc.bottom -= rc.top;
      rc.top = rc.left = 0;
      ::InitCommonControls();                // ADD THIS LINE
      m_ctlSysListView32.Create(m_hWnd, rc);
      return 0;
   }
				
For the richedit control, you'll need to load Riched32.dll and unload it:
   BEGIN_MSG_MAP(CRichEdit6)
      MESSAGE_HANDLER(WM_DESTROY, OnDestroy) // Add this line.
      // ...
   END_MSG_MAP()

   HINSTANCE m_hLibRichEdit;                 // Add this line.

   LRESULT OnCreate(UINT uMsg, WPARAM wParam, LPARAM lParam,
                    BOOL& bHandled)
   {
      RECT rc;
      GetWindowRect(&rc);
      rc.right -= rc.left;
      rc.bottom -= rc.top;
      rc.top = rc.left = 0;
      m_hLibRichEdit =
         LoadLibrary(_T("RICHED32.DLL"));    // Add this line.
      m_ctlRichEdit.Create(m_hWnd, rc);
      return 0;
   }

   // Add this function.
   LRESULT OnDestroy(UINT, WPARAM, LPARAM, BOOL&)
   {
      m_ctlRichEdit.DestroyWindow();
      FreeLibrary(m_hLibRichEdit);
      return 0;
   }
				

STATUS

Microsoft has confirmed this to be a bug in the Microsoft products listed at the beginning of this article. This bug has been corrected in Microsoft Visual C++, 6.0.

MORE INFORMATION

Containers such as ActiveX Control Test Container in MFC 4.2 already call InitCommonControls() so this problem does not occur.

Additional query words: ocx RichEdit SysListView32 SysTreeView32 SysAnimate32 SysTabControl32 rich edit listview treeview animate tab tabcontrol tooltip header hotkey image list progress slider spin buttton

Keywords: kbbug kbfix kbvc600fix kbatl300fix kbctrlcreate kbwizard KB191355