Search code examples
c++winapivisual-c++trackbar

Read Trackbar control value in C++/WinAPI program


I am writing a simple GUI application in Visual C++/Windows API. I have a Trackbar control on the dialogbox defined in resources as:

CONTROL "",IDC_SLIDER1045,"msctls_trackbar32",0x50010000,23,52,141,16,0x00000000

I want to show the trackbar value on static text control, so I wrote:

case WM_NOTIFY:
if(lParam == TRBN_THUMBPOSCHANGING)
{
    Pos1 = SendMessage(GetDlgItem(hwndDlg, 1045), TBM_GETPOS, 0, 0);

    wsprintf(szPos1, "Change IP address every %d minutes", Pos1);

    SetDlgItemText(hwndDlg, 1044, szPos1);
}
break;

I tried also:

case WM_NOTIFY:
    Pos1 = SendMessage(GetDlgItem(hwndDlg, 1045), TBM_GETPOS, 0, 0);

    wsprintf(szPos1, "Change IP address every %d minutes", Pos1);

    SetDlgItemText(hwndDlg, 1044, szPos1);
break;

Both codes doesn't work. First gives no action, second hangs the application.

My question is: How to get Trackbar value and show it on static text control in real time?


Solution

  • Be sure to read the SDK documentation for Trackbar. The section titled "Trackbar Notification Messages" tells you how the control tells you about the position.

    Note how it documents that you should listen for the WM_HSCROLL or WM_VSCROLL message.