CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3
  1. #1
    Join Date
    Jun 2004
    Posts
    15

    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; June 18th, 2004 at 06:15 PM.

  2. #2
    Join Date
    Jun 2004
    Posts
    15
    Does anyone know how to find the screen coords of a running windows applicaion?

  3. #3
    Join Date
    Feb 2000
    Location
    OH - USA
    Posts
    1,892
    Code:
        Private Sub Form1_MouseMove(ByVal sender As Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles MyBase.MouseMove
            Dim pScreen As New Point
            pScreen = PointToScreen(New Point(e.X, e.Y))
    
            Debug.WriteLine(String.Format("X: {0} Y: {1}", pScreen.X.ToString, pScreen.Y.ToString))
        End Sub
    Good Luck,
    Craig - CRG IT Solutions - Microsoft Gold Partner

    -My posts after 08/2015 = .NET 4.x and Visual Studio 2015
    -My posts after 11/2011 = .NET 4.x and Visual Studio 2012
    -My posts after 02/2010 = .NET 4.0 and Visual Studio 2010
    -My posts after 12/2007 = .NET 3.5 and Visual Studio 2008
    -My posts after 04/2007 = .NET 3.0 and Visual Studio 2005
    -My posts before 04/2007 = .NET 1.1/2.0

    *I do not follow all threads, so if you have a secondary question, message me.

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

Featured