Control container supporting windowless activation

MFC has some nice Control container support embedded seamlessly into the CWnd class.
ActiveX controls can be created via CreateControl just as if they were normal Windows
controls. Besides, CWnd, the relevant classes are COleControlContainer, COleControlSite and COccManager
(declared in mfcsrcoccimpl.h)

I wanted to modify this container support so that controls supporting windowless activation
are created without a window. Windowless activation (used e.g. in IE4) is a technique used to
improve performance and minimize resource needs, especially when lots of controls are to be created.

The extension is implemented in principle by deriving new classes COleControlContainerEx and
COleControlSiteEx providing the required OLE Interfaces and overriding some base class functions.

While most parts of this task were straightforward, there were some parts in MFC
resisting simple means:

  • Some CWnd members I need to access are declared protected. MFC’s container support classes
    are friends of CWnd and can thus access these members, but derived classes cannot.
  • None of the CreateControl functions in CWnd, COleControlContainer, and COleControlSite
    is virtual.
  • COleControlContainer manages the contained site in a map HWND -> COleControlSite.
    This required each control to have a HWND.
  • A problem with the MFC import library causes a crash when adding new interfaces to
    COleControlContainer and COleControlSite.

Some hacks were required to solve these problems. The classes are now working, though
they should be considered experimental status and some more effort would be needed to
optimize drawing, support non-rectangular controls etc.
I would like to get some feedback on the implementation, especially how to get rid of those hacks.
Note that creating windowless controls by dialog templates is currently not supported.

The source code (42KB) contains files OccEx.h and OccEx.cpp implementing the new classes,
and a sample SDI project, where the view class creates a windowless control.

To use the extension, you will first have to register the new OCC manager in InitInstance:

	COccManagerEx g_occManager;

BOOL CTestApp::InitInstance()
{
AfxEnableControlContainer(&g_occManager);
// …

You must also prepare the container window to contains windowless controls.
Forwarding keyboard and mouse messages is done automatically by COleContainerEx
(by subclassing the WNDPROC of the container window), but
you will have to call COleContainerEx::OnDraw from your windows drawing code, i.e. from
OnDraw in CView derived classes and OnPaint in other CWnd derived classes (does someone
have an idea to handle this automatically, too?):


void CTestView::OnDraw(CDC* pDC)
{
CTestDoc* pDoc = GetDocument();
ASSERT_VALID(pDoc);
// …

if (m_pCtrlCont)
STATIC_DOWNCAST(COleControlContainerEx, m_pCtrlCont)->OnDraw(pDC);
}

The simplest method to create a control, is to create an instance of my CWindowlessControl
and call its CreateControl function. You can also use ClassWizard generated wrapper classes if
you change all occurrences of CWnd to CWindowlessControl in the generated H and CPP files.

More by Author

Get the Free Newsletter!

Subscribe to Developer Insider for top news, trends & analysis

Must Read