Trackbar Control Example demo

To create a Trackbar control specify TRACKBAR_CLASS as the window class and register the class by specifying the ICC_BAR_CLASSES bit flag in the accompanying INITCOMMONCONTROLSEX structure.

The TBS_AUTOTICKS style creates a tick mark for each increment in its range of values. Setting other controls as buddy window for the trackbar, means descriptive labels can be placed at either end of the trackbar. When Trackbar slider is moved, the parent window receives the WM_HSCROLL message.

#define _WIN32_WINNT 0x0501 #include <windows.h> #include <commctrl.h> #pragma comment(lib, "comctl32.lib") #define LEFTLABEL 1 #define RIGHTLABEL 2 #define COUNTLABEL 3 #define TRACKBARCTL 4 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM); void CreateControls(HWND); void UpdateLabel(); HWND hTrackBar; HWND hTrackLabel; // Main program int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) { HWND hwnd; MSG msg; WNDCLASS wc; // Register window class wc.style = 0; wc.lpfnWndProc = WndProc; wc.cbClsExtra = 0; wc.cbWndExtra = 0; wc.hInstance = hInstance; wc.hIcon = LoadIcon(NULL, IDI_APPLICATION); wc.hCursor = LoadCursor(NULL, IDC_ARROW); wc.hbrBackground = (HBRUSH)(COLOR_3DFACE + 1); wc.lpszMenuName = NULL; wc.lpszClassName = TEXT("TrackbarClass"); RegisterClass(&wc); // Create main window hwnd = CreateWindow( TEXT("TrackbarClass"), TEXT("Trackbar Control Demo"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 180, NULL, NULL, hInstance, NULL); // Message loop while(GetMessage(&msg,NULL,0,0)) { TranslateMessage(&msg); DispatchMessage(&msg); } return msg.wParam; } // Window procedure LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) { switch(msg) { case WM_CREATE: CreateControls(hwnd); break; // Trackbar sends this when the slider moves case WM_HSCROLL: UpdateLabel(); break; case WM_DESTROY: PostQuitMessage(0); break; default: return DefWindowProc(hwnd,msg,wParam,lParam); } return 0; } // Create controls void CreateControls(HWND hwnd) { INITCOMMONCONTROLSEX icex; // Load trackbar common control icex.dwSize = sizeof(INITCOMMONCONTROLSEX); icex.dwICC = ICC_BAR_CLASSES; InitCommonControlsEx(&icex); // Left value label CreateWindow( TEXT("STATIC"), TEXT("0"), WS_CHILD | WS_VISIBLE, 10, 50, 20, 20, hwnd, (HMENU)LEFTLABEL, NULL, NULL); // Right value label CreateWindow( TEXT("STATIC"), TEXT("100"), WS_CHILD | WS_VISIBLE, 220, 50, 40, 20, hwnd, (HMENU)RIGHTLABEL, NULL, NULL); // Display current value hTrackLabel = CreateWindow( TEXT("STATIC"), TEXT("0"), WS_CHILD | WS_VISIBLE | WS_BORDER, 150, 100, 50, 25, hwnd, (HMENU)COUNTLABEL, NULL, NULL); // Create trackbar hTrackBar = CreateWindow( TRACKBAR_CLASS, TEXT(""), WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS | TBS_HORZ, 40, 45, 170, 40, hwnd, (HMENU)TRACKBARCTL, NULL, NULL); // Set minimum and maximum values SendMessage( hTrackBar, TBM_SETRANGE, TRUE, MAKELONG(0,100)); // Set page movement SendMessage( hTrackBar, TBM_SETPAGESIZE, 0, 10); // Add tick marks every 10 SendMessage( hTrackBar, TBM_SETTICFREQ, 10, 0); // Set starting position SendMessage( hTrackBar, TBM_SETPOS, TRUE, 0); } // Update displayed value void UpdateLabel() { long position; TCHAR buffer[20]; position = SendMessage( hTrackBar, TBM_GETPOS, 0, 0); wsprintf( buffer, TEXT("%ld"), position); SetWindowText( hTrackLabel, buffer); }