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 provided to illustrate a concept and should not be used in applications or Web sites, as it may not illustrate the safest coding practices. Microsoft assumes no liability for incidental or consequential damages should the sample code be used for purposes other than as intended.
To get samples and instructions for installing them:
On the Visual Studio Help menu, click Samples.
For more information, see Visual Studio Samples.
The most recent version and complete list of samples is available online from the Visual Studio 2008 Samples page.
You can also locate samples on your computer's hard disk. By default, samples and a Readme file are copied into a folder under \Program Files\Visual Studio 9.0\Samples\. For Express editions of Visual Studio, all samples are located online.
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.
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.
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