开发者

Button not hiding when mouse leaves from bottom

开发者 https://www.devze.com 2023-01-17 23:10 出处:网络
I have a form where I want buttons at the very bottom edge of the form, with no gap to the border. These buttons shall be \"auto-hide\", so they only show when the mouse is in for example the lower 20

I have a form where I want buttons at the very bottom edge of the form, with no gap to the border. These buttons shall be "auto-hide", so they only show when the mouse is in for example the lower 20 pixels of the form. So I use the MouseMove event to trigger this, like code below. However, if mouse leaves the form across the bottom edge, where the buttons are, then the buttons will obviously remain. But I want them to hide. So I need for this purpose to hide the buttons by some other event. Hence I try to hide them in the form's MouseLeave event. But this makes the buttons unclickable and in an erratic state, flashing on and off when the mouse goes over the button.. Why is this? And how can I avoid this problem to get such autohide feature?

  Private Sub ZgScale_MouseMove(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove

    If e.Y > Me.ClientSize.Height - 30 Then
        Button1.Visible = True
    Else
        Button1.Visible = False
    End If
End Sub

Private开发者_如何学JAVA Sub ZgScale_MouseLeave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.MouseLeave
    Button1.Visible = False
End Sub


The MouseLeave event fires when the mouse is no longer directly on that control (or form).
If the mouse moves on to a child control, the event will fire.

You need to check whether the mouse is no longer on the form, like this:

If Not Me.ClientRectangle.Contains(Me.PointToClient(e.Location)) Then
    Button1.Visible = False
End If

EDIT: Fixed


Windows has direct support built-in for this scenario. Also exposed in Windows Forms and WPF. Once you get the MouseMove event, set the Capture property on the control to True. That forces all mouse messages to be directed to the control, even if the mouse moves outside of the control window.

Once you see it moving outside of the control bounds, set Capture back to false and hide your control. Beware that capture is turned off when the user clicks the mouse so you'll probably have to turn it back on afterwards. Although it should be automatic, you'll get another MouseMove event. Could fail if the user moves the mouse really fast.

0

精彩评论

暂无评论...
验证码 换一张
取 消