How do I get a trackbar to move to a click position (like Media Player)?

M

mmdst23

I'm working on a video player app, and I want to implement a trackbar
that behaves like the one in Media Player, that is the cursor position
moves to where the user clicked instead of moving in that direction by
a fixed value. Has anyone implemented something like this before? I
haven't found anything on codeproject, or searching the .NET
newsgroups. This seems like it would be a common control type, am I
missing the easy solution?

I have been trying to make one by overridding the mouseUp event (so
that it can be either clicked or dragged) but I can't get the cursor to
move to the correct position, and the difference between the click and
where the cursor gets moved changes depending on where in the trackbar
I click.

I'm manaully building the DShow player graph in a DLL, and the UI is in
a VB app. I need full control over the graph and playback, so I don't
think I can use the Media Player object.

Thanks,
Mike
 
K

Kevin Spencer

Hi Mike,

The TrackBar has a Minimum, a Maximum, and a Value property. It also has a
slot in which the slider moves. The Minimum value is the value when the
slider is at the far left side of the slot. The Maximum value is the value
when it is at the far right side of the slot. Now, let's say for the sake of
simplicity that the Minimum is 0 and the Maximum is 100. What will the Value
be when the slider is 1/4 of the way between the left and right? 25, right.
What about when it is half-way? 50. Are you following me here?

The point where the Mouse clicks represents a pixel location that is a point
inside the TrackBar. If the TrackBar slot is 10 pixels in from the left,
you subtract 10 from the X value to get the pixel position of the slider.
The total number of pixels that spans the slider represents the total amount
between Minimum and Maximum (Maximum minus Minimum). The number of pixels
from the left edge of the slot is a fraction of the total number of pixels
that spans the slider (pixel X divided by Total Pixels). The fraction is the
same fraction as the fraction of the total amount between Minimum and
Maximum that the Value would be if the slider was positioned at that point.
If you were to set the Value to this number, the slider would be positioned
there.

So, the X xel position of the Mouse click can be converted to a Value using
the following formulas:

Maximum minus Minimum = Total (number)
Slot Right (end of Slot) (pixels) minus Slot Left (beginning of Slot) pixels
= Total (pixels)
Mouse X minus Slot Left. = Mouse Slot position (pixels)
Fraction = Mouse Slot Position divided by Total (pixels)
Mouse Position (number) = Fraction Times Total (number)

Let's assign variables to them:

MaxVal - MinVal = TotalVal
SlotEndPix - SlotStartPix = TotalPix
MouseLeftPix - SlotStartPix = MouseSlotPix
Fraction = MouseSlotPix / TotalPix
MouseNum = Fraction * TotalVal

So, all you have to do is handle the Click event of the TrackBar, and use
the math I've provided to set the Value property of the TrackBar.

--
HTH,

Kevin Spencer
Microsoft MVP
Professional Chicken Salad Alchemist

What You Seek Is What You Get.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top