CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    Feb 2013
    Posts
    2

    Trackbar Position OnClick

    Hello,
    I'd just like to take a few moments to say how surprised I was to find a "semi"-active forum regarding WinAPI, I got interested in it by reading theForger's WinProg tutorial but I was left stranded when I needed "immediate" support, they reference the EFNET's #Winprog but whenever I logged on with a problem I was greeted with everlasting silence, or they would just completely ignore me and proceed with their own subjects. It was really frustrating.
    There's a lot of content out there but pretty much all of it is dated and I can't tell you how many times I've found broken links to MSDN that left me dead in the water.

    But enough ranting, on with the problem.

    It's pretty simple, whenever the user clicks the trackbar I want the Thumb(value) to be positioned at that point, an example of this at work would be any MediaPlayer out there, VLC / MPC / WMP /etc...
    In this topic the user Kevin Spencer suggests an approach but from what I've seen the results aren't that great.

    The problem is that the Thumb only changes position reliably in the interval [ 40%, 70%], after that it starts deviating severely, 0-40 the offset is smaller than it is on 70-100, by offset I mean the distance between the cursor and the Thumb.
    I've also noticed that if I click the area 0-3% the Thumb sets it self at 3%, and 96-100 to 96%.

    This is clearly a problem with the equation, I've implemented Kevin Spencer's suggestion as such:
    Code:
    		case WM_HSCROLL:
    		{
    			POINT curPos = {0, 0};
    			RECT rc;
    			int totalVal	=	0;
    			int totalPx		=	0;
    			int mousePx		=	0;
    			float fraction	=	0;
    			int mouseVal	=	0;
    
    			char test[50] = "";
    
    			if( LOWORD(wParam) == TB_PAGEUP | TB_PAGEDOWN)
    			{
    				GetCursorPos(&curPos);
    				GetWindowRect((HWND)lParam, &rc);
    
    				totalVal	=	SendMessage( (HWND)lParam, TBM_GETRANGEMAX, 0, 0) - SendMessage( (HWND)lParam, TBM_GETRANGEMIN, 0, 0);
    
    				totalPx		=	rc.right - rc.left;
    
    				mousePx		=	curPos.x - rc.left;
    
    				fraction	=	(float)mousePx / totalPx;
    
    				mouseVal	=	fraction * totalVal;
    
    				SendMessage((HWND)lParam, TBM_SETPOS, (WPARAM)TRUE, (LPARAM)mouseVal);
    
    
    			}
    
    		}
    		break;
    It seems to me that part of the problem is that the equation is percentage based, and that is always going to have an error margin, but it shouldn't be this large IMO.

    I got nowhere with my own approaches, closest I've come wields the same results as the Spencer's algorithm.

    Furthermore the action of "dragging the Thumb" is getting captured in this as well, even though it shouldn't, I haven't yet figured out how to filter all the actions but clicking a point on the Trackbar and dragging the thumb.

    I welcome all replies.


    One last thing, is anyone aware of a good real time support line for WinAPI i.e. IRC channel that isn't dead?
    I hope I don't get misinterpreted as a "leecher", I'd gladly participate in a community, I just haven't found an active one yet.

    Thank you for your time.

  2. #2
    Join Date
    Nov 2000
    Location
    Voronezh, Russia
    Posts
    6,620

    Re: Trackbar Position OnClick

    The code is MFC, but it should be easily understood
    Code:
    void CSliderCtrlEx::OnLButtonDown(UINT nFlags, CPoint point)
    {
    	CRect rc, trc;
    	GetChannelRect(rc);
    	GetThumbRect(trc);
    	rc.InflateRect(0, (trc.Height() - rc.Height())/2);
    
    	if (!PtInRect(rc, point))
    		return;
    
    	LONG range = GetRangeMax();
    	LONG pos = point.x - rc.left - trc.Width()/2;
    	LONG width = rc.Width() - trc.Width();
    	SetPos(int(DOUBLE(pos) * range / width + 0.5));
    
    	CSliderCtrl::OnLButtonDown(nFlags, point);
    }
    TBM_GETCHANNELRECT message is the key.
    Best regards,
    Igor

  3. #3
    Join Date
    Feb 2013
    Posts
    2

    Re: Trackbar Position OnClick

    Thank you very much it works like a charm.

    I had previously used the ChannelRect but I didn't notice any improvements, the problem was with my position calculation, yours is the accurate one.

  4. #4
    Join Date
    Jan 2011
    Posts
    4

    Re: Trackbar Position OnClick

    I'm sure I can translate this to VB.net, but I'm using a Vertical Trackbar. Would swapping out Height and Width work?

  5. #5
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,490

    Re: Trackbar Position OnClick

    Quote Originally Posted by Amerigo777 View Post
    I'm sure I can translate this to VB.net, but I'm using a Vertical Trackbar. Would swapping out Height and Width work?
    Why don't you try it and let us know?

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured