INF: Extending Standard Windows Controls Through Superclassing

ID Number: Q76947

3.00

WINDOWS

Summary:

A Windows application can extend the behavior of a standard Windows

control by using the technique of superclassing. An application can

superclass a standard Windows control by retrieving its window class

information, modifying the fields of the WNDCLASS structure, and

registering a new class. For example, to associate status information

with each button control in an application, the buttons can be

superclassed to provide a number of window extra bytes.

This article describes a technique to access the WNDCLASS structure

associated with the standard "button" class.

More Information:

The following five steps are necessary to register a new class that

uses some information from the standard windows "button" class:

1. Call GetClassInfo() to fill the WNDCLASS structure.

2. Save the cbWndExtra value in a global variable.

3. Add the desired number of bytes to the existing cbWndExtra value.

4. Change the lpszClassName field.

5. Call RegisterClass() to register the new class.

The first step will fill the WNDCLASS structure with the data that was

used when the class was originally registered. In this example, the

second step is necessary so that when the "new" extra bytes are

accessed, the original extra bytes are not destroyed. Please note that

it is NOT safe to assume that the original cbWndExtra value was zero.

When accessing the "new" extra bytes, it is necessary to use the

original value of cbWndExtra as the base for any new data stored in

the extra bytes. The third step allocates the new extra bytes. The

fourth step specifies the new name of the class to be registered, and

the final step actually registers the new class.

Any new class created in this manner MUST have a unique class name.

Typically, this name would be similar but not identical to the

original class. For example, to superclass a button, an appropriate

class name might be "superbutton." There is no conflict with class

names used by other applications as long as the CS_GLOBALCLASS class

style is not specified. The standard Windows "button" class remains

unchanged and can still be used by the application as normal. In

addition, once a new class has been registered, any number of controls

can be created and destroyed with no extra coding effort. The

superclass is simply another class in the pool of classes that can be

used when creating a window.

The sample code below demonstrates this procedure:

BOOL DefineSuperButtonClass(void)

{

#define MYEXTRABYTES 8

HWND hButton;

WNDCLASS wc;

GetClassInfo(NULL, "button", (LPWNDCLASS)&wc);

iStdButtonWndExtra = wc.cbWndExtra; // Save this in a global

wc.cbWndExtra += MYEXTRABYTES;

lstrcpy((LPSTR)wc.lpszClassName, (LPSTR)"superbutton");

return(RegisterClass((LPWNDCLASS)&wc));

}

It is important to note that the lpszClassName, lpszMenuName, and

hInstance fields in the WNDCLASS structure are NOT returned by the

GetClassInfo() function. Please refer to page 4-153 of the "Microsoft

Windows Software Development Kit Reference Volume 1" for more

information. Also, each time a new class is registered, scarce system

resources are used. If it is necessary to alter many different

standard classes, the GetProp(), SetProp(), and RemoveProp() functions

should be used as an alternative approach to associating extra

information with standard Windows controls.