简体   繁体   English

MouseUp事件中断了吗?

[英]MouseUp event interrupted?

I'm making a custom control with a panel. 我正在使用面板进行自定义控件。 I want to be able to drag and drop it so I've implemented that in the MouseDown event of my control. 我希望能够拖放它,因此已经在控件的MouseDown事件中实现了它。 But I want the thing to react when you start drag to give a little feedback to the user. 但是我希望事情在开始拖动时能够做出反应,以向用户提供一些反馈。 So in the MouseDown even I change the color. 因此,即使在MouseDown中,我也更改了颜色。 Then I want to change it back in the MouseUp event. 然后,我想在MouseUp事件中将其更改回。

My control is not installed into VS2008 but just a class I've written that I instanciate at run time (I don't know in advance how many I need and so on). 我的控件未安装到VS2008中,而只是安装了我编写的一个类,可以在运行时实例化(我事先不知道需要多少,依此类推)。 Now, my control exposes a MouseDown event so as to be able to be dragged. 现在,我的控件公开了MouseDown事件,以便能够拖动它。 When I subscribe to this event from the parent application to actually perform the drag and drop my control is not repainted on its MouseUp event! 当我从父应用程序订阅此事件以实际执行拖放操作时,我的控件不会在其MouseUp事件上重新绘制! In fact, the MouseUp is never invoked. 实际上,从不调用MouseUp。 If, on the other hand, I don't subscribe to the event in the parent app it works as intended. 另一方面,如果我未在父应用程序中订阅该事件,则该事件按预期运行。

What's going on? 这是怎么回事? Is the parent interrupting the flow so that the MouseUp event never fires in my control? 父级是否正在中断流,以便MouseUp事件从不触发我的控件? How do I get around this? 我该如何解决?

I'm not sure if you are using Windows Forms or WPF, but in Windows forms here is what I mean: 我不确定您是使用Windows窗体还是WPF,但是在Windows窗体中,这是我的意思:

public class DerivedPanel : Panel
{
    protected override void OnMouseDown(MouseEventArgs e)
    {
        base.OnMouseDown(e);
        Capture = true;
    }

    protected override void OnMouseUp(MouseEventArgs e)
    {
        base.OnMouseUp(e);
        Capture = false;
        // Change your color or whatever here
    }
}

In WPF there are two methods, CaptureMouse() and ReleaseMouseCapture() to do the same thing. 在WPF中,有两种方法,CaptureMouse()和ReleaseMouseCapture()可以完成相同的操作。 When the control captures the mouse, it will received mouse events even if the cursor isn't over the control. 当控件捕获鼠标时,即使光标不在控件上,它也会收到鼠标事件。 This could be causing your problem. 这可能导致您的问题。 See MSDN Article 请参阅MSDN文章

Do you capture the mouse in the custom control on the mousedown event? 您是否在mousedown事件的自定义控件中捕获了鼠标? Try capturing on the mousedown and releasing the capture on the mouseup. 尝试在mousedown上捕获并在mouseup上释放捕获。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM