Click to Rate and Give Feedback
MSDN
MSDN Library
User Interface
Windows Controls
Control Library
Trackbar
 Using Trackbar Controls
Using Trackbar Controls

This section provides implementation details and examples for trackbar controls.

Creating a Trackbar

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; 
} 

Processing Trackbar Notification Messages

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; 

        } 
} 

Limiting Slider Movement

As described in About Trackbar Controls, it is possible to set off part of the trackbar range as a selection range. One purpose of a selection range might be to limit movement of the slider, making some parts of the full range off limits.

The following example code, from the window procedure of a dialog box, limits movement of the slider by resetting its position whenever it is moved outside the selection range.

case WM_HSCROLL:
    {
        HWND hTrackbar = GetDlgItem(hDlg, IDC_SLIDER1);
        if (hTrackbar == (HWND)lParam)
        {
            int newPos = SendMessage(hTrackbar, TBM_GETPOS, 0, 0);
            int selStart = SendMessage(hTrackbar, TBM_GETSELSTART, 0, 0);
            int selEnd = SendMessage(hTrackbar, TBM_GETSELEND, 0, 0);
            if (newPos > selEnd)
            {
                SendMessage(hTrackbar, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)selEnd);
            }
            else if (newPos < selStart)
            {
                SendMessage(hTrackbar, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)selStart);
            }
        }
        break;
    }

Using Buddy Windows

By setting other controls as buddy windows for a trackbar, you can automatically position those controls at the ends of the trackbar as labels. The following illustration shows a horizontal and a vertical trackbar, both with static controls as buddy windows.

Trackbars with labels in buddy windows.

The following example function creates the buddy windows shown in the illustration.

// IDC_SLIDER1 and IDC_SLIDER2 are trackbars created in
// the resource editor.

void LabelTrackbarsWithBuddies(HWND hDlg)
{
    const int staticWidth = 50;
    const int staticHeight = 20;

    // Get horizontal trackbar.
    HWND hwndTrackbar = GetDlgItem(hDlg, IDC_SLIDER1);

    // Create buddies.
    HWND hwndBuddy = CreateWindowEx(0, L"STATIC", L"Left", 
        SS_RIGHT | WS_CHILD | WS_VISIBLE, 
        0, 0, 
        staticWidth, staticHeight, 
        hDlg, NULL, g_hInst, NULL);
    SendMessage(hwndTrackbar, TBM_SETBUDDY, 
        (WPARAM)TRUE, (LPARAM)hwndBuddy);

    hwndBuddy = CreateWindowEx(0, L"STATIC", L"Right", 
        SS_LEFT | WS_CHILD | WS_VISIBLE, 
        0, 0, 
        staticWidth, staticHeight, 
        hDlg, NULL, g_hInst, NULL);
    SendMessage(hwndTrackbar, TBM_SETBUDDY, 
        (WPARAM)FALSE, (LPARAM)hwndBuddy);

    // Get vertical trackbar.
    hwndTrackbar = GetDlgItem(hDlg, IDC_SLIDER2);

    // Create buddies.
    hwndBuddy = CreateWindowEx(0, L"STATIC", L"Top", 
        SS_CENTER | WS_CHILD | WS_VISIBLE,
        0, 0, 
        staticWidth, staticHeight, 
        hDlg, NULL, g_hInst, NULL);
    SendMessage(hwndTrackbar, TBM_SETBUDDY,
        (WPARAM)TRUE, (LPARAM)hwndBuddy);

    hwndBuddy = CreateWindowEx(0, L"STATIC", L"Bottom", 
        SS_CENTER | WS_CHILD | WS_VISIBLE,
        0, 0,
        staticWidth, staticHeight, 
        hDlg, NULL, g_hInst, NULL);
    SendMessage(hwndTrackbar, TBM_SETBUDDY,
        (WPARAM)FALSE, (LPARAM)hwndBuddy);
}
Tags What's this?: Add a tag
Community Content   What is Community Content?
Add new content RSS  Annotations
Processing
© 2009 Microsoft Corporation. All rights reserved. Terms of Use | Trademarks | Privacy Statement
Page view tracker