Restricting the mouse to a shape without the possibility of overriding

So, I am trying to create a program where the user is restricted when the mouse moves into the form. I still want the user to see what is going on behind the form, but cannot click on anything other than my form.

I tried using this

Cursor.Clip = Me.RectangleToScreen(Me.ClientRectangle)

      

The problem is that if the user clicks in certain places on the title ... the mouse is unlocked. The machines this operation takes place have a mouse type ball, so if you scroll quickly and click fast enough ... it can unlock as well.

I am very fixated on how to solve this.

I am coding in VB.NET 2.0

Thanks in advance.

0


a source to share


3 answers


What you need is a Windows global hook. You can have some fun rolling around on your own, or you can pick up a DLL that someone else made: http://www.vbforums.com/showthread.php?t=436321

If you're using a DLL, you just need to instantiate it, set the hook, wire for events, and don't forget to close it when you're done. I am assuming that you can handle calculations inGlobalMouseMove()



Private MH As WindowsHook.MouseHook

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    Me.MH = New WindowsHook.MouseHook()
    Me.MH.InstallHook()
    AddHandler Me.MH.MouseMove, AddressOf GlobalMouseMove
End Sub
Public Sub GlobalMouseMove()
    'Perform your logic here, Cursor.Position is desktop-based so you'll have to calculate relative to the form
    Trace.WriteLine(Cursor.Position)
End Sub
Protected Overrides Sub OnClosing(ByVal e As System.ComponentModel.CancelEventArgs)
    If Me.MH IsNot Nothing Then
        Me.MH.RemoveHook()
        Me.MH.Dispose()
        Me.MH = Nothing
    End If
    MyBase.OnClosing(e)
End Sub

      

+2


a source


If the user clicks outside of your app, then you no longer have control. What you are doing is a bad idea. Every time you try to restrict the mouse, you are still asking for trouble. Why are you trying to do this?



+1


a source


Cropping the cursor is something you should be very careful about. You should only do this on the buttondown message, and only while you have a mouse grab.

If you accidentally leave the mouse clip, you can leave the system unused until you reboot. And people who know how to operate the system with a mouse will not be able to reboot

So, don't try this at home.

If Cursor.Clip

doesn't work for you, you can try using interop to access the native one. Pinvoke.net shows the prototype as:

static extern bool ClipCursor(ref RECT lpRect);
Private Declare Function ClipCursor Lib "user32"(ByRef lpRect As RECT) As Long

      

Just make sure you ClipCusor (NULL) to remove clipping when you're done.

Also, I am puzzled as to why you are saying that the user can get icons in the title bar when the clip clip is in effect, if you do indeed crop your client area it should be possible. You can't be quick to jump out of a clip.

0


a source







All Articles