当鼠标指针离开 UIElement 时如何确定鼠标按钮是否被按下

发布于 2024-10-30 08:21:20 字数 1850 浏览 2 评论 0原文

我一直在 WPF 中开发 GUI,但我对它还很陌生,到目前为止只使用过 Windows 窗体。到目前为止,我的 GUI 非常简单:它包含两个矩形,每个矩形都会投射一个阴影。可以说,阴影创建了“漂浮在画布上方”的矩形效果。

当按下其中一个矩形时,将处理 myRectangle.MouseDown 事件,使阴影消失,从而创建矩形被按下到画布上的效果,就像按钮一样。同样,当释放鼠标按钮时,将处理 myRectangle.MouseDown 事件,以便阴影重新出现并且矩形再次“浮动”。这种行为使它们类似于按钮。请注意,我希望它们是矩形而不是自定义按钮是有原因的。

这是我上面描述的一些代码:

Private Sub RectangleMouseDown(ByVal sender As Object, _
                               ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Dim effectNoShadow As New Effects.DropShadowEffect
    effectNoShadow.Opacity = 0
    CType(sender, Rectangle).Effect = effectNoShadow
End Sub

Private Sub RectangleMouseUp(ByVal sender As Object, _
                             ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Dim effectDropShadow As New Effects.DropShadowEffect
    effectDropShadow.Opacity = 1
    effectDropShadow.BlurRadius = 10
    effectDropShadow.ShadowDepth = 5
    CType(sender, Rectangle).Effect = effectDropShadow
End Sub

Private Sub AddHandlersToRectangles()
    For Each element As UIElement In MainGrid.Children
        If TypeOf element Is Rectangle Then                'If element is a Rectangle'
            AddHandler CType(element, Rectangle).MouseDown, AddressOf RectangleMouseDown
            AddHandler CType(element, Rectangle).MouseUp, AddressOf RectangleMouseUp
        End If
    Next
End Sub

到目前为止一切顺利。我遇到的一个问题是,当鼠标光标离开矩形而鼠标按钮仍然按下时,矩形永远保持“按下”状态,因为显然,按钮没有在矩形上方释放。

有办法解决这个问题吗?我希望看到的行为如下:

  • 如果按下鼠标按钮时指针离开矩形,我希望阴影重新出现。这可以说很简单,因为从理论上讲,在这种情况下,任何时候 myRectangle.MouseLeave 被引发时,阴影都应该重新出现,即使它已经存在了。
  • 如果按下鼠标按钮时指针离开一个矩形,然后在释放按钮之前返回,我希望阴影再次消失。这就是给我带来问题的地方。不知道该怎么做。

我尝试过 myRectangle.IsMouseCaptured ,但总是错误的。 UIElementRectangle 中是否有任何布尔值事件可以帮助我解决这个问题?

I've been working on a GUI in WPF which I'm fairly new to, having only used Windows Forms up until now. So far, my GUI is very simple: it contains two rectangles, each of which drops a shadow. The shadow creates an effect of rectangles "floating above the canvas" so to speak.

When one of the rectangles is pressed, the myRectangle.MouseDown event is handled such that the shadow goes away, thus creating an effect of the rectangle being pressed down onto the canvas, like a button. Similarly, when mouse button is released, the myRectangle.MouseDown event is handled such that the shadow reappears and the rectangle "floats" again. This behavior make them resemble buttons. Note, there a reasons I want them to be rectangles and not custom buttons.

Here's some code of what I've described above:

Private Sub RectangleMouseDown(ByVal sender As Object, _
                               ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Dim effectNoShadow As New Effects.DropShadowEffect
    effectNoShadow.Opacity = 0
    CType(sender, Rectangle).Effect = effectNoShadow
End Sub

Private Sub RectangleMouseUp(ByVal sender As Object, _
                             ByVal e As System.Windows.Input.MouseButtonEventArgs)
    Dim effectDropShadow As New Effects.DropShadowEffect
    effectDropShadow.Opacity = 1
    effectDropShadow.BlurRadius = 10
    effectDropShadow.ShadowDepth = 5
    CType(sender, Rectangle).Effect = effectDropShadow
End Sub

Private Sub AddHandlersToRectangles()
    For Each element As UIElement In MainGrid.Children
        If TypeOf element Is Rectangle Then                'If element is a Rectangle'
            AddHandler CType(element, Rectangle).MouseDown, AddressOf RectangleMouseDown
            AddHandler CType(element, Rectangle).MouseUp, AddressOf RectangleMouseUp
        End If
    Next
End Sub

So far so good. The one scenario I've got a problem with is that when the mouse cursor leaves the rectangle while the mouse button is still pressed, the rectangle stays is the "pressed" state forever because, obviously, the button wasn't released above the rectangle.

Is there a way to work around this? The behavior I'd like to see is the following:

  • If the pointer leaves a rectangle while mouse button is pressed, I'd like shadow to reappear. This is arguable simple, because theoretically in this case, any time myRectangle.MouseLeave is raised, the shadow should come back on, even if it was already there.
  • If the pointer left a rectangle when mouse button was pressed, then came back while before the button was released, I want the shadow to disappear again. This one is what gives me problems. No idea how to do this.

I've tried myRectangle.IsMouseCaptured and that's always false. Are there any events of Boolean values in UIElement or Rectangle in particular that would help me with that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

隔纱相望 2024-11-06 08:21:20

MouseDown 事件中调用 myRectangle.CaptureMouse(),这样即使鼠标移动到矩形之外也会触发 MouseUp,检查 <在 MouseUp 事件中使用 code>IsMouseCaptured 来查看 MouseDown 是否确实发生在矩形内部,并通过 myRectangle.ReleaseMouseCapture() 释放它>。

要检查鼠标是否离开,您可以处理 MouseMove 事件并查看鼠标是否仍在控件上,我不确定 Mouse.DirectlyOver 是否会返回捕获期间的正确值,我需要测试......

Call myRectangle.CaptureMouse() in the MouseDown event, that way MouseUp will be fired even if the mouse moves outside the rectangle, check for IsMouseCaptured in the MouseUp event to see if the MouseDown actually occurred inside the rectangle and release it via myRectangle.ReleaseMouseCapture().

To check if the mouse is leaving you could probably handle the MouseMove event and see if the mouse is still over the control, i am not sure if Mouse.DirectlyOver would return the correct value during capture, i'll need to test that...

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文