比如移动到指定位置

解决方案 »

  1.   

    主要是要通过两个方面:
    (一)对鼠标的停留位置做出判断,也就是得到鼠标在屏幕上停留的位置。
    (二)将鼠标的移动到所确定的位置上。 而实现这个功能则要使用到setcursorpos这个函数,此函数的功能是设定鼠标位置。
    这个函数的声明如下: 
    public declare function setcursorpos lib "user32" (byval x as long, byval y as long) as long
    而仅仅有这个函数是不够的,还需要定义一个type格式的自定义变量。定义为:
    public type pointapi
    x as long
    y as long
    end type 
    它用于存放鼠标的位置(屏幕上的位置)。 
    但是一个新的问题又出现了:鼠标到底放在哪里呢?也就是如何获得屏幕上的位置。
    这个问题就要用到另一个函数:getcursorpos,它的功能是获得屏幕上鼠标的坐标。
    它的声明如下:
    public declare function getcursorpos lib "user32" (lppoint as pointapi) as long
    这样就可以通过getcursorpos函数获得鼠标的位置,存放到一个pointapi变量中,再通过setcursorpos函数来设置鼠标的位置。
    这样就可以十分顺利的来控制鼠标了! 
      

  2.   

    '这是一个简单的控制光标移动的程序,按开始演示按钮,光标就会向Command1按钮
    '移动,然后向Command1发送按下鼠标左键的消息Private Type RECT
            Left As Long
            Top As Long
            Right As Long
            Bottom As Long
    End TypePrivate Type POINTAPI
            X As Long
            Y As Long
    End TypeConst WM_LBUTTONUP = &H202Private Declare Function GetWindowRect Lib "user32" _
            (ByVal hwnd As Long, lpRect As RECT) As Long
    Private Declare Function SetCursorPos Lib "user32" _
            (ByVal X As Long, ByVal Y As Long) As Long
    Private Declare Function GetCursorPos Lib "user32" _
            (lpPoint As POINTAPI) As Long
    Private Declare Function SendMessage Lib "user32" _
            Alias "SendMessageA" (ByVal hwnd As Long, ByVal _
            wMsg As Long, ByVal wParam As Long, lParam As Any) As LongDim PoDist As POINTAPI
    Dim PoSrc As POINTAPI
    Dim Nx As Integer
    Dim Ny As Integer
    Dim MoveCount As Integer
    Dim PoMid As POINTAPIPrivate Sub Command1_MouseUp(Button As Integer, Shift As Integer, X As Single, Y As Single)
        MsgBox ("你按下了Command1键")
        Timer1.Enabled = False
    End SubPrivate Sub Command2_Click()
        Dim ConRect As RECT    '获得Command1在屏幕上的坐标
        GetWindowRect Command1.hwnd, ConRect
        '获得要移动到的位置的光标的坐标
        PoDist.X = ConRect.Left: PoDist.Y = ConRect.Top
        '获得当前光标的坐标
        GetCursorPos PoSrc
        
        Timer1.Enabled = True
        Nx = PoDist.X - PoSrc.X: Ny = PoDist.Y - PoSrc.Y
        '设置光标平滑移动的步数(这里是10步)
        Nx = Nx \ 10: Ny = Ny \ 10
        PoMid = PoSrc
    End SubPrivate Sub Timer1_Timer()
        PoMid.X = PoMid.X + Nx: PoMid.Y = PoMid.Y + Ny
        SetCursorPos PoMid.X, PoMid.Y
        MoveCount = MoveCount + 1
        If MoveCount > 10 Then
            Timer1.Enabled = False
            SendMessage Command1.hwnd, WM_LBUTTONUP, 10, 10
        End If
    End Sub
      

  3.   

    ClipCursor
     Confines the cursor to a rectangular area on the screen. 
     将鼠标限定在一个范围GetClipCursor
     Retrieves the screen coordinates of the rectangular area to which the cursor is confined.  获得鼠标范围GetCursorPos Retrieves the cursor's position. 
    获得鼠标位置LoadCursor Loads a cursor resource from an executable file.  
    调入鼠标图标LoadCursorFromFile Creates a cursor based on data contained in a file. 
    从文件读入鼠标图标SetCursor Sets the cursor shape. 
    设置鼠标形状SetCursorPos Moves the cursor to the specified screen coordinates. 
    设置鼠标位置SetSystemCursor
     Enables an application to customize the system cursors. 
    设置系统鼠标ShowCursor Displays or hides the cursor. 
    显示或消失鼠标
      

  4.   

    Option Explicit
    Private Type POINTAPI
        x As Long
        y As Long
    End TypePrivate Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As Long
    Dim P As POINTAPI
    Private 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)
    Private Declare Function SetForegroundWindow Lib "user32" (ByVal hwnd As Long) As Long
    Private Const MOUSEEVENTF_ABSOLUTE = &H8000 ' absolute move
    Private Const MOUSEEVENTF_LEFTDOWN = &H2 ' left button down
    Private Const MOUSEEVENTF_LEFTUP = &H4 ' left button up
    Private Const MOUSEEVENTF_MOVE = &H1 ' mouse move
    Private Const MOUSEEVENTF_MIDDLEDOWN = &H20
    Private Const MOUSEEVENTF_MIDDLEUP = &H40
    Private Const MOUSEEVENTF_RIGHTDOWN = &H8
    Private Const MOUSEEVENTF_RIGHTUP = &H10Private Sub Command1_Click()
    mouse_events
    End SubPrivate Sub Command2_Click()
    Dim test
        P.x = Screen.Width - 300
        Debug.Print P.x
        P.y = Screen.Height - 300
        Debug.Print P.y
        test = SetCursorPos(P.x, P.y)
        Command1_Click
        'SetForegroundWindow Me.hwnd
    End Sub
    Private Sub mouse_events()
    'Mouse Down
    mouse_event MOUSEEVENTF_LEFTDOWN, 0, 0, 0, 0
    'Mouse Up
    mouse_event MOUSEEVENTF_LEFTUP, 0, 0, 0, 0
    End Sub