当鼠标指针离开 UIElement 时如何确定鼠标按钮是否被按下
我一直在 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 ,但总是错误的。 UIElement
或 Rectangle
中是否有任何布尔值事件可以帮助我解决这个问题?
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在
MouseDown
事件中调用myRectangle.CaptureMouse()
,这样即使鼠标移动到矩形之外也会触发MouseUp
,检查 <在MouseUp
事件中使用 code>IsMouseCaptured 来查看MouseDown
是否确实发生在矩形内部,并通过myRectangle.ReleaseMouseCapture()
释放它>。要检查鼠标是否离开,您可以处理
MouseMove
事件并查看鼠标是否仍在控件上,我不确定Mouse.DirectlyOver
是否会返回捕获期间的正确值,我需要测试......Call
myRectangle.CaptureMouse()
in theMouseDown
event, that wayMouseUp
will be fired even if the mouse moves outside the rectangle, check forIsMouseCaptured
in theMouseUp
event to see if theMouseDown
actually occurred inside the rectangle and release it viamyRectangle.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 ifMouse.DirectlyOver
would return the correct value during capture, i'll need to test that...