摘要
您可以讓 Microsoft Windows DLL (動態連結程式庫),來取得及設定目前的游標位置呼叫 Windows API (應用程式發展介面)。在 USER32 中使用 GetCursorPos 函式,可取得目前的位置。DLL。
其他相關資訊
Microsoft Excel 並沒有內建的功能,要取得或設定游標的位置。不過,您可以使用 Declare 陳述式在 Microsoft Excel Visual Basic for Applications 巨集來呼叫 Windows 函式來存取目前的位置。您也可以使用另一個函式 SetCursorPos,若要設定游標的位置。SetCursorPos 函式可以用於迴圈的結構,來移動游標在螢幕上。Microsoft 提供的範例 Visual Basic for Applications 程序僅供說明,不做任何明示或默示的保證,其中包括,但不是限於適售性以及適合某特定用途之默示擔保責任。本文中的 Visual Basic 程序會顯示一些 ' is',Microsoft 不保證,它可用於所有情況。雖然 Microsoft 技術支援工程師可以協助說明特定的巨集的功能,不會修改這些範例以提供附加的功能,也不會協助您建構巨集來滿足您的特定需求。如果您有限制的程式設計經驗,您可能想要參閱 Microsoft 解決方案提供者之一。解決方案提供者會提供各種付費服務,包括建立自訂巨集。如需有關 Microsoft 解決方案提供者的詳細資訊,呼叫 (800) 426-9400 的 Microsoft 客戶資訊服務。
範例
-
輸入新的模組中的下列程式碼:
' 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
-
Get_Cursor_Pos 常式的文字內部任意處按一下,並按下 F5 鍵以執行 Get_Cursor_Pos 巨集。就會出現訊息方塊顯示目前的滑鼠指標位置的座標。
-
Set_Cursor_Pos 常式的文字內部任意處按一下,並按下 F5 鍵以執行 Set_Cursor_Pos 巨集。
游標將會下移以對角方式在螢幕上。