我可以延长按钮,以这样的方式添加rightClick事件的图形副作用也保持? [英] Can I extend Button to add a RightClick event in such a way that the graphical side effects are also maintained?

查看:133
本文介绍了我可以延长按钮,以这样的方式添加rightClick事件的图形副作用也保持?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图延长按钮添加rightClick事件。

我的客户想要一个按钮,这取决于如果你左键或右键单击做不同的事情。我希望那里成为右击一个简单的事件,但事实证明,有没有。

我preFER按钮的视觉行为被等同于preexisting Click事件,但它证明很难。按钮,当您点击并拖动和关闭按钮会出现很多图形化的行为。


  • 当你点击向下的按钮降低。如果您将关闭按钮降低,它会引发(例如未降低)。你拖过其他任何按钮会忽视它。

  • 当您将回到原来的按钮,它会再次下降。

  • 如果您拖动了,然后放开,原来的按钮的状态应该重置(即你不能reclick并再次拖累)。

这些小怪癖图形会显得粗糙,如果左键单击视觉效果不匹配右键单击视觉效果。

目前我卡在这:如果右键单击并按住该按钮,然后拖动关闭按钮,我怎样检测,如果用户未点击?我需要知道这一点,所以我可以不知道再次降低对再入按钮。

一个更广泛的问题:难道我甚至在正确的轨道?我找不到任何人谁是这样做过。我的code如下。

 公共类RightClickButton:按钮
{
    公共事件RoutedEventHandler单击鼠标右键;    公共RightClickButton()
    {
        this.MouseRightButtonDown + =新System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonDown);
        this.MouseRightButtonUp + =新System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonUp);
        this.MouseEnter + =新System.Windows.Input.MouseEventHandler(RightClickButton_MouseEnter);
        this.MouseLeave + =新System.Windows.Input.MouseEventHandler(RightClickButton_MouseLeave);
    }    无效RightClickButton_MouseRightButtonDown(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
    {
        this.Is pressed = TRUE;
    }    无效RightClickButton_MouseRightButtonUp(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
    {
        this.Is pressed = FALSE;
        如果(单击鼠标右键!= NULL)
            RightClick.Invoke(这一点,E);
    }    无效RightClickButton_MouseLeave(对象发件人,System.Windows.Input.MouseEventArgs E)
    {
        如果(this.Is pressed)
            this.Is pressed = FALSE;
    }    无效RightClickButton_MouseEnter(对象发件人,System.Windows.Input.MouseEventArgs E)
    {
        如果(this.IsFocused&安培;&安培; Mouse.RightButton == MouseButtonState pressed)
            this.Is pressed = TRUE;
    }
}


解决方案

由于给出一个答案<一个href=\"http://stackoverflow.com/questions/9961050/can-i-get-a-mouseleave-event-while-mouse-capture-is-active/9966984\">my另一个问题,这是我想出了。

似乎有可能是XP和Win7行为之间存在一些差异。我需要进一步测试Win7上。

 公共类RightClickButton:按钮
{
    公共事件RoutedEventHandler单击鼠标右键;    私人布尔_clicked = FALSE;    公共RightClickButton()
    {
        this.MouseRightButtonDown + =新System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonDown);
        this.MouseRightButtonUp + =新System.Windows.Input.MouseButtonEventHandler(RightClickButton_MouseRightButtonUp);
    }    //子类不能直接调用此活动,让提供此方法
    保护无效TriggerRightClickEvent(System.Windows.Input.MouseButtonEventArgs E)
    {
        如果(单击鼠标右键!= NULL)
            单击鼠标右键(这一点,E);
    }    无效RightClickButton_MouseRightButtonDown(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
    {
        this.Is pressed = TRUE;
        CaptureMouse();
        _clicked = TRUE;
    }    无效RightClickButton_MouseRightButtonUp(对象发件人,System.Windows.Input.MouseButtonEventArgs E)
    {
        ReleaseMouseCapture();        如果(this.IsMouseOver&安培;&安培; _clicked)
        {
            如果(单击鼠标右键!= NULL)
                RightClick.Invoke(这一点,E);
        }        _clicked = FALSE;
        this.Is pressed = FALSE;
    }    保护覆盖无效的OnMouseMove(MouseEventArgs E)
    {
        base.OnMouseMove(E);        如果(this.IsMouseCaptured)
        {
            布尔isInside = FALSE;            VisualTreeHelper.HitTest(
                这个,
                D =&GT;
                {
                    如果(D ==本)
                    {
                        isInside = TRUE;
                    }                    返回HitTestFilterBehavior.Stop;
                },
                HT =&GT; HitTestResultBehavior.Stop,
                新PointHitTestParameters(e.GetPosition(本)));            如果(isInside)
                this.Is pressed = TRUE;
            其他
                this.Is pressed = FALSE;
        }
    }
}

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;
    }
}

解决方案

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;
        }
    }
}

这篇关于我可以延长按钮,以这样的方式添加rightClick事件的图形副作用也保持?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆