Creating a Scroll Bar (Windows Embedded CE 6.0)
1/6/2010
A scroll bar is used to scroll through 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 through the content of a window to the left or right. A vertical scroll bar enables a user to scroll through the content up or down.
You can use as many scroll bar controls as you need in a single window. When you create a scroll bar control, you must specify the size and position of the scroll bar. However, if the window that uses a scroll bar control 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 by using the CreateWindow function
Specify the SCROLLBAR window class in the lpClassName parameter of the CreateWindow function or the CreateWindowEx function.
Specify one or more scroll bar control styles in the dwStyle parameter of the CreateWindow function or the 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 always must specify the x-coordinate and the y-coordinate of the position of the scroll bar, as well as the dimensions of the scroll bar. For a complete listing of supported styles, see Window and Control Styles. If the WS_EX_LAYOUTRTL window style is set, the scroll bar control has to be mirrored. The reading order can be also be controlled by WS_EX_RTLREADING window style.
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("scroll bar"), // 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 the parent window
(HMENU) SCROLLBARID,// The control identifier
hInst, // The instance handle
NULL); // Specify NULL for this parameter when
// you create a control.
To create a scroll bar control in a dialog box
Add the following SCROLLBAR resource-definition statement to your DIALOG resource.
SCROLLBAR id, x, y, width, height [[, style [[, extended-style]]]]
Here, id is the value that identifies the scroll bar.
The x and y parameters determine the scroll bar position and are represented as integers. They are relative to the left end or the upper end of the scroll bar, depending on whether the scroll bar is horizontal or vertical. The position must be within the minimum and maximum values of the scrolling range. For example, in a scroll bar with a range from 0 through 100, position 50 is the middle, with the remaining positions distributed equally along the scroll bar. The initial range depends on the scroll bar. Standard scroll bars have an initial range from 0 through 100. Scroll bar controls have an empty range — both minimum and maximum values are zero — unless you supply an explicit range when you create the control. You can alter the range at any time after its initial creation. You can use the SetScrollInfo function to set the range values and the GetScrollInfo function to retrieve the current range values.
The width and height parameters determine size of the scroll bar. You can set a scroll bar equal to a page size. The page size represents the number of data units that can fit in the client area of the owner window, given its current size. For example, if the client area can hold eight lines of text, an application would set the page size to eight. Windows Embedded CE uses the page size, along with the scrolling range and length of the gray area of the scroll bar, to set the size of the scroll bar. When a window that contains a scroll bar is resized, an application should call the SetScrollInfo function to set the page size. An application can retrieve the current page size by calling the GetScrollInfo function.
Both style and extended-style determine the appearance of the edit box. The default style of a scroll bar is SBS_HORZ, which creates a horizontal scroll bar. The following illustration shows a horizontal scroll bar. For a vertical scroll bar, specify the SBS_VERT style. For a complete listing of supported styles, see Window and Control Styles.
The following illustration shows a scroll bar.
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 update automatically the position of the scroll box. Instead, the scroll bar passes the request to the parent window, which must scroll through the data and update the position of the scroll box. Use the SetScrollInfo function in your application to update the position of the scroll box. Because your application controls the movement of the scroll box relative to the window data object, you determine the incremental position settings for the scroll box that work best for the data that is being scrolled.