使用 Microsoft 登录
登录或创建帐户。
你好,
使用其他帐户。
你有多个帐户
选择要登录的帐户。

摘要

可以调用 Windows API (应用程序编程接口) 调用 Microsoft Windows DLL (动态链接库) 获取和设置当前光标位置。 当前位置可以通过使用 USER32.DLL 中的 GetCursorPos 函数获取。

更多信息

Microsoft Excel没有获取或设置光标位置的内置功能。 但是,可以使用宏中的 Declare 语句Microsoft Excel Visual Basic for Applications Microsoft Windows 函数来访问当前位置。 也可使用另一函数 SetCursorPos 设置光标位置。 SetCursorPos 函数可用于循环结构,以在屏幕上移动光标。


Microsoft 提供的示例Visual Basic for Applications说明过程,无明示或默示的担保,包括但不限于特定用途的可商家性和/或适用性的默示担保。 本文Visual Basic过程"是"提供的,Microsoft 不保证它们可用于所有情况。 虽然 Microsoft 支持专业人员可以帮助解释特定宏的功能,但他们不会修改这些示例以提供附加功能,也不会帮助你构建宏以满足你的特定需求。 如果编程经验有限,可能需要咨询 Microsoft 解决方案提供商之一。 解决方案提供商提供各种基于费用的服务,包括创建自定义宏。 有关 Microsoft 解决方案提供商的信息,请拨打 426-9400 (800) Microsoft 客户信息服务。

示例

  1. 将以下代码键入到新模块中:

    ' Access the GetCursorPos function in user32.dll
    Declare Function GetCursorPos Lib "user32" _
    (lpPoint As POINTAPI) As Long
    ' Access the GetCursorPos function in user32.dll
    Declare Function SetCursorPos Lib "user32" _
    (ByVal x As Long, ByVal y As Long) As Long

    ' GetCursorPos requires a variable declared as a custom data type
    ' that will hold two integers, one for x value and one for y value
    Type POINTAPI
    X_Pos As Long
    Y_Pos As Long
    End Type

    ' Main routine to dimension variables, retrieve cursor position,
    ' and display coordinates
    Sub Get_Cursor_Pos()

    ' Dimension the variable that will hold the x and y cursor positions
    Dim Hold As POINTAPI

    ' Place the cursor positions in variable Hold
    GetCursorPos Hold

    ' Display the cursor position coordinates
    MsgBox "X Position is : " & Hold.X_Pos & Chr(10) & _
    "Y Position is : " & Hold.Y_Pos
    End Sub

    ' Routine to set cursor position
    Sub Set_Cursor_Pos()

    ' Looping routine that positions the cursor
    For x = 1 To 480 Step 20
    SetCursorPos x, x
    For y = 1 To 40000: Next
    Next x
    End Sub
  2. 单击例程文本内Get_Cursor_Pos任意位置,然后按 F5 键运行Get_Cursor_Pos宏。

    将显示一个消息框,其中显示了鼠标指针当前位置的坐标。

  3. 单击例程文本中的Set_Cursor_Pos,然后按 F5 键运行Set_Cursor_Pos宏。

光标将在屏幕上对角线向下移动。

需要更多帮助?

需要更多选项?

了解订阅权益、浏览培训课程、了解如何保护设备等。

社区可帮助你提出和回答问题、提供反馈,并听取经验丰富专家的意见。

此信息是否有帮助?

你对语言质量的满意程度如何?
哪些因素影响了你的体验?
按“提交”即表示你的反馈将用于改进 Microsoft 产品和服务。 你的 IT 管理员将能够收集此数据。 隐私声明。

谢谢您的反馈!

×