Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello
How To Get The Current Position of The Trackbar and How To Call Function When the Position Increase or Decrease ?
thank You

What I have tried:

C#
switch (HIWORD(wParam))

			{
			case WM_HSCROLL:
			{
				HWND hTrackbar = GetDlgItem(hWnd, TrackBar);

				if (hTrackbar == (HWND)lParam)
				{
					int newPos = SendMessage(hTrackbar, TBM_GETPOS, 0, 0);
					int selStart = SendMessage(hTrackbar, TBM_GETSELSTART, 0, 0);
					int selEnd = SendMessage(hTrackbar, TBM_GETSELEND, 0, 0);

					if (newPos > selEnd)
					{
						
						SendMessage(hTrackbar, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)selEnd);
					}

					else if (newPos < selStart)
					{
						
						SendMessage(hTrackbar, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)selStart);
					}
				}

				break;
			}
			}
Posted
Updated 14-Dec-16 21:37pm

1 solution

Inside the handler check the lower word of the WPARAM. It contains the notification code (e.g. TB_LINEDOWN for a step to the right and TB_LINEUP for a step to the left with horizontal bars when using the keyboard). When using the mouse, TB_THUMBTRACK is send while moving with left mouse button down and TB_THUMBPOSITION is send when the mouse button is released. See About Trackbar Controls (Windows)[^].

You already know how to get the current position: By sending the TBM_GETPOS message.

However, with the TB_THUMBTRACK and TB_THUMBPOSITION notifications the position is send in the high word of the WPARAM. See WM_HSCROLL (Trackbar) notification code (Windows)[^].

This will not work as expected:
switch (HIWORD(wParam))
{
    case WM_HSCROLL:
// ...
As explained the above, the high word is the position with the track and position events (and zero with all other track bar scroll events). The switch statement must use the message code instead.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900