Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 474
David Avatar asked Dec 21 '25 05:12

David


2 Answers

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.

like image 126
Hans Passant Avatar answered Dec 22 '25 19:12

Hans Passant


What are 1045 and 1044 in your code? Possibly you mean IDC_SLIDER1045 and static control resource ID. If necessary, include resource.h to the source file.

like image 28
Alex F Avatar answered Dec 22 '25 20:12

Alex F