Subclassing and Superclassing

These are techniques that can augment or extend the properties of windows, whether they are based on existing, default window classes, such as controls like buttons and comboboxes, or on user-defined windows. The terms subclassing and superclassing are not used in the context of C++ but do have an object orientated connotation.


This is a process where the existing window procedure for an already created window is replaced by another, user-defined window procedure. The CallWindowProc api function can be used to get message handling behaviour from the original window procedure, if required.

There are two approaches to window subclassing. The first is usable by most windows operating systems (win9x,win2k,winxp and later), the second is only usable with a minimum operating system of winxp since it relies on ComCtl32.dll version 6, in particular three api functions exported by this library, namely DefSubclassProc, RemoveWindowSubclass and SetWindowSubclass. In the first, universally applicable approach, the new window procedure is set using SetWindowLongPtr and retrieved with the corresponding GetWindowLongPtr. While this method will work on most Windows operating systems, it suffers from the obvious flaw that others might subclass your subclassed window (or you might subclass theirs) with possibly disastrous consequences, particularly if one of the subclasses allocates memory but, due to later subclassing, is unable to free it giving rise to a memory leak. The second approach, while it addresses many of these management issues, is obviously limited to later operating systems.

A third, possible approach to window subclassing might be to use hooks. Using this approach, management of chains of subclasses becomes irrelevant as does the issue of limitation to specific families of operating systems.


This approach bases a new, unique window class off of an existing window class. The original window class's details are copied into a WNDCLASSEX structure using GetClassInfoEx and those parameters of the filled WNDCLASSEX structure that need to be modified, such as, for example, the window procedure and application instance, are changed as required. This modified WNDCLASSEX stucture is used with RegisterClassEx prior to creating windows of this new, unique registered window class.

Subclassed Button Control
image: subclassed button, click for larger image

In this simple example a button control is subclassed in order to enable editing of its caption when the mouse is right-clicked on the button surface. This causes an edit control to appear on the button control containing the current button caption. This example uses the generally applicable approach where the SetWindowLongPtr api function is used to replace the default, system window procedure for the button and consequently is not limited to specific operating systems.

Superclassed Edit Control
image: superclassed edit, click for larger image

This example superclasses an edit control to add the capability of changing the control's font. A small button is added to the top-left corner of the superclassed edit which, when clicked, activates a Font Common Dialog with which the user can select a new font. This is a proof of concept demonstration - the same 'feature' could be achieved in a number of ways, including subclassing.