Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I extend Button to add a RightClick event in such a way that the graphical side effects are also maintained?

I'm trying to extend Button to add a RightClick event.

My customer wants a button to do different things depending on if you left-click or right-click. I expected there to be an easy event for right-clicking, but it turns out there's not.

I'd prefer Button's visual behavior to be identical to the preexisting Click event, but it's proving tough. Button has many graphical behaviors that occur when you click and drag on and off the button.

  • When you click-down, the button lowers. If you drag off the lowered button, it raises (e.g. un-lowers). Any other buttons you drag over will ignore it.
  • When you drag back on the original button, it will lower again.
  • If you drag off and then release, the original button's state should reset (i.e. you can't reclick and drag on again).

These little graphical quirks will look rough if the left-click visuals don't match the right-click visuals.

Currently I'm stuck on this: If right-click and hold the button, then drag off the button, how do I detect if the user un-clicks? I need to know this so I can know not to re-lower the button on re-entry.

A broader question: Am I even on the right track? I couldn't find anyone who's done this before. My code is below.

public class RightClickButton : Button
{
    public event RoutedEventHandler RightClick;

    public RightClickButton()
    {
        this.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonDown);
        this.MouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonUp);
        this.MouseEnter += new System.Windows.Input.MouseEventHandler(RightClickButton_MouseEnter);
        this.MouseLeave += new System.Windows.Input.MouseEventHandler(RightClickButton_MouseLeave);
    }

    void RightClickButton_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.IsPressed = true;
    }

    void RightClickButton_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.IsPressed = false;
        if (RightClick != null)
            RightClick.Invoke(this, e);
    }

    void RightClickButton_MouseLeave(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (this.IsPressed)
            this.IsPressed = false;
    }

    void RightClickButton_MouseEnter(object sender, System.Windows.Input.MouseEventArgs e)
    {
        if (this.IsFocused && Mouse.RightButton == MouseButtonState.Pressed)
            this.IsPressed = true;
    }
}
like image 545
Grant Birchmeier Avatar asked Mar 30 '12 22:03

Grant Birchmeier


2 Answers

Thanks to an answer given to my other question, this is what I came up with.

It seems there might be some differences between XP and Win7 behavior. I'll need to test further on Win7.

public class RightClickButton : Button
{
    public event RoutedEventHandler RightClick;

    private bool _clicked = false;

    public RightClickButton()
    {
        this.MouseRightButtonDown += new System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonDown);
        this.MouseRightButtonUp += new System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonUp);
    }

    // Subclasses can't invoke this event directly, so supply this method
    protected void TriggerRightClickEvent(System.Windows.Input.MouseButtonEventArgs e)
    {
        if (RightClick != null)
            RightClick(this, e);
    }

    void RightClickButton_MouseRightButtonDown(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        this.IsPressed = true;
        CaptureMouse();
        _clicked = true;
    }

    void RightClickButton_MouseRightButtonUp(object sender, System.Windows.Input.MouseButtonEventArgs e)
    {
        ReleaseMouseCapture();

        if(this.IsMouseOver && _clicked)
        {
            if (RightClick != null)
                RightClick.Invoke(this, e);
        }

        _clicked = false;
        this.IsPressed = false;
    }

    protected override void OnMouseMove(MouseEventArgs e)
    {
        base.OnMouseMove(e);

        if (this.IsMouseCaptured)
        {
            bool isInside = false;

            VisualTreeHelper.HitTest(
                this,
                d =>
                {
                    if (d == this)
                    {
                        isInside = true;
                    }

                    return HitTestFilterBehavior.Stop;
                },
                ht => HitTestResultBehavior.Stop,
                new PointHitTestParameters(e.GetPosition(this)));

            if (isInside)
                this.IsPressed = true;
            else
                this.IsPressed = false;
        }
    }
}
like image 200
Grant Birchmeier Avatar answered Sep 30 '22 02:09

Grant Birchmeier


If you look at the source code for ButtonBase, you'll see that it would be difficult to easily duplicate what's happening in the OnMouseLeftButtonDown and OnMouseLeftButtonUp overrides.

A simple hack which seems to work fairly well is to derive from Button as follows:

public class MyButton : Button
{
    protected override void OnMouseRightButtonDown(MouseButtonEventArgs e)
    {
        base.OnMouseRightButtonDown(e); // this line probably optional/not required
        base.OnMouseLeftButtonDown(e);
    }

    protected override void OnMouseRightButtonUp(MouseButtonEventArgs e)
    {
        base.OnMouseRightButtonUp(e); // this line probably optional/not required
        base.OnMouseLeftButtonUp(e);
    }
}

Then use <MyButton> in place of <Button> in your Xaml.

I can't guaranteee this will work correctly in all scenarios, but it does seem to work as required in that the visual behaviour of the button is the same when right or left clicking.

like image 25
Phil Avatar answered Sep 30 '22 03:09

Phil