Implementing Scrollbars
Scrollbars can be useful whenever the display rectangle is larger than the window's client area. This is an alternative to making the displayed image fit the window. The following instructions describe how to implement scrollbars, turning them on when they are required and turning them off when they are not.
To be as simple and specific as possible, these instructions describe how to modify the Color example program. If you want to test the sample code, you should make a copy of the Color program, and modify the copy. The display rectangles used in this program are described in Fitting an Image to a Window.
Modify the window style specified in the InitInstance function so that the window can have scrollbars. You do this by adding the WS_VSCROLL and WS_HSCROLL styles. | |
Modify the Color.H file to add global variables that track the scroll range and position, and to add the prototype for a CalcClientMetrics function that aids in calculating scroll ranges and positions. | |
At the end of the Color.CPP file, add the new local function, CalcClientMetrics, which interprets the WM_SIZE message's client area parameters. It is necessary to interpret these parameters because turning on or turning off scrollbars when processing the WM_SIZE message changes the size of the client area. | |
Modify the Color.H file to add some global variables to support scrolling. | |
In the Window_OnCreate function, modify the WM_CREATE message processing so that the display rectangle does not fit the client area. To keep it simple, initialize the display rectangle to be the same size as the bitmap. | |
In the MainWndProc function, add code to process WM_SIZE messages. This code shows or hides the scrollbars, as needed, and it adjusts the scrollbar ranges and positions. | |
In the MainWndProc function, add code to process WM_HSCROLL and WM_VSCROLL messages. | |
In the Window_OnPaint function, change the WM_PAINT message handling code. |
The following topics provide detailed descriptions of these steps, with code that you can copy from the online help and paste into the sample program.