Extending Standard Windows Controls Through Superclassing

Last reviewed: November 29, 1995
Article ID: Q76947
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) versions 3.1
  • Microsoft Win32 Application Programming Interface (API) included with:

        - Microsoft Windows NT versions 3.5 and 3.51
        - Microsoft Windows 95 version 4.0
    

SUMMARY

A Windows-based 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;
   static char pszClassName[] = "superbutton";

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

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

   wc.cbWndExtra += MYEXTRABYTES;

   wc.lpszClassName= pszClassName;

   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.


Additional reference words: 3.00 3.10 3.50 4.00 95
KBCategory: kbui
KBSubcategory: UsrCtl


THE INFORMATION PROVIDED IN THE MICROSOFT KNOWLEDGE BASE IS PROVIDED "AS IS" WITHOUT WARRANTY OF ANY KIND. MICROSOFT DISCLAIMS ALL WARRANTIES, EITHER EXPRESS OR IMPLIED, INCLUDING THE WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. IN NO EVENT SHALL MICROSOFT CORPORATION OR ITS SUPPLIERS BE LIABLE FOR ANY DAMAGES WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF MICROSOFT CORPORATION OR ITS SUPPLIERS HAVE BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. SOME STATES DO NOT ALLOW THE EXCLUSION OR LIMITATION OF LIABILITY FOR CONSEQUENTIAL OR INCIDENTAL DAMAGES SO THE FOREGOING LIMITATION MAY NOT APPLY.

Last reviewed: November 29, 1995
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.