Here is another, somewhat less elegant, way to it. But allows user to
resize, move, or close the form. Note: with the other method, you could
expand the clipping rectangle to include the form border, but as soon as
someone alters the forms size or location, you invalidate the rectangle.
Add a Timer to your form, give it an acceptably short cycle, and try this:
'Call me from a Timer Event
Private Sub ConstrainCursor2()
Dim newPnt As New Point
newPnt = Cursor.Position
'Check if cursor is inside form
With Cursor.Position
If .X > Me.Right Then
newPnt.X = Me.Right
ElseIf .X < Me.Left Then
newPnt.X = Me.Left
End If
If .Y < Me.Top Then
newPnt.Y = Me.Top
ElseIf .Y > Me.Bottom Then
newPnt.Y = Me.Bottom
End If
End With
If newPnt.Equals(Cursor.Position) = False Then
Cursor.Position = newPnt
End If
End Sub
Private Sub Form2_Activated(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Activated
'ConstrainCursor(True)
Timer1.Enabled = True
End Sub
Private Sub Form2_Deactivate(ByVal sender As Object, ByVal e As
System.EventArgs) Handles MyBase.Deactivate
'ConstrainCursor(False)
Timer1.Enabled = False
End Sub
Private Sub Timer1_Tick(ByVal sender As Object, ByVal e As
System.EventArgs) Handles Timer1.Tick
ConstrainCursor2()
End Sub
Gerald