要求很简单: 就是写代码把鼠标设定在界面的正中.
代码怎么写?

解决方案 »

  1.   

    Option ExplicitPrivate Type POINTAPI
        x As Long
        y As Long
    End Type
    Private Type RECT
        left As Long
        top As Long
        right As Long
        bottom As Long
    End Type
    Private Declare Function ClientToScreen Lib "user32.dll" (ByVal hwnd As Long, ByRef lpPoint As POINTAPI) As Long
    Private Declare Function SetCursorPos Lib "user32.dll" (ByVal x As Long, ByVal y As Long) As Long
    Private Declare Function ClipCursor Lib "user32.dll" (ByRef lpRect As Any) As Long
    Private Declare Function GetWindowRect Lib "user32" (ByVal hwnd As Long, lpRect As RECT) As LongPrivate Sub Command1_Click()
        Dim pt As POINTAPI
       
        pt.x = Me.ScaleWidth / Screen.TwipsPerPixelX / 2
        pt.y = Me.ScaleHeight / Screen.TwipsPerPixelY / 2
            
        ClientToScreen Me.hwnd, pt
        
        'Set Cursor position
        SetCursorPos pt.x, pt.y
        
    End SubPrivate Sub Command2_Click()
        Dim rt As RECT
        Static flag As Boolean
        
        If flag = False Then
            'Limits the Cursor movement to within the Command2.
            GetWindowRect Command2.hwnd, rt
            ClipCursor rt
        Else
            'Releases the cursor limits
            ClipCursor ByVal 0&
        End If
        flag = Not flag
    End Sub
      

  2.   

    Option Explicit'API:设定鼠标的位置
    Private Declare Function SetCursorPos Lib "user32" (ByVal x As Long, ByVal y As Long) As LongPrivate Sub Command1_Click()    '取得鼠标在界面中间位置的Twip值(相对于Screen)
        '并把Twip转换为Pixel
        Dim x As Long
        Dim y As Long
        x = (Form1.Left + Form1.Width / 2) \ Screen.TwipsPerPixelX
        y = (Form1.Top + Form1.Height / 2) \ Screen.TwipsPerPixelY
        
        Dim ret As Long
        ret = SetCursorPos(x, y)  ' (X, Y) 为欲设定的鼠标座标,座标单位是 Pixel(像素)
    End Sub