Professional Documents
Culture Documents
Figure 1. The Anatomy of A Scroll Bar
Figure 1. The Anatomy of A Scroll Bar
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.
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.
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.
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);
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.
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:
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
GetScrollPos
GetScrollRange
SetScrollPos
SetScrollRange
ShowScrollBar
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;