Monday 28 November 2011

What is window subclassing? in C programming

What is window sub classing?

Windows subclassing refers to a technique whereby you can “tap” into a built-in Windows function and add your own functionality without disturbing or abandoning the original Windows functionality. For example, the Windows procedure for a check box control is coded deep within the system internals of Windows, and the source code is not readily available. However, through the use of two Windows API functions, you can tap into this code and build your own functionality into it.

The two Windows API functions that accomplish this task are called GetWindowLong() and SetWindowLong(). The GetWindowLong() function returns the address of the Windows procedure you want to subclass. The SetWindowLong() function can be used to override the default Windows procedure and point to your own custom-made version. Note that you do not have to replicate all functionality by doing this—when you need to reference the original procedure’s functionality, you can pass the messages through to it.

You can save the old procedure’s address by including the following code in your program: 

lpfnOldCheckBoxProc = (FARPROC) GetWindowLong(hwndCheckBox, GWL_WNDPROC); 

Your new custom check box procedure can replace the old procedure by including the following code in your program:

SetWindowLong(hwndCheckBox, GWL_WNDPROC, (LONG) lpfnCustomCheckBoxProc);

In this example, the GetWindowLong() function is used to save the old procedure’s address. The GWL_WNDPROC identifier tells the GetWindowLong() function to return a pointer to the check box’s procedure. After this is saved, a new procedure (named lpfnCustomCheckBoxProc) is invoked by a call to the SetWindowLong() function. Now, whenever Windows would normally call hwndCheckBox’s default procedure, your custom check box procedure will be called instead.

In your custom check box procedure, you can always pass messages through to the original procedure. This
is done by using the Windows API function CallWindowProc() as shown here:

CallWindowProc(lpfnOldCheckBoxProc, hwnd, message, wParam, lParam);

This way, you do not have to replicate all functionality that was in the original procedure. Instead, you can trap for only the messages you want to customize. For instance, if you have ever seen a variation of a check box (such as Borland’s custom check box), you know that it does not look like the default check box you would normally see in Windows programs. This variation is accomplished through Windows subclassing.The WM_PAINT message is simply subclassed out, and a new customized version of the check box rather than the original version is painted.

Cross Reference:
XXI.27: What is a static child window?

1 comment:

  1. Harrah's Cherokee Casino & Hotel - Mapyro
    › hotels › harrahs-chero › 광주 출장마사지 hotels › harrahs-chero 김해 출장샵 The Harrah's 강원도 출장샵 Cherokee Casino & Hotel has a number of amenities including a casino, an outdoor pool, a seasonal outdoor pool and a 양산 출장샵 seasonal outdoor 안성 출장샵 pool.

    ReplyDelete