Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 9

Introduction

A scroll bar control is a rectangular window that contains a scroll box


(usually called the thumb of the scroll bar) and two scroll arrows. The scroll
bar control sends a notification message to its parent window whenever the
user clicks the control with the mouse. The parent window is responsible for
updating the parent window's contents and the position of the thumb (if
necessary).

A scroll bar is part of a window, but a scroll bar control is actually a window
itself. Unlike a scroll bar, a scroll bar control can be positioned anywhere in
a window and used whenever the window needs to scroll input.

Anatomy of a Scroll Bar


A scroll bar consists of a shaded shaft with a scroll arrow at each end and a
scroll box (usually called a thumb) between the arrows (see Figure 1).

Figure 1. The anatomy of a scroll bar

A scroll bar represents the full range (overall length or width) of a data
object such as a document or picture in the window's client area. The
thumb represents the portion of the data object that is visible in the client
area. The user can scroll the contents of the window by clicking one of the
scroll arrows, by clicking the area in the shaded shaft, or by dragging the
thumb. For example, when the user clicks the up arrow, the thumb moves
up and the application scrolls the contents of the window by one unit
(typically a single line or column). When the user clicks inside the shaft, the
application scrolls the contents by one window. The amount of scrolling
that occurs when the user drags the thumb is known as the scrolling
range and is defined by the application developer.

Scroll Bar Styles


This section describes the styles that you can use when creating your scroll
bar. These styles are also documented in the Platform SDK.

Style Functionality

SBS_BOTTOMALIGN Places a horizontal scroll bar at the bottom of the rectangle speci
scroll bars. This style is used with the SBS_HORZ style.

SBS_HORZ Creates a horizontal scroll bar. The scroll bar is sized accordi
SBS_TOPALIGN style is specified.

SBS_LEFTALIGN Places a vertical scroll bar at the left edge of the rectangle specified
bars. This style is used with the SBS_VERT style.

SBS_RIGHTALIGN Places a vertical scroll bar at the right edge of the rectangle speci
scroll bars. This style is used with the SBS_VERT style.

SBS_SIZEBOX Creates a size box. (A size box is a small rectangle that is used to s
this article for more information.) If you don't specify the SBS_SI
the height, width, and position specified by the CreateWindow rec

SBS_SIZEBOXBOTTOMRIGHTALIG Places the size box with the lower-right corner of the rectangle spec
N boxes. This style is used only with the SBS_SIZEBOX style.

SBS_SIZEBOXTOPLEFTALIGN Places the size box with the upper-left corner of the rectangle spec
boxes. This style is used only with the SBS_SIZEBOX style.

SBS_TOPALIGN Puts a horizontal scroll bar at the top edge of the rectangle defined
bars. This style is used with the SBS_HORZ style.
SBS_VERT Creates a vertical scroll bar. If don't you specify the SBS
the CreateWindow rectangle parameters.

Scroll Bar Techniques


Adding a Scroll Bar to a Window

If you want to allow users to scroll the contents of your window, you can
add horizontal and vertical scroll bars to the window simply by adding the
WS_HSCROLL and WS_VSCROLL styles to your window in your call
to CreateWindow. This creates the look of a "traditional" window.

Copy
hWnd = CreateWindow(
"TypicalWindow",
"Test",
WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
CW_USEDEFAULT, CW_USEDEFAULT, 300, 150,
NULL,
NULL,
hInstance,
NULL);

Scrolling Text in a Window

When you add scroll bar(s) to your window, Windows notifies you
whenever the user clicks the scroll bar. The application responds to these
notification messages by redrawing the contents of the window that need
to be redrawn. One common method of scrolling a window's contents is to
call the ScrollWindow orScrollWindowEx function with a parameter
specifying the rectangular area that needs to be scrolled and drawing only
the area that contains new information.
A typical method used to scroll text in a window involves the following
steps:

1. Get the current text metrics for the window, and calculate how many
lines of code will fit on the screen.
2. Set the vertical scroll range and the scroll position based on the
number of additional lines you need to display. Use the text metric for
average character height in this calculation.
3. Set the horizontal scroll range and scroll position based on the
amount of text available for horizontal viewing. Use the text metric for
average character width in this calculation.
4. Once a scroll notification is given, scroll the window using
the ScrollWindow function. Note that ScrollWindow invalidates part
of the window for repainting. An example of this is shown in the code
below. For a full code listing of this scrolling technique, see the
Platform SDK documentation. In the code listing below, xInc is the
increment amount, xPos is the current scroll position, and xMax is the
maximum horizontal scrolling position.

Copy

/*
* Increment the scrolling position, adjust the position
* of the thumb, and update the window.
*/
if (xInc = max (-xPos, min (xInc, xMax - xPos))) {
xPos += xInc;
ScrollWindow (hwnd, -xChar * xInc, 0,
(CONST RECT *) NULL, (CONST RECT *) NULL);
SetScrollPos (hwnd, SB_HORZ, xPos, TRUE);
UpdateWindow (hwnd);
}

5. Draw the text out to the screen using the current vertical scrolling
position and the coordinates of the invalid rectangle to determine the
range of new lines that should be drawn.
6. In the case of a horizontal or vertical scroll notification, reset the scroll
position based on an appropriate increment. For example, if the user
clicks the right scroll arrow, increment your scroll position by one unit.
If the user drags the thumb to the end of the scroll bar, calculate the
end point and set your position to that point.

Scroll Bar Functions


Unlike other window classes, scroll bars are manipulated through functions.
These functions are described in the sections below.

The first parameter to each function is the handle to the scroll bar control
(or the handle to the owner window, if the scroll bar was created with the
WS_HORZ or WS_VERT window style). The second parameter specifies the
type of scroll bar to manipulate:

 SB_CTL: A scroll bar control


 SB_HORZ: The horizontal scroll bar of the specified window
 SB_VERT: The vertical scroll bar of the specified window
 SB_BOTH: The horizontal and vertical scroll bars of the specified
window

All scroll bar functions return an error in the following cases:

 The second parameter is SB_CTL, but the first parameter is not a valid
handle to a scroll bar control.
 The second parameter is SB_HORZ, SB_VERT, or SB_BOTH, but the
first parameter points to a window that does not have a scroll bar
child window.

The following function sets the scroll position of the horizontal scroll bar
control to 100 and forces a redraw of the scroll bar control:

Copy
Err = SetScrollPos(hWndHorzScroll, SB_CTL, 100, TRUE);

EnableScrollBar

The EnableScrollBar function enables or disables one or both of the scroll


arrows. If you specify both vertical and horizontal scroll bars, the system
changes the horizontal scroll bar first, changes the vertical scroll bar next,
and then forces a redraw of the window. This function supports the SB_CTL,
SB_HORZ, SB_VERT, and SB_BOTH flags.

GetScrollPos

The GetScrollPos function returns the current position of the thumb in the


specified scroll bar. If you specify a scroll bar control, GetScrollPos sends
an SBM_GETPOS message to the control. Otherwise, it returns the thumb
position that it keeps internally. This function supports the SB_CTL,
SB_HORZ, and SB_VERT flags.

GetScrollRange

The GetScrollRange function returns the current range (minimum and


maximum scroll position) of the scroll bar previously set
with SetScrollRange. If you specify a scroll bar
control, GetScrollRange sends an SBM_GETRANGE message to the control.
Otherwise, it returns the minimum and maximum values that it keeps
internally for the scroll bar. If the window handle you pass to the function is
not a handle to a scroll bar, or if the window specified does not have scroll
bars, GetScrollRangereturns an error and sets the minimum and maximum
values that you pass to the function to 0. This function supports the SB_CTL,
SB_HORZ, and SB_VERT flags.

SetScrollPos

The SetScrollPos function sets the position of the thumb in the scroll bar


to the specified value. If you specify a scroll bar control, SetScrollPos sends
an SBM_SETPOS to the control. Otherwise, the system resets the thumb to
the specified position and redraws the scroll bar and thumb. If you specify a
position outside the current scroll range, SetScrollPos returns an error. This
function supports the SB_CTL, SB_HORZ, and SB_VERT flags.

SetScrollRange

The SetScrollRange function sets the range (minimum and maximum scroll


position) of the scroll bar. If you specify a scroll bar
control, SetScrollRange sends an SBM_SETRANGE to the control.
Otherwise, the system sets the range to the specified minimum and
maximum values. If you specify invalid values or pass an invalid window
handle to the function, SetScrollRange returns an error. This function
supports the SB_CTL, SB_HORZ, and SB_VERT flags.

ShowScrollBar

The ShowScrollBar function shows or hides the specified scroll bar. You


can use this function to hide or show a horizontal scroll bar, a vertical scroll
bar, both horizontal and vertical scroll bars, or a scroll bar control. If you
hide a scroll bar and then show it, you do not have to reset the thumb
position and range because the window handle is still valid. This function
supports the SB_CTL, SB_HORZ, SB_VERT, and SB_BOTH flags.

Interfacing with the Keyboard


The ability to manipulate the scroll bar with the keyboard instead of the
mouse is not a built-in function of the scroll bar class. However,
implementing scrolling behavior based on keyboard input is a fairly trivial
task. One interesting problem associated with this task is determining what
to do in the case of the PAGE_UP, PAGE_DOWN, HOME, and END keys. In
the example below, I have associated PAGE_UP and PAGE_DOWN with the
vertical scroll bar, and HOME and END with the horizontal scroll bar.
Copy
case WM_KEYDOWN:
switch (wParam)
{
case VK_UP:
wScrollNotify = SB_LINEUP;
uMessage = WM_VSCROLL;
break;

case VK_PRIOR: //PAGEUP key


wScrollNotify = SB_PAGEUP;
uMessage = WM_VSCROLL;
break;

case VK_NEXT: // PAGEDOWN key


wScrollNotify = SB_PAGEDOWN;
uMessage = WM_VSCROLL;
break;

case VK_DOWN:
wScrollNotify = SB_LINEDOWN;
uMessage = WM_VSCROLL;
break;

case VK_HOME:
wScrollNotify = SB_BOTTOM;
uMessage = WM_HSCROLL;
break;

case VK_END:
wScrollNotify = SB_TOP;
uMessage = WM_HSCROLL;
break;

case VK_RIGHT:
wScrollNotify = SB_LINEDOWN;
uMessage = WM_HSCROLL;
break;

case VK_LEFT:
wScrollNotify = SB_LINEUP;
uMessage = WM_HSCROLL;
break;

default:
wScrollNotify = 0xFFFF;
break;
}
if (wScrollNotify != -1)
SendMessage(hWnd, uMessage, MAKELONG(wScrollNotify, 0),
0L);
break;

You might also like