How to Process Trackbar Notification Messages

Trackbars notify their parent window of user actions by sending the parent a WM_HSCROLL or WM_VSCROLL message.

What you need to know

Technologies

Prerequisites

  • C/C++
  • Windows User Interface Programming

Instructions

Process Trackbar Notification Messages

The following code example is a function that is called when the trackbar's parent window receives a WM_HSCROLL message. The trackbar in this example has the TBS_ENABLESELRANGE style. The position of the slider is compared to the selection range, and the slider is moved to the starting or ending position of the selection range when necessary.

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

Remarks

A dialog box that contains a TBS_VERT style trackbar can use this function when it receives a WM_VSCROLL message.

Using Trackbar Controls