Trackbar control in Visual C++

Started by
0 comments, last by adeyblue 12 years, 9 months ago
Hi, I am trying to get the logical value for the thumbtrack position, but the value won't update.
I've looked at win32 api tutorials on it and some other threads with similar problems, but the value just doesn't seem to update (ie. stays at 0).
This is what I've tried so far:



HWND xSlider = NULL;
HWND xSliderVal = NULL;

double xSliderVal;

std::string to_string(double value) {
std::stringstream sstr;
sstr<<value;
std::string s = sstr.str();
return s;
}

LRESULT WndProc (HWND hWnd, UNIT uMsg, WPARAM wParam, LPARAM lParam)
{

switch (uMsg)
{
case WM_HSCROLL:
switch(wParam)
{
case TB_THUMBTRACK:
xSlideVal = SendMessage(xSlider, TBM_GETPOS, 0,0);
SetWindowText(GetDlgItem(hWnd, IDC_X_SLIDER_VAL), to_string(xSlideVal).c_str());
}
break;

default:
return DefWindowProc(hWnd,uMsg,wParam,lParam);
}
}

void CreateControls(HWND hWnd, HINSTANCE hInstance)
{
if((xSliderVal = CreateWindow ( WC_STATIC,
"0",
WS_CHILD|WS_VISIBLE|WS_TABSTOP,
400,400,70,23,
hWnd,(HMENU)IDC_X_SLIDER_VAL,
hInstance, NULL))==NULL)
{
throw std::exception("Failed to create controls");
}
if((xSlider = CreateWindow ( TRACKBAR_CLASS,
"",
WS_CHILD|WS_VISIBLE|TBS_HORZ,
40,400,350,23,
hWnd,(HMENU)IDC_X_SLIDER,
hInstance, NULL))==NULL)
{
throw std::exception("Failed to create controls");
}
SendMessage(xSlider,TBM_SETRANGEMAX,true, 1000);
SendMessage(xSlider,TBM_SETRANGEMIN,true, -1000);
}


Any idea on what I'm doing wrong? Thanks.
Advertisement
Your WM_HSCROLL switch is faulty:

http://msdn.microsoft.com/en-us/library/bb760149.aspx#tkb_notifications
The low-order word of the wParam parameter of WM_HSCROLL or WM_VSCROLL contains the notification code. For the TB_THUMBPOSITION and TB_THUMBTRACK notification codes, the high-order word of the wParam parameter specifies the position of the slider.
[/quote]
You want to be switching on LOWORD(wParam) instead.

This topic is closed to new replies.

Advertisement