SAMPLE: Code to Demonstrate Superclassing Available

Last reviewed: February 15, 1996
Article ID: Q32167
The information in this article applies to:
  • Microsoft Windows Software Development Kit (SDK) for Windows versions 3.0 and 3.1

SUMMARY

There is a sample application available in the Microsoft Software Library that demonstrates the concept of superclassing control windows. Superclassing provides the same capabilities as subclassing; however, it can be much more convenient to use and produces smaller code if there are multiple controls to be altered in the same fashion.

Download SUPERCLS.EXE, a self-extracting file, from the Microsoft Software Library (MSL) on the following services:

  • Microsoft Download Service (MSDL)

          Dial (206) 936-6735 to connect to MSDL
          Download SUPERCLS.EXE (size: 24247 bytes) 
    
  • Internet (anonymous FTP)

          ftp ftp.microsoft.com
          Change to the \SOFTLIB\MSLFILES directory
          Get SUPERCLS.EXE (size: 24247 bytes) 
    

MORE INFORMATION

To superclass, create a new class that uses the control window procedure instead of the default window procedure. This enables the application to process the appropriate messages and to pass the others along to the default procedure where the messages will be processed correctly based on the control type.

To implement superclassing, create a window of the type to superclass. Query all available information about the control and use this information when the new window is created, substituting the class name and window procedure. When this window procedure is called, process the desired messages and pass the others to the standard control procedure (determined in the query phase).

By registering a new class, the application can create superclassed controls using the application resource (RC) file or CreateWindow() function without any further work, which can eliminate a number of SetWindowLong() calls if many controls are to be modified in the same manner.

In the SUPERCLS example, the application creates a new control class called "SuperEdit", which is identical to a regular edit control but refuses to accept the "b" character.

Look for the string "SuperEdit" in the .C, .RC, and .DEF files to see the affected areas of the code.

This example shows superclassed Windows controls. Because the class name has been changed and the application's instance is used in the class registration, all controls will be created with this application's DS. This causes no problems for controls. However, this does not work if an attempt is made to superclass something other than a control. In that case, it is desirable to use the application's instance with the class so that it will be destroyed when the application exits. However, it is necessary to perform some additional work before the call to CallWindowProc() to set the DS for the subclassed window procedure. The hInstance passed to CreateWindow() determines which DS the superclassing procedure uses; to have the superclassed procedure use a different DS, it is necessary to change the DS value. For more information, see the notes near the call to CallWindowProc().

Each edit control created using DialogBox(), CreateDialog(), or through an RC file normally has its own DS. When the application changes the class name, the code within Windows that handles edit controls is not activated. Instead, the control uses the application's DS as if it were created using CreateWindow(). This does not cause any difficulties except that when the code for the superclassed edit control calls LocalAlloc(), it consumes space in the application's local heap. This should not cause problems in most cases.


Additional reference words: 3.00 3.10 sub class subclass control softlib
SUPERCLS.EXE
KBCategory: kbprg kbfile
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: February 15, 1996
© 1998 Microsoft Corporation. All rights reserved. Terms of Use.