getting x,y conodinates using GetCursorPos from anywhere on screen

mcfly

Well-known member
Joined
Jun 15, 2009
Messages
54
Programming Experience
Beginner
Hi all,

Here's the code thus far, excuse the other bits in between, these are for getting the foreground form title, form coordinates etc, i now need the cursor coorindates where ever the mouse cursor is to:

VB.NET:
Option Explicit On
Imports System.Text
Imports System.Runtime.InteropServices
'Imports System.Windows.Forms.Cursor.Current.Position

Public Class Form1

    Private Declare Auto Function GetForegroundWindow Lib "user32" () As IntPtr

    Private Const WM_GETTEXT As Integer = &HD

    <DllImport("User32", SetlastError:=True)> _
    Private Shared Sub SendMessage(ByVal hwnd As IntPtr, ByVal messageId As Integer, ByVal wparam As Integer, <Out()> ByVal lpString As System.Text.StringBuilder)
    End Sub

    Declare Function GetWindowRect Lib "user32.dll" (ByVal hwnd As Int32, ByRef lpRect As RECT) As Int32

    Structure RECT
        Public Left As Int32
        Public Top As Int32
        Public Right As Int32
        Public Bottom As Int32
    End Structure

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'aligns form at the top right hand side 
        Me.Left = System.Windows.Forms.Screen.PrimaryScreen.Bounds.Width - Me.Width
    End Sub

    Private Sub txtGo_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnGo.Click
        If btnGo.Text = "Stop" Then
            eyes.Enabled = False
            btnGo.Text = "Go"
            Exit Sub
        Else
            eyes.Enabled = True
            btnGo.Text = "Stop"
        End If
    End Sub

    'API Functions, Subs and Constants
    Public Declare Auto Function SetCursorPos Lib "User32.dll" (ByVal X As Integer, ByVal Y As Integer) As Long
    Public Declare Sub mouse_event Lib "user32" Alias "mouse_event" (ByVal dwFlags As Long, ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, ByVal dwExtraInfo As Long)
    Public Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Public Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20 ' middle button down
    Public Const MOUSEEVENTF_MIDDLEUP = &H40 ' middle button up
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8 ' right button down
    Public Const MOUSEEVENTF_RIGHTUP = &H10 ' right button up

    Public Structure POINTAPI
        Public x As Long
        Public y As Long
    End Structure

    Public Declare Function GetCursorPos Lib "user32" (ByRef lpPoint As POINTAPI) As Integer

    Private Sub eyes_Tick(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles eyes.Tick
        'get thread pointer
        Dim formPointer As IntPtr = GetForegroundWindow
        TextBox1.Text = formPointer

        Dim valueToRetrive As New StringBuilder(100)
        Dim remoteHandle As IntPtr = New IntPtr(Convert.ToInt32(TextBox1.Text))

        'get form name
        SendMessage(remoteHandle, WM_GETTEXT, 100, valueToRetrive)
        txtName.Text = valueToRetrive.ToString()

        'get RECT coordinates to thread called remoteHandle
        Dim r As RECT
        GetWindowRect(remoteHandle, r)

        Dim rct As Rectangle = Rectangle.FromLTRB(r.Left, r.Top, r.Right, r.Bottom)

        txtL.Text = r.Left
        txtR.Text = r.Right
        txtT.Text = r.Top
        txtB.Text = r.Bottom

        'If txtName.Text = "Databases" Then
        '    'call move cursor
        '    moveCursor()
        'End If
        'SetCursorPos(100, 100)

        Dim p As POINTAPI
        Dim l As Long

        'Get pointer position
        l = GetCursorPos(p)

        ' Display information.
        'txtCursor.Text = CStr(p.x) & ", " & CStr(p.y)
        txtCursor.Text = p.x & ", " & p.y

    End Sub

    Public Sub moveCursor()
        'move the cusor
        SetCursorPos(100, 100) ' Where X and Y are in pixel
        'MsgBox("Moved cursor")

        'turn off the timer
        'eyes.Enabled = False
        'btnGo.Text = "Go"
    End Sub
End Class

When I get try and get the x and y coorindates only the x is appearing i think, this gives me a very long number, the y is set to 0. Not sure if I need to take this x number and split it to get my x and y or whether there is logic error in my code, any ideas anyone?...
 
does this work for anywhere on screen or is just over a windows form? I need the coordinates of anywhere on screen.

With regards to the code above the x figure i get back looks like this when I set it too x = 100 and y = 100

429496729700

How is this an x coordinate, it changes when my cursor is moved so looks like it's interacting ok with the cursor but am not sure this is the cursor x coordinate?:confused: i can't seem to make sense of it...
 
Property Value
Type: System.Drawing.Point
A Point that represents the cursor's position in screen coordinates.
Yes, it is screen, did your code at any point fail to verify this? The Point value has X and Y properties.
 
sorted, it was:

Public Structure POINTAPI
Public x As Int32
Public y As Int32
End Structure

so simple doh, untill the next time, thanks again for your help :)
 
No, actually the type is System.Drawing.Point, System.Drawing namespace is always imported in a Forms project so you can refer to it as Point only.
 
Back
Top