tags:

views:

107

answers:

3

So I am trying to make a program where the user is limited in moving the mouse to a form. I still want the user to see whats going on behind the form but not be able to click on anything but my form.

I tried using this

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

The problem is that if the user clicks in certain spots on the titlebar...the mouse is unlocked. The machines this is going on has a ball type of mouse so if you scroll fast and click fast enough...it can also unlock.

I'm very stuck on how to solve this.

I'm coding in VB.NET 2.0

Thanks in advance.

+1  A: 

If the user clicks outside of your application, then you have no control anymore. What you are doing is a bad idea. Any time you try to limit the mouse, you are asking for trouble anyway. Why is it you are trying to do this?

Gabriel McAdams
I am trying to make an application, like i said, will lock my system while my primary application is running. My company uses a piece of software on our system that runs at all times. The problem is if you lock windows via windows ctrl-alt-del, then it hides the desktop. I want to still see the desktop and what the program behind it is doing. I need to limit someone other than the user from sitting down at the machine and interrupting their work while they are away.
Sean P
+1  A: 

What you need is a global windows hook. You could mess around with rolling your own or you can pick up a DLL someone else made here: http://www.vbforums.com/showthread.php?t=436321

If you use the DLL you just need to instantiate it, install the hook, wire for events and remember to shut it down when you're done. I assume you can handle calculating things in GlobalMouseMove()

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
Chris Haas
I am kinda of a novice so I am going to have to give it a try then come back to you with my results.
Sean P
This is bad advice. Global hooks inject code into _every_ process that has a window.
John Knoeller
Why the negative one? Yes, global hooks are a bad idea but that's the only way to capture the mouse across the entire system. Your problem isn't with the answer, its the question and it appears that he has a valid reason for doing this.
Chris Haas
I do have a valid reason. I will try out this solution and see what happens.
Sean P
Yep, your answer was a good 'un, I've given you a +1. I agree though, the question is a wee bit silly, but it doesn't negate your effort in helping Sean achieve his goals (hopefully not of world domination!)
Dan F
A: 

Clipping the cursor is something you need to be very careful about. You should only do it on a buttondown message, and only while you have mouse capture.

If you accidently abandon a Mouse clip, you can leave the system in an unuseable state until rebooted. And people who only know how to operate the system with a mouse, won't be able to reboot

So, Don't try this at home.

If Cursor.Clip isn't working for you, you could try using interop to get to 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 be sure that you ClipCusor(NULL) to remove the clipping rect when you are done.

Also, I'm puzzled why you say that the user can get to icons on the title bar when the cursor clip is in effect, if you a really clipping to your client area, that should't be possible. Going fast to hop out of the clip also shouldn't be possible.

John Knoeller
For some reason when the user clicks and holds, the mouse can leave the area.
Sean P
Clicks on what? What they clip on is probably removing the cursor clip.
John Knoeller