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

    How can you move the cursor w/out moving the mouse?

    HI,

    I have tried using SetCursorPos to adjust the position of the cursor on screen, without the mouse, but I want it to adjust the cursor position relative to something else. E.g. :

    FirstMousePosition.X - 10

    Where 10 represents an `x` co-ordinate.

    Is it possible to do this?

    Any Info

    Thanks

    Mark


  2. #2
    Join Date
    Jun 2001
    Location
    MO, USA
    Posts
    2,868

    Re: How can you move the cursor w/out moving the mouse?

    Take a look at SetCaretPos rather than SetCursorPos.


  3. #3
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How can you move the cursor w/out moving the mouse?

    Just to move cursor:
    t& = SetCursorPos(0,0)

    This will only work if the formula has been declared in the declarations section:

    Declare Function SetCursorPosition& Lib "user32" _
    (ByVal x as long, ByVal y as long)


    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  4. #4
    Join Date
    May 2000
    Location
    New York, NY, USA
    Posts
    2,878

    Re: How can you move the cursor w/out moving the mouse?

    Move cursor and simulate click

    Declare Function GetCursorPos Lib "user32" (lpPoint As POINTAPI) _
    As Long


    Declare Function ClientToScreen Lib "user32" (ByVal hwnd As _
    Long, lpPoint As POINTAPI) As Long


    Declare Sub mouse_event Lib "user32" (ByVal dwFlags As Long, _
    ByVal dx As Long, ByVal dy As Long, ByVal cButtons As Long, _
    ByVal dwExtraInfo As Long)
    Public Const MOUSEEVENTF_MOVE = &H1
    Public Const MOUSEEVENTF_LEFTDOWN = &H2
    Public Const MOUSEEVENTF_LEFTUP = &H4
    Public Const MOUSEEVENTF_RIGHTDOWN = &H8
    Public Const MOUSEEVENTF_RIGHTUP = &H10
    Public Const MOUSEEVENTF_MIDDLEDOWN = &H20
    Public Const MOUSEEVENTF_MIDDLEUP = &H40
    Public Const MOUSEEVENTF_ABSOLUTE = &H8000


    Public Type POINTAPI
    X As Long
    Y As Long
    End Type
    The cursor is moved On the screen by setting the coordinates For the desired position on the screen. The top-left corner is (0,0) and the bottom-right is (65535,65535). In order to move the cursor accurately, you have to convert any coordinates into this system. For this example the user will click on one command button, the program will move the cursor to the center of another button and click. Here's the code for the two functions. The second is just a response to the simulated click.
    Command1 Click


    Private Sub Command1_Click()
    Dim pt As POINTAPI, curX As Long, curY As Long
    Dim dstX As Long, dstY As Long
    ScaleMode = vbPixels
    pt.X = Command2.Width / 2
    pt.Y = Command2.Height / 2
    ClientToScreen Command2.hwnd, pt
    dstX = pt.X * 65535 / ScaleX(Screen.Width, vbTwips, vbPixels)
    dstY = pt.Y * 65535 / ScaleY(Screen.Height, vbTwips, vbPixels)
    mouse_event MOUSEEVENTF_ABSOLUTE + MOUSEEVENTF_MOVE + _
    MOUSEEVENTF_LEFTDOWN + MOUSEEVENTF_LEFTUP, dstX, _
    dstY, 0, 0
    End Sub
    Command2 Click


    Private Sub Command2_Click()
    MsgBox "Clicked me"
    End Sub


    Iouri Boutchkine
    iouri@hotsheet.com
    Iouri Boutchkine
    iouri@hotsheet.NOSPAM.com

  5. #5
    Join Date
    Jun 2001
    Posts
    65

    Re: How can you move the cursor w/out moving the mouse?

    Rather than set a co-ordinate (0,0), I would like to set a co-ordinate FROM something else. E.g. On the screen, where I have FirstMousePosition.X, I would like to click anwhere on the screen, and for the cursor position to be moved to the co-ordinates by the object on the screen?

    Can you do this?

    Any Info

    Thanks

    Mark


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