Creating Trackbar Control Example

To create a Tab control specify TRACKBAR_CLASS as the window class and register the class by specifying the ICC_LISTVIEW_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.

//trackbar control demo
#include <windows.h>
#include <commctrl.h>
#define LEFTLABEL 1
#define RIGHTLABEL 2
#define COUNTLABEL 3
#define TRACKEBARCTL 4
#pragma comment(lib, "comctl32.lib")
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
void CreateControls(HWND hwnd);
void UpdateLabel(void);

HWND TrackBar;
HWND TrackLabel;

int WINAPI wWinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PWSTR lpCmdLine, int nCmdShow) {

HWND hwnd;
MSG msg ;
WNDCLASS wc = {0};
wc.lpszClassName = TEXT("Trackbar");
wc.hInstance = hInstance;
wc.hbrBackground = GetSysColorBrush(COLOR_3DFACE);
wc.lpfnWndProc = WndProc;
wc.hCursor = LoadCursor(0,IDC_ARROW);

RegisterClass(&wc);
hwnd = CreateWindow(wc.lpszClassName, TEXT("Trackbar control"), WS_OVERLAPPEDWINDOW | WS_VISIBLE, 100, 100, 350, 180, 0, 0, hInstance, 0);

while (GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}

return (int) msg.wParam;
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

switch(msg) {

case WM_CREATE:
CreateControls(hwnd);
break;

case WM_HSCROLL:
UpdateLabel();
break;

case WM_DESTROY:
PostQuitMessage(0);
break;
}

return DefWindowProc(hwnd, msg, wParam, lParam);
}

void CreateControls(HWND hwnd) {
// load common control class ICC_LISTVIEW_CLASSES from the dynamic-link library (DLL).
INITCOMMONCONTROLSEX icex;
icex.dwSize = sizeof(INITCOMMONCONTROLSEX);
icex.dwICC = ICC_LISTVIEW_CLASSES;
InitCommonControlsEx(&icex);

HWND LeftLabel = CreateWindow(TEXT("Static"), TEXT("0"), WS_CHILD | WS_VISIBLE, 0, 0, 10, 30, hwnd, (HMENU)LEFTLABEL, NULL, NULL);
HWND RightLabel = CreateWindow(TEXT("Static"), TEXT("100"), WS_CHILD | WS_VISIBLE, 0, 0, 30, 30, hwnd, (HMENU)RIGHTLABEL, NULL, NULL);
TrackLabel = CreateWindow(TEXT("Static"), TEXT("0"), WS_CHILD | WS_VISIBLE, 270, 50, 30, 30, hwnd, (HMENU)COUNTLABEL, NULL, NULL);
TrackBar = CreateWindow(TRACKBAR_CLASS, TEXT("Trackbar Control"),WS_CHILD | WS_VISIBLE | TBS_AUTOTICKS,40, 50, 170, 30, hwnd, (HMENU)TRACKEBARCTL, NULL, NULL);

//set up trackbar details
SendMessage(TrackBar, TBM_SETRANGE, TRUE, MAKELONG(0, 100));
SendMessage(TrackBar, TBM_SETPAGESIZE, 0, 10);
SendMessage(TrackBar, TBM_SETTICFREQ, 10, 0);
SendMessage(TrackBar, TBM_SETPOS, FALSE, 0);
SendMessage(TrackBar, TBM_SETBUDDY, TRUE, (LPARAM) LeftLabel);
SendMessage(TrackBar, TBM_SETBUDDY, FALSE, (LPARAM) RightLabel);
}

//update label when trackbar is changed
void UpdateLabel(void)
{
LRESULT pos = SendMessage(TrackBar, TBM_GETPOS, 0, 0);
TCHAR buf[4];
wsprintf(buf, TEXT("%ld"), pos);
SetWindowText(TrackLabel, buf);
}