PDA

Click to See Complete Forum and Search --> : Keep cursor in window


V(ery) Basic
Jul 31st, 2000, 02:34 PM
Well, the subject explains it all, but for those of us who have had a little too much to drink, here's the story:

I'm making a little 'joke' app and I want the cursor to be confined to my app's window so that he/she can't go 'do stuff'

I did make my own little timer thing with GetCursor and SetCursor, but it was a little messy, and since Windows in in abundance of useless functions, I'm sure there's the one I want.

Thank you for the help I am sure you are all going to give me.

Good bye, and Good night!

Jul 31st, 2000, 02:44 PM
Use the ClipCursor API. Make a Form with a CommandButton and put the following code in it.


Private Declare Function ClipCursor Lib "user32" (lpRect As Any) As Long
Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As Long
Private Declare Function GetDesktopWindow Lib "user32" () As Long
Private Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type


Private Sub Command1_Click()

Dim rRect As RECT
GetWindowRect Me.hwnd, rRect
ClipCursor rRect

End Sub

Private Sub Form_Unload(Cancel As Integer)

Dim rRect As RECT

Retval = GetDesktopWindow
GetWindowRect Retval, rRect
ClipCursor rRect

End Sub

Jul 31st, 2000, 02:45 PM
Type RECT
Left As Long
Top As Long
Right As Long
Bottom As Long
End Type
Declare Function ClipCursor Lib "user32" _
(lpRect As Any) As Long

Public Sub DisableTrap(CurForm As Form)
Dim erg As Long
'Declare a variable for the procedure
'to set the new coordinates
Dim NewRect As RECT
CurForm.Caption = "Mouse released"
'Set the new coordinates to full screen
With NewRect
.Left = 0&
.Top = 0&
.Right = Screen.Width / Screen.TwipsPerPixelX
.Bottom = Screen.Height / Screen.TwipsPerPixelY
End With
erg& = ClipCursor(NewRect)
End Sub
Public Sub EnableTrap(CurForm As Form)
Dim x As Long, y As Long, erg As Long
'Declare a variable for the procedure
'to set the new coordinates
Dim NewRect As RECT
'Get the TwipsperPixel
'The Form's ScaleMode must be set to Twips!!!
x& = Screen.TwipsPerPixelX
y& = Screen.TwipsPerPixelY
CurForm.Caption = "Mouse trapped"
'Set the Cursor-Region to the coordinates
'of the form
With NewRect
.Left = CurForm.Left / x&
.Top = CurForm.Top / y&
.Right = .Left + CurForm.Width / x&
.Bottom = .Top + CurForm.Height / y&
End With
erg& = ClipCursor(NewRect)
End Sub

Private Sub Command1_Click()
EnableTrap Form1
End Sub
Private Sub Command2_Click()
DisableTrap Form1
End Sub
Private Sub Form_Unload(Cancel As Integer)
DisableTrap Form1
End Sub

V(ery) Basic
Aug 4th, 2000, 01:37 PM
Why thank you my gallant knights in shining armour.

Forever I am endebted to your superior powers.

Cheers,

Me