SUBEDIT Sample: Superclasses a Standard Windows Control
The SUBEDIT sample demonstrates how to create an ATL control that superclasses the standard Windows Edit control.
The SUBEDIT attributes sample is the attributed version of this sample.
This sample code is intended to illustrate a concept, and it shows only the code that is relevant to that concept. It may not meet the security requirements for a specific environment, and it should not be used exactly as shown. We recommend that you add security and error-handling code to make your projects more secure and robust. Microsoft provides this sample code "AS IS" with no warranties.
To get samples and instructions for installing them:
To access samples from Visual Studio
On the Help menu, click Samples.
By default, these samples are installed in drive:\Program Files\Microsoft Visual Studio 10.0\Samples\.
- For the most recent version of this sample and a list of other samples, see Visual Studio Samples on the MSDN Web site.
Building and Running the Sample
To build and run this sample
Open the solution file SubEdit.sln.
From the Build menu, click Build.
After the sample builds, open AtlEdit.htm in your Web browser and try out the control.
You can test the control in the ActiveX Control Test Container by opening the ATLEdit Class. For details on accessing Test Container and using it to test a control, see Testing Properties and Events with Test Container.
Superclassing a Windows 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 Control 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.
Examining the SUBEDIT Sample Code (AtlEdit.h)
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.
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.
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.
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