Understanding Windows Forms AutoScroll
Windows Forms Scrollable Control based objects have the ability to manage scroll bars and scroll positions for you in a clean and consistent manner. Auto-scroll will correctly provide scroll bars in two circumstances, either when you specifically set a minimum size for the client area, below which the scroll-bars will be shown or if controls are contained within the control but outside of the currently viewable client area.
When AutoScroll is enabled, the client area size is compared against the setting in the AutoScrollMinSize property or the bounding rectangle of the accumulated sizes and positions of it's controls whenever it's resized. Scrollbars with the correct settings are displayed and the user can scroll around the client area as required. Figure 1 illustrates the relationship of the AutoScrollMinSize property to the appearance of scroll bars.
Figure 1. The AutoScrollMinSize property.
In figure 1, the area covered by AutoScrollMinSize is filled with a pattern. While the client area is larger than this the scrollbars will not display (A). When the client area is reduced in size so that one or other extent is smaller than the minimum allowable size a scrollbar will appear (B). Likewise, when the client area is smaller in both directions then both scrollbars will appear (C).
Figure 2 shows a form that has a control, in this case a button, which is outside of the visible client area. The scroll settings will be adjusted to take the position and size of the control into consideration when displaying the scroll bars.
In the case of AutoScroll that occurs in response to the positions and sizes of controls on a form, no more work needs to be done because the scroll handlers automatically update the child windows so that they remain in sync with the scroll bars. When drawing graphics however you need to make allowances for the positions of the scrollbars otherwise the graphics will always appear in the same place and won't appear to scroll at-all when the scroll bars are moved.
The AutoScrollMargin property is used to provide an added "bufffer zone" around controls which is taken into account when buttons, combo-boxes and other controls are outside of the window area. The AutoScrollMargin will force the form or container to scroll to a specific distance past the extents of the furthest control.
Stay in sync.
The scroll bar positions are reported in the AutoScrollPosition property that provides an X and Y coordinate offset for your drawing commands to take into account. Note that the offsets are negative so that when the offset is added to the location of the object you're drawing, the final coordinate will be shifted left or up accordingly.
To draw an ellipse on a scrolled client area you might use the following code in the Paint event handler:
Drawing every object with an offset can be laborious and doesn't make for readable code, especially in a graphics intensive application. A simpler way to ensure that your scroll bars affect all the graphics at the same time is to use the graphics transform to adjust the output of the pixels to the correct place and then do your drawing. This can be accomplished using the following code in the Paint event handler:
Matrix mx=new Matrix(1, 0, 0, 1, AutoScrollPosition.X, AutoScrollPosition.Y);
//Do your drawing here...
Dim mx as new Matrix(1, 0, 0, 1, AutoScrollPosition.X, AutoScrollPosition.Y)
Copyright Robert W. Powell 2004. All rights reserved.