# SUBEDIT Attributes Sample: Superclasses a Standard Windows Control

The SUBEDIT attributes sample demonstrates how to create an ATL control that superclasses the standard Windows Edit control.

The SUBEDIT sample is the nonattributed version of this sample.

Security Note
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.

# Building and Running the Sample

#### To build and run this sample

1. Open the solution file SubEdit.sln.

2. From the Build menu, click Build.

3. After the sample builds, open AtlEdit.htm in your Web browser and try out the control.

You can test out 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 Controls 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 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.

# Attributes

This sample uses the following attributes:

coclass, com_interface_entry, default, dual, emitidl, helpstring, implements_category, module, object, pointer_default, progid, registration_script, threading, uuid, version, vi_progid

# Keywords

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

Note
Some of the samples, such as this one, have not been modified to reflect the changes in the Visual C++ wizards, libraries, and compiler, but still demonstrate how to complete your desired task.