I have a form that contains a Panel.
At first, this Panel invisible and becomes visible when the mouse enters its Client Area.
The code I use works as long as I do not resize the Form.
By changing the Form size, this code no longer works.
I tried this code:
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Label1.Text = e.Location.ToString
If e.Location.Y > 441 Then
Panel1.Visible = True
Else
Panel1.Visible = False
End If
End Sub
Since it appears that this Panel is docked to the bottom of your Form, you can just consider the Panel's Bounds (the rectangle that describes the area a Control occupies inside its container) to determine whether the Mouse position is inside its Client Area.
The Rectangle.Contains(Point) method returns True or False if a Point is contained inside the bounds of the Rectangle specified.
You can then use the Boolean result to also set the visibility of the Panel:
Private Sub Form1_MouseMove(sender As Object, e As MouseEventArgs) Handles Me.MouseMove
Panel1.Visible = Panel1.Bounds.Contains(e.Location)
End Sub
Extra:
How to handle other common cases:
You cannot add event handlers to all Controls in the Form that may, at some point, be involved in these actions. Controls can be added or removed or moved around in the designer. Not feasible.
In these and other similar cases, implementing IMessageFilter comes in handy.
You can use the Application.AddMessageFilter / Application.RemoveMessageFilter methods to monitor messages directed to any Control before they are dispatched to the destination.
You can also prevent the messages from reaching the destination, if needed, returning True in the IMessageFilter.PreFilterMessage() method.
Here, the Form class implements IMessageFilter directly. You can of course create a reusable helper class to do the same.
Public Class SomeForm
Implements IMessageFilter
Private Const WM_MOUSEMOVE As Integer = &H200
Public Sub New()
InitializeComponent()
Application.AddMessageFilter(Me)
End Sub
Protected Overrides Sub OnFormClosed(e As FormClosedEventArgs)
Application.RemoveMessageFilter(Me)
MyBase.OnFormClosed(e)
End Sub
Public Function PreFilterMessage(ByRef m As Message) As Boolean Implements IMessageFilter.PreFilterMessage
If m.Msg = WM_MOUSEMOVE Then
Panel1.Visible = Panel1.RectangleToScreen(Panel1.ClientRectangle).Contains(MousePosition)
End If
Return False
End Function
End Class
Try this:
Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles Me.MouseMove
Label1.Text = e.Location.ToString
If e.X > Panel1.Left And e.X < Panel1.Left + Panel1.Width And e.Y > Panel1.Top And e.Y < Panel1.Top + Panel1.Height Then
Panel1.Visible = True
Else
Panel1.Visible = False
End If
End Sub