Results 1 to 6 of 6

Thread: Setting Mouse Position with SetCursorPos

  1. #1

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36

    Setting Mouse Position with SetCursorPos

    I am attempting to set the position of the mouse to set coords in another window. I have been able to set the position by using SetCursorPos but for some reason I cannot get it to set the position in the other window. I am pretty sure I need to set the window by using the ClietToScreen() yet I am not able to get it to work. I am using the following code

    Code:
        Private Declare Function ClientToScreen Lib "user32" (ByVal hwnd As IntPtr, _
        ByVal lpPoint As POINTAPI) As Integer
    
        Private Declare Function SetCursorPos Lib "user32" (ByVal x As Integer, _
            ByVal y As Integer) As Integer
    
        Private Structure POINTAPI
            Dim X As Long
            Dim Y As Long
        End Structure
    Code:
            Dim pt As POINTAPI
    
            pt.X = txtX.Text
            pt.Y = txtY.Text
            ClientToScreen(myHandle, pt)
            SetCursorPos(pt.X, pt.Y)
    I have also tried varriations of this:
    Code:
            Dim pt As POINTAPI
    
            ClientToScreen(myHandle, pt)
            pt.X = pt.X + txtX.Text
            pt.Y = pt.Y + txtY.Text
            SetCursorPos(pt.X, pt.Y)

    I have the correct handle for the window (tried both as Integer and IntPtr) when I try the program with out the ClientToScreen function the mouse is moved to the coords based on my desktop, not on the window whos handle I pass. If I use the Integer or IntPtr it erros out with (Additional information: Object reference not set to an instance of an object)

    Any help would be appreciated,
    Patrick
    Last edited by SoonerToucan; Jun 18th, 2004 at 06:15 PM.

  2. #2
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    You don't need to use Win32 API calls to do either of those function calls in with the .NET Framework. Just use the following code:
    Code:
    Dim myPoint As New Point(100, 100)
    myPoint = Form1.PointToScreen(myPoint)
    System.Windows.Forms.Cursor.Current.Position = myPoint

  3. #3

    Thread Starter
    Member
    Join Date
    Jun 2004
    Posts
    36
    That does seem to be a better way to move the mouse cursor. The problem I am having is that I am trying to move the cursor to a point relative to a seperate window.

    I am attempting to find out where the window(the window of another program such as winamp) resides on the desktop. I know that the position I want my mouse to be at is 100, 100 in from the top left corner of the winamp window. Because I am trying to find the location of the winamp window it needs to be variable in case the winamp window moves.

    If you know how to find the coords of a certain windows application I can use the code above to move the mouse.

    Thanks,
    -P

  4. #4
    Hyperactive Member
    Join Date
    May 2002
    Location
    Wisconsin, USA
    Posts
    279
    You can get the position of a window with the GetWindowRect function.

    http://pietschsoft.com/programming/v...indowrect.html

  5. #5
    Frenzied Member MrGTI's Avatar
    Join Date
    Oct 2000
    Location
    Ontario, Canada
    Posts
    1,277
    Code:
    Dim myPoint As New Point(100, 100)
    myPoint = Form1.PointToScreen(myPoint)
    System.Windows.Forms.Cursor.Current.Position = myPoint
    That code doesn't work crpietschmann.

    The bolded section returns the error "Reference to a non-shared member requires an object reference."
    ~Peter


  6. #6

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