disable mouse

F

Fei

Hi,


Is this possible that when the user move out a form, the mouse is
disabled/hidden? I want to restrict user to use mouse just in my
application.

Thanks !

Fei
 
G

Gerald Hernandez

Well, this is a pretty unfriendly thing to do. Hope you have a good reason.
Here is an easy way to restrict to your application client area.

Private Sub ConstrainCursor(ByVal Constrain As Boolean)
Dim clientRect As Rectangle

If Constrain = True Then
Me.Cursor = Cursors.Cross
Cursor.Clip = Me.RectangleToScreen(Me.ClientRectangle)
Else
Me.Cursor = Cursors.Default
'Might have security issues with this
Cursor.Clip = SystemInformation.VirtualScreen
'So you could pass a bad rectangle to reset
'Cursor.Clip = New Rectangle(0, 0, 0, 0)
End If

End Sub

Be sure to give yourself a way to call this with Constrain = False.
Otherwise you'll need to ALT-F4 to close your app to get your cursor out.
Note, this does not prevent user from ALT-TAB'ing to another app, but the
cursor will still be locked to your app area. Not friendly! So you might
want to deactivate this when your app loses focus. Like so:

Private Sub Form1_Activated( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Activated

ConstrainCursor(True)
End Sub

Private Sub Form1_Deactivate( _
ByVal sender As Object, ByVal e As System.EventArgs) _
Handles MyBase.Deactivate

ConstrainCursor(False)
End Sub

Gerald
 
G

Gerald Hernandez

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top