使用 Microsoft 登入
登入或建立帳戶。
您好:
選取其他帳戶。
您有多個帳戶
選擇您要用來登入的帳戶。

摘要

您可以撥打 Windows API (程式設計介面) 連至 Microsoft Windows DLL (動態連結文件庫) 以取得並設定目前的游標位置。 您可以使用 GetCursorPos 函數來取得目前USER32.DLL。

其他相關資訊

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 管理員將能夠收集這些資料。 隱私權聲明。

感謝您的意見反應!

×