A trackbar is a window that contains a slider and optional tick marks. When the user moves the slider, using either the mouse or the direction keys, the trackbar sends notification messages to indicate the change.
Trackbars are useful when you want the user to select a discrete value or a set of consecutive values in a range. For example, you might use a trackbar to allow the user to set the repeat rate of the keyboard by moving the slider to a given tick mark. The following illustration shows a typical trackbar.
![]()
The slider in a trackbar moves in increments that you specify when you create it. The values in this range are referred to as logical units. For example, if you specify that the trackbar should have logical units that range from zero to five, the slider can occupy only six positions: a position at the left side of the trackbar and one position for each increment in the range. Typically, each of these positions is identified by a tick mark.
You create a trackbar by using the
The logical units of a trackbar are the set of contiguous values that the
trackbar can represent. They are usually defined by specifying the range of
possible values with a
An application can send messages to the trackbar to retrieve information
about the window and to change its characteristics. To retrieve the position of
the slider (that is, the value the user has chosen), use the
A trackbar automatically displays tick marks at its beginning and end, unless
you specify the
To set the position of a single tick mark, send the
A trackbar's line size determines how far the slider moves in response to
keyboard input from the arrow keys, such as the RIGHT ARROW or DOWN ARROW key.
To retrieve or set the line size, send the
A trackbar's page size determines how far the slider moves in response to
keyboard input, such as the PAGE UP or PAGE DOWN key, or mouse input, such as
clicks in the trackbar channel. To retrieve or set the page size, send the
An application can send messages to retrieve the dimensions of a trackbar.
The
If you create a trackbar with the
You retrieve or set the selection range by sending messages to the trackbar.
Use the
A trackbar notifies its parent window of user actions by sending the parent a
The system sends the TB_BOTTOM, TB_LINEDOWN, TB_LINEUP, and TB_TOP notification messages only when the user interacts with a trackbar by using the keyboard. The TB_THUMBPOSITION and TB_THUMBTRACK notification messages are only sent when the user is using the mouse. The TB_ENDTRACK, TB_PAGEDOWN, and TB_PAGEUP notification messages are sent in both cases. The following table lists the trackbar notification messages and the events (virtual key codes or mouse events) that cause the notifications to be sent.
| Notification message | Reason sent |
|---|---|
| TB_BOTTOM | |
| TB_ENDTRACK | |
| TB_LINEDOWN | VK_RIGHT or VK_DOWN |
| TB_LINEUP | VK_LEFT or VK_UP |
| TB_PAGEDOWN | VK_NEXT (the user clicked the channel below or to the right of the slider) |
| TB_PAGEUP | VK_PRIOR (the user clicked the channel above or to the left of the slider) |
| TB_THUMBPOSITION | |
| TB_THUMBTRACK | Slider movement (the user dragged the slider) |
| TB_TOP | VK_HOME |
This section describes the window message processing performed by a trackbar.
| Message | Processing performed |
|---|---|
| Kills the timer if one was set during |
|
| Performs additional initialization, such as setting the line size, page size, and tick mark frequency to default values. | |
| Frees resources. | |
| Repaints the trackbar window. | |
| Erases the window background, using the current background color for the trackbar. | |
| Returns the DLGC_WANTARROWS value. | |
| Processes the direction keys and sends the TB_TOP, TB_BOTTOM, TB_PAGEUP, TB_PAGEDOWN, TB_LINEUP, and TB_LINEDOWN notification messages, as appropriate. | |
| Sends the TB_ENDTRACK notification message if the key was one of the direction keys. | |
| Repaints the trackbar window. | |
| Sets the focus and the mouse capture to the trackbar. When necessary, it sets a timer that determines how quickly the slider moves toward the mouse cursor when the user holds down the mouse button in the window. | |
| Releases the mouse capture and kills the timer if one was set during WM_LBUTTONDOWN processing. It sends the TB_THUMBPOSITION notification message, if necessary. It always sends the TB_ENDTRACK notification message. | |
| Moves the slider and sends the TB_THUMBTRACK notification
message when tracking the mouse (see |
|
| Paints the trackbar. If the wParam parameter is non-NULL, the control assumes that the value is an HDC and paints using that device context. | |
| Repaints the trackbar window. | |
| Sets the dimensions of the trackbar, removing the slider if there is not enough room to display it. | |
| Retrieves the mouse position and updates the position of the slider. (It is received only when the user is dragging the slider.) | |
| Initializes slider dimensions. |
The following example shows how to create a trackbar with the TBS_AUTOTICKS and TBS_ENABLESELRANGE styles. When the trackbar is created, both its range and its selection range are initialized. The page size is also set at this time.
// CreateTrackbar - creates and initializes a trackbar.
//
// Global variable
// g_hinst - instance handle
HWND WINAPI CreateTrackbar(
HWND hwndDlg, // handle of dialog box (parent window)
UINT iMin, // minimum value in trackbar range
UINT iMax, // maximum value in trackbar range
UINT iSelMin, // minimum value in trackbar selection
UINT iSelMax) // maximum value in trackbar selection
{
InitCommonControls(); // loads common control's DLL
hwndTrack = CreateWindowEx(
0, // no extended styles
TRACKBAR_CLASS, // class name
"Trackbar Control", // title (caption)
WS_CHILD | WS_VISIBLE |
TBS_AUTOTICKS | TBS_ENABLESELRANGE, // style
10, 10, // position
200, 30, // size
hwndDlg, // parent window
ID_TRACKBAR, // control identifier
g_hinst, // instance
NULL // no WM_CREATE parameter
);
SendMessage(hwndTrack, TBM_SETRANGE,
(WPARAM) TRUE, // redraw flag
(LPARAM) MAKELONG(iMin, iMax)); // min. & max. positions
SendMessage(hwndTrack, TBM_SETPAGESIZE,
0, (LPARAM) 4); // new page size
SendMessage(hwndTrack, TBM_SETSEL,
(WPARAM) FALSE, // redraw flag
(LPARAM) MAKELONG(iSelMin, iSelMax);
SendMessage(hwndTrack, TBM_SETPOS,
(WPARAM) TRUE, // redraw flag
(LPARAM) iSelMin);
SetFocus(hwndTrack);
return hwndTrack;
}
The following example is a function that is called whenever a WM_HSCROLL message is received by the dialog box containing the trackbar. The trackbar has the TBS_ENABLESELRANGE style. The position of the slider is compared against the selection range, and the slider is moved to the starting or ending position of the selection range, when necessary.
A dialog containing a trackbar with the TBS_VERT style could use this function when it receives a WM_VSCROLL message.
// TBNotifications - handles trackbar notifications received
// in the wParam parameter of WM_HSCROLL. This function simply
// ensures that the slider remains within the selection range.
VOID WINAPI TBNotifications(
WPARAM wParam, // wParam of WM_HSCROLL message
HWND hwndTrack, // handle of trackbar window
UINT iSelMin, // minimum value of trackbar selection
UINT iSelMax) // maximum value of trackbar selection
{
DWORD dwPos; // current position of slider
switch (LOWORD(wParam)) {
case TB_ENDTRACK:
dwPos = SendMessage(hwndTrack, TBM_GETPOS, 0, 0);
if (dwPos > iSelMax)
SendMessage(hwndTrack, TBM_SETPOS,
(WPARAM) TRUE, // redraw flag
(LPARAM) iSelMax);
else if (dwPos < iSelMin)
SendMessage(hwndTrack, TBM_SETPOS,
(WPARAM) TRUE, // redraw flag
(LPARAM) iSelMin);
break;
default:
break;
}
}
Trackbar controls in Microsoft® Internet Explorer support the following new features.
| Buddy Windows | Trackbar controls now provide support for up to two buddy windows.
Trackbar buddy windows are automatically positioned by the control to
appear centered at the ends of the trackbar control. To assign an
existing window to a trackbar, use the |
| ToolTips | Trackbar controls now support ToolTips. A trackbar creates a default
ToolTip control when created with the |