The window procedure for the scroll bar controls is somewhere inside Windows. However, you can obtain the address of this window procedure by a call to GetWindowLong using the GWL_WNDPROC identifier as a parameter. Moreover, you can set a new window procedure for the scroll bars by calling SetWindowLong. This technique, called ”window subclassing,“ is very powerful. It lets you hook into existing window procedures, process some messages within your own program, and pass all other messages to the old window procedure.
The window procedure that does preliminary scroll bar message processing in COLORS1 is called ScrollProc; it is toward the end of the COLORS1.C listing. Because ScrollProc is a function within COLORS1 that is called by Windows, it must be defined as FAR PASCAL and must be listed under EXPORTS in the COLORS1.DEF module definition file.
First, to ensure that ScrollProc accesses the proper data segment, COLORS1 must obtain a far address for the function using MakeProcInstance:
lpfnScrollProc = MakeProcInstance ((FARPROC) ScrollProc, hInstance);
For each of the three scroll bars, COLORS1 uses GetWindowLong to obtain and save the address of the existing scroll bar window procedure:
lpfnOldScr[n] = (FARPROC) GetWindowLong (hwndScrol[n], GWL_WNDPROC) ;
Next, the program sets the new scroll bar window procedure:
SetWindowLong (hwndScrol[n], GWL_WNDPROC, (LONG) lpfnScrollProc) ;
Now the function ScrollProc gets all messages that Windows sends to the scroll bar window procedure for the three scroll bars in COLORS1 (but not, of course, for scroll bars in other programs). The ScrollProc window procedure simply changes the input focus to the next (or previous) scroll bar when it receives a Tab or Shift-Tab keystroke. It calls the old scroll bar window procedure using CallWindowProc.