Results 1 to 6 of 6

Thread: HitTest

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    HitTest

    Here's what I'm trying to accomplish:
    - up to 5 sliders on a single y-axis "track"
    - when a slider is clicked, it's draggable along the track

    So I've started with a Grid control to define the "track", and Rectangle objects for the sliders. The Grid is present in XAML (name "DragBounds"), and the first Rectangle is being created like so:
    Code:
    Rectangle[] dragRects = new Rectangle[5];
    dragRects[0] = new Rectangle();
    dragRects[0].Width = 15;
    dragRects[0].Height = 15;
    dragRects[0].VerticalAlignment = VerticalAlignment.Top;
    dragRects[0].Fill = new SolidColorBrush(Colors.White);
    DragBounds.Children.Add(dragRects[0]);
    Then I assigned a function to DragBounds' PreviewMouseMove, which moves the Rectangle by changing its top margin based on the mouse position:
    Code:
    private void DragBounds_onDrag(object sender, System.Windows.Input.MouseEventArgs e)
    {
        Point mousePos = e.GetPosition(DragBounds);
        if (e.LeftButton == MouseButtonState.Pressed && mousePos.Y >= 0)
        {
            dragRects[0].Margin = new Thickness(0, mousePos.Y - dragRects[0].Height / 2, 0, 0);
        }
    }
    This works, but it moves the Rectangle around regardless of where I click on the Grid; I only want it to move the Rectangle if the Rectangle was clicked. So I thought to add a hittest into the function attached to DragBounds' PreviewMouseLeftButtonDown:
    Code:
    private void DragBounds_onClick(object sender, MouseButtonEventArgs e)
    {
        startPoint = e.GetPosition(DragBounds);
        if (VisualTreeHelper.HitTest(dragRects[0], startPoint) != null)
        {
            System.Windows.Forms.MessageBox.Show("hit it", "Status", MessageBoxButtons.OK);
        }
    }
    For testing's sake, "hit it" should pop up when the Rectangle is clicked, and when the Rectangle has 0 margin on all sides, this works. But when the top margin is increased (and the Rectangle's position is displaced), it only pops up the message when I click at the very top of the Grid - presumably within the 15 pixel height of the original placement of the Rectangle.

    What might I be doing wrong here? Moreover, if there's an entirely better way to accomplish what I want, I would like to know that as well. I am a total newb to this subject matter - so no explanation is too basic.

  2. #2
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: HitTest

    are you just creating a custom slider control? couldnt you just use the one thats built into wpf and change its template to how you would like it? maybe im misunderstanding what ur trying to do..

    http://www.c-sharpcorner.com/UploadF...WpfSlider.aspx

  3. #3

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: HitTest

    I saw the Slider control, but thought you could only have 1 "slider" per "track" - is that incorrect? Could I use the Slider control and have multiple sliders on it?

  4. #4
    Lively Member
    Join Date
    Jul 2007
    Posts
    127

    Re: HitTest

    http://marlongrech.wordpress.com/200...rol-templates/
    http://www.thejoyofcode.com/Creating...Controls_.aspx

    so your looking for a range slider of some sort? check out those 2 links i posted. they might be able to help you, but yeah, it looks like you will have to create a custom control. but it looks like it has been done before

  5. #5

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: HitTest

    Yes, those look like what I want! I'd imagined someone had done it before, but seems like my searching was inadequate. Thanks - I'll look over them and see if I have any further problems.

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Apr 2009
    Location
    CA, USA
    Posts
    1,516

    Re: HitTest

    Some more questions... I grabbed the source from the second link and have been learning about custom controls. It's going alright, and I've come to some other needs for the control.

    I want the control to be able to do something when a user clicks on the track (as opposed to the thumb(s)). So I thought I'd add a button under the track of my control and assign the desired action to its click event. This seems to work, but after I click it, the button looks like it has been moved up above the track and its thumbs - I can no longer drag the thumbs. Can I prevent this? Also, I want this button to be "invisible" - before, during and after being clicked - can I do that?

    Like with my original question, if the way I'm trying to accomplish the goal is totally stupid and there's a much better way, please let me know.

    Edit: Tried changing the button to a rectangle and attaching a function for "MouseLeftButtonDown". That takes care of the "invisible" aspect, but same problem exists with the object stack order.

    Edit 2: Nevermind, figured it out. Had nothing to do with the stack order after all.
    Last edited by SambaNeko; Mar 21st, 2010 at 11:04 PM.

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