Subclassing a Window

The use of subclassing to modify the methods of an existing window class has been described in Subclassing. An application subclasses a particular window instance (rather than the entire window class) by creating a subclass window procedure, and registering this window procedure to Presentation Manager using the WinSubclassWindow() function.

The use of the WinSubclassWindow() function is shown in Figure "WinSubclassWindow() Function".

The WinSubclassWindow() function substitutes a new window procedure, known as the subclass window procedure, for the original window procedure associated with the window being subclassed. The window handle of the window, along with the entry point of the subclass window procedure, is passed to the WinSubclassWindow() function. The function returns the entry point address of the original window procedure for that window.

Once a window has been subclassed, Presentation Manager routes messages destined for that window to the subclass window procedure. The subclass window procedure may:

  • Process the message itself, if it indicates an action for which the method must be modified.

    The subclass window procedure then returns control immediately to Presentation Manager.

  • Pass the message on to the original window procedure for that window, if the subclass window procedure is not explicitly concerned with the action indicated by the message.

    The original window procedure is directly invoked by the subclass window procedure; note that this is one of the few instances where direct invocation of a window procedure is recommended. The return code from the original window procedure is then returned to Presentation Manager.

  • Both of the above, if the subclass window procedure must perform some processing in addition to that normally performed by the original window procedure.

    The additional processing performed by the subclass window procedure may be performed either before or after the processing performed by the original window procedure. This sequence is at the discretion of the application developer, and depends largely on the desired modification in the window's behavior.

    A subclass window procedure is similar in structure to a "normal" window procedure, except that instead of calling the WinDefWindowProc() function as its default case, it should invoke the original window procedure. This means that the entry point address of the original window procedure must be known to and accessible from the subclass window procedure. Note also that the entry point address might not be that of the original window procedure specified when the window class was registered to Presentation Manager, since the window might previously have been subclassed, and the current subclassing operation might be effectively subclassing the subclass window procedure.

    The entry point address of the original procedure can be supplied to the subclass window procedure in a number of ways:

  • It may be determined from the information returned by the WinSubclassWindow() call, and passed to the subclass window procedure in an application-defined message. The subclass window procedure may then store the entry point address in a global variable or in the window words of the window, assuming the available window words are not already in use.

  • It may be determined by the subclass window procedure itself by querying Presentation Manager. Note, however, that this method will only work if the window has not previously been subclassed, since Presentation Manager only records the original window procedure (as specified in the WinRegisterClass() function call) in the CLASSINFO structure for the window.

    An example of a subclass window procedure, including a query to obtain the original entry point address from the Presentation Manager class information, is given in Figure "Subclass Window Procedure". Figure "Subclass Window Procedure" shows each of the possible cases listed above. The message class WMP_MESSAGE1 is explicitly processed by the subclass window procedure, which then returns control to Presentation Manager with a return statement upon completion.

    The message class WMP_MESSAGE2 is also explicitly processed by the subclass window procedure, but in this case it is required that the processing performed by the original window procedure be allowed to occur, after the subclass window procedure's processing. The subclass window procedure therefore does not return control immediately to Presentation Manager, but merely terminates the switch statement, allowing the final four statements to be executed.

    For other message classes with which the subclass window procedure is not concerned, the default case also terminates the switch statement, allowing the final four statements to be executed.

    These final statements determine the entry point address of the original window procedure, using the WinQueryClassName() and WinQueryClassInfo() functions to access control information held by Presentation Manager. This entry point address is then used to invoke the original window procedure to process messages with which the subclass window procedure is not concerned, or for which the normal processing must be allowed to occur.

    The last four statements in the example above are common to all subclass window procedures, and organizations undertaking development of Presentation Manager applications may wish to incorporate them into a standard subroutine and place them in a library for access by developers.

    Note that a subclass window procedure, like all window and dialog procedures, must use the system linkage convention. This is normally achieved by declaring the subclass window procedure using the EXPENTRY keyword.


    [Back: Instance Data and Window Words]
    [Next: Window Communication]