Results 1 to 2 of 2

Thread: Mouse Movement

  1. #1

    Thread Starter
    New Member
    Join Date
    Feb 2000
    Location
    West Launceston
    Posts
    15

    Post

    How do you detect the mouses movement with out using a mouse event in a form or any other thing in the tool bar in vb?

  2. #2
    Frenzied Member
    Join Date
    Jan 2000
    Location
    Bellevue, WA, USA
    Posts
    1,357

    Post Use an API...

    You can use the GetCursorPos API function to get the cursor position from anywhere on the screen (not just over your form or controls).

    Here is a sample project:

    1. Start a new VB project
    2. Add a standard module (Module1)
    3. Add 2 labels to your form (Label1 and Label2)
    4. Add a timer to your form (Timer1)
    5. Set the timer Interval property to 1
    6. Paste the first section of code into the Module
    7. Paste the second section of code into the form's code window
    8. Run the project and move the mouse around!


    Here is the code for the Module:
    Code:
    Option Explicit
    
    Type POINTAPI
        x As Long
        y As Long
    End Type
    
    Declare Function GetCursorPos Lib "user32" _
        (lpPoint As POINTAPI) As Long
    Here is the code for the form's code window:
    Code:
    Option Explicit
    
    Private Sub Form_Load()
        'This just sets up the form and controls (not really necessary)
        With Me
            .Caption = "Cursor Position"
            .Width = 2750
            .Height = 1500
            .left = (Screen.Width - .Width) / 2
            .top = (Screen.Height - .Height) / 2
        End With
        
        With Label1
            .Alignment = vbCenter
            .Width = 1750
            .Height = 300
            .left = (Me.ScaleWidth - .Width) / 2
            .top = 100
        End With
        
        With Label2
            .Alignment = vbCenter
            .Width = Label1.Width
            .Height = Label1.Height
            .left = Label1.left
            .top = Label1.top + Label1.Height + 100
        End With
    End Sub
    
    Private Sub Timer1_Timer()
        'This is the real meat of the program...not too difficult!
        Dim CursorPos As POINTAPI
        
        GetCursorPos CursorPos    'Assigns cursor's x and y to CursorPos
        
        Label1 = "Cursor X = " & CursorPos.x
        Label2 = "Cursor Y = " & CursorPos.y
    End Sub
    Hope that helps!

    ~seaweed

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