Results 1 to 4 of 4

Thread: Keep cursor in window

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516

    Question

    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!
    Courgettes.

  2. #2
    Guest
    Use the ClipCursor API. Make a Form with a CommandButton and put the following code in it.

    Code:
    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

  3. #3
    Guest
    Code:
    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

  4. #4

    Thread Starter
    Fanatic Member
    Join Date
    Apr 2000
    Location
    Whats a location?
    Posts
    516
    Why thank you my gallant knights in shining armour.

    Forever I am endebted to your superior powers.

    Cheers,

    Me
    Courgettes.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width