SUBEDIT: Superclasses a Standard Windows Control

Click to open or copy the SUBEDIT project files.

This sample demonstrates how to create an ATL control that superclasses the standard Windows Edit control.

ATL provides the ability to create a control that superclasses a standard Windows control. Superclassing allows you to create a window class that is based on an existing class but uses a different window procedure. You then create a window based on this new window class. When you superclass a control, messages are first processed by an ATL message map before being sent to the control's original window procedure. This allows you to modify the default behavior of standard Windows controls.

When you use the ATL Object Wizard to create an ActiveX control, you can choose to add a control based on a standard window class. In this case, the wizard adds a member variable of type CContainedWindow to your ActiveX control's class. CContainedWindow::Create then creates a window that superclasses the window class you specified. This window uses CContainedWindow::WindowProc to route its messages through a message map. If a message needs further processing, it is sent to the original window procedure of the window class.

Building the SUBEDIT Control

Download the sample files, build the control, and insert it into a container.

Examining the SUBEDIT Sample Code (AtlEdit.h)

  1. The constructor for the CAtlEdit class sets the m_bWindowOnly member variable to TRUE. This ensures the control will never activate as a windowless control.

  2. The CContainedWindow member variable, m_EditCtrl, is initialized by the CAtlEdit constructor. The CContainedWindow constructor takes three parameters: the name of the window class to be superclassed (in this case, "EDIT"); a pointer to the CAtlEdit class, which contains the message map; and the identifier of the message map that will process m_EditCtrl's messages. By default, m_EditCtrl uses an alternate message map, declared with the ALT_MSG_MAP macro.

  3. The default message map declares the names of the handler functions for WM_CREATE and WM_CTLCOLOREDIT messages sent to the CAtlEdit control. The OnCreate handler calls CContainedWindow::Create to create m_EditCtrl's window. The OnCtlColorEdit handler specifies a new background and text color for m_EditCtrl.

  4. The alternate message map declares a handler function for WM_CHAR messages sent to m_EditCtrl. This handler only accepts characters, not symbols or numbers, and then passes the WM_CHAR message to the original window procedure defined by the Windows Edit class.

This sample uses the following keywords:

CComCoClass; CComControl; CComModule::GetClassObject; CComModule::GetLockCount; CComModule::Init; CComModule::RegisterServer; CComModule::Term; CComModule::UnregisterServer; CComObjectRoot; CContainedWindow::DefWindowProc; DisableThreadLibraryCalls; GetStockObject; GetWindowRect; IDataObjectImpl; IDispatchImpl; IOleControlImpl; IOleInPlaceActiveObjectImpl; IOleInPlaceObjectWindowlessImpl; IOleInPlaceObjectWindowlessImpl::SetObjectRects; IOleObjectImpl; IPersistStorageImpl; IPersistStreamInitImpl; IProvideClassInfo2Impl; IQuickActivateImpl; ISpecifyPropertyPagesImpl; IViewObjectExImpl; SetBkColor; SetTextColor; SetWindowPos