How to find out whether a mouse button is pressed when mouse pointer is leaving a UIElement
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开发者_JS百科 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?
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...
精彩评论