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;

}