Navigation

Scrollbars - Part 7 - Processing the scroll message

In the previous articles in this series (parts 1, 2, 3, 4, 5 and 6) we investigated the scrollbar messages and set-up the scrollbars for our application. In this article we finally make the scrollbars do something for us.

In the Scroll handler we react to each scrollbar operation, and re-position the image:

The first is that because we have chosen a positive range for our reported positions, we have to negate them before using them to position the label control.

The second is that we also need to re-position the control in the
Resize handler: because we change the page size in this handler we may make the current position invalid (remember from the last article in the series the page-size affects the maximum reportable position of the scrollbar). Fortunately the scrollbar implementation takes care of adjusting the current position for us, so we can just retrieve the new current position after we set the page size and use that to position the control.

Finally we end up with what we want - scrollbars that allow us to scroll around the image and see the parts that don't fit into the window.

Note that handling both the SB_THUMBTRACK and SB_THUMBPOSITION operations is not strictly necessary. Typically it is better to handle the SB_THUMBTRACK, as that allows us to move the contents while the user is dragging the thumb; however, if we had a window that took a long time to redraw, then we might chose to only process the SB_THUMBPOSITION operation so that we only need to redraw when the user releases the thumb. Try removing the branches in the above code to see the 2 behaviours.