CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 3 of 3

Thread: Move Mouse

  1. #1
    Join Date
    Oct 1999
    Posts
    10

    Move Mouse

    Is there anyway you can move the mouse to a certain position on your form?..I just need to now how to put the mouse in different positions on my form



  2. #2
    Join Date
    Nov 1999
    Posts
    7

    Re: Move Mouse

    Use mouse_event API call , You will get it



  3. #3
    Join Date
    Nov 1999
    Location
    Sacramento
    Posts
    9

    Re: Move Mouse

    As noted in other replies, you need to make a WIN API call to move it. But be careful, because you will have to give the location to which to move the mouse cursor in screen, rather than current window, coordinates. If you can assume that your application is maximized on at a known resolution, it simiplifies the problem.

    'function that moves the mouse cursor to a X, Y position
    Declare Function SetCursorPos Lib "user32" (ByVal X As Long, ByVal Y As Long) As Long

    Here is a not-fully-debugged interface:

    Sub VBSetCursorPos(X, Y)
    ' Description: This is a routine to set Mouse Tracking
    ' Cursor (Arrow, usually) to a position specified
    ' in the current Visual Basic coordinate system.
    ' Input: (x,y) - a coordinate pair in the current
    ' Visual Basis coordinate system
    ' Output: position of the mouse tracking cursor
    ' Calls: SetCursorPos - a Win API call to move the
    ' cursor (in screen pixels)
    ' Note: Most of what this routine does is to convert a
    ' pair of coordinates in VB system to screen pixels.
    ' It also stacks and restores current
    ' coordinate system.

    ' ASSUMPTIONS: This routine assumes that the current
    ' form's left, right, and bottom borders,
    ' (if any) are of equal width, and any
    ' extra border is at the top.



    Dim SaveScaleMode, SaveScaleLeft, SaveScaleTop As Single
    Dim SaveScaleWidth, SaveScaleHeight As Single
    Dim LeftBorder, RightBorder, TopBorder, BottomBorder As Single
    Dim X1, Y1, X2, Y2, X3, Y3 As Single
    SaveScaleMode = ScaleMode
    SaveScaleTop = ScaleTop
    SaveScaleLeft = ScaleLeft
    SaveScaleHeight = ScaleHeight
    SaveScaleWidth = ScaleWidth
    X1 = (X - ScaleLeft) / ScaleWidth
    Y1 = (Y - ScaleTop) / ScaleHeight

    ScaleMode = vbPixels
    X2 = Me.Width / Screen.TwipsPerPixelX
    Y2 = Me.Height / Screen.TwipsPerPixelY
    X3 = ScaleWidth
    Y3 = ScaleHeight
    LeftBorder = (X2 - X3) / 2
    RightBorder = LeftBorder
    BottomBorder = LeftBorder
    TopBorder = (Y2 - Y3) - BottomBorder
    Xpix = X1 * X3 + Left / Screen.TwipsPerPixelX + LeftBorder
    Ypix = Y1 * Y3 + Top / Screen.TwipsPerPixelY + TopBorder

    j = SetCursorPos(Xpix, Ypix)
    ScaleMode = SaveScaleMode
    ScaleTop = SaveScaleTop
    ScaleLeft = SaveScaleLeft
    ScaleHeight = SaveScaleHeight
    ScaleWidth = SaveScaleWidth

    End Sub


    Dave Straayer
    Varatouch, Inc.

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