Thank you for visiting → Compete For Nothing.com ← If you find anything useful leave me a comment and share. Interested in Arduino, C#, Unity3D, Game Creation, VB.NET, learning and most important of all Sharing!.
Close Notification
Open Notification

VB.NET Get Cursor Location on any window (even games) Module WinAPI

VB.NET Get Cursor Location on any window (even games) Module WinAPI

This is a great way to get Cursor (or mouse) location in X and Y using WinAPI. So this means you can return the X and Y no matter what is on the screen.

OK first for the module…

Module CursorPositionWinAPI
    Structure PointAPI
        Public x As Int32 ' Integer if you prefer
        Public y As Int32 ' Integer if you prefer
    End Structure

    Public Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As PointAPI) As Boolean

    Declare Function ScreenToClient Lib "user32" (ByVal hwnd As Int32, ByRef lpPoint As PointAPI) As Int32

    Public Function MouseXposition()
        Dim lpPoint As PointAPI
        Dim MousePOS As String = GetCursorPos(lpPoint)
        Dim MouseX As String = CStr(lpPoint.x)
        Return MouseX
    End Function

    Public Function MouseYposition()
        Dim lpPoint As PointAPI
        Dim MousePOS As String = GetCursorPos(lpPoint)
        Dim MouseY As String = CStr(lpPoint.y)
        Return MouseY
    End Function

End Module

To return co-ords use it like this

Label1.Text = MouseXY()

Leave a reply