Creating a Scroll Bar

A scroll bar is used to scroll text in a window. Scroll bars should be included in any window for which the content of the client area extends beyond the window borders. The orientation of a scroll bar determines the direction in which scrolling occurs when a user operates the scroll bar. A horizontal scroll bar enables a user to scroll the content of a window to the left or right. A vertical scroll bar enables a user to scroll the content up or down.

You can use as many scroll bar controls as needed in a single window. When you create a scroll bar control, you must specify the size and position of the scroll bar. However, if a scroll bar control's window can be resized, your application must adjust the size of the scroll bar when the size of the window changes.

    To create a scroll bar using CreateWindow

  1. Specify the SCROLLBAR window class in the lpClassName parameter of the CreateWindow or CreateWindowEx function.
  2. Specify one or more scroll bar control styles in the dwStyle parameter of the CreateWindow or CreateWindowEx function.

    A scroll bar control can have a number of styles to control the orientation and position of the scroll bar. Some of the styles create a scroll bar control that uses a default width or height. However, you must always specify the x and y coordinates and the scroll bar dimensions. For a complete listing of supported styles, see Window and Control Styles.

The following code example shows how to use CreateWindow to create a scroll bar.

#define SCROLLBARID 100

DWORD dwStyle = SBS_BOTTOMALIGN | SBS_HORZ | WS_VISIBLE | WS_CHILD;

hwndSB = CreateWindow (
            TEXT("scrollbar"),  // Class name
            NULL,               // Window text
            dwStyle,            // Window style
            0,                  // x coordinate of the upper-left corner
            0,                  // y coordinate of the upper-left corner
            CW_USEDEFAULT,      // The width of the edit control window
            CW_USEDEFAULT,      // The height of the edit control window
            hwnd,               // Window handle to parent window
            (HMENU) SCROLLBARID,// The control identifier
            hInst,              // The instance handle
            NULL);              // Specify NULL for this parameter when 
                                // creating a control

    To create a scroll bar control in a dialog box

To establish a useful relationship between the scroll bar range and the data object, your application must adjust the range when the size of the data object changes.

As a user moves the scroll box in a scroll bar, the scroll bar reports the scroll box position as an integer in the scrolling range. If the position is the minimum value, the scroll box is at the top of a vertical scroll bar or at the left of a horizontal scroll bar. If the position is the maximum value, the scroll box is at the bottom of a vertical scroll bar or at the right end of a horizontal scroll bar.

Your application must move the scroll box in a scroll bar. Although a user makes a request for scrolling in a scroll bar, the scroll bar does not automatically update the scroll box position. Rather, it passes the request to the parent window, which must scroll the data and update the scroll box position. Use the SetScrollInfo function in your application to update the scroll box position. Because your application controls the scroll box movement relative to the window data object, you determine the incremental position settings for the scroll box that work best for the data being scrolled.