如何在Windows系统中使用Python获取当前光标状态(WaitCursor等)
Hey there! Since you know how to use Cursor.Current in C# to get the current cursor state (like WaitCursor, Arrow, Default, etc.) on Windows, let's replicate that functionality in Python. Here's a practical solution using Windows API calls via the ctypes module, which lets us interact directly with the Windows system libraries just like C# does under the hood.
How to Get Current Cursor State in Windows with Python
Implementation Code
First, we'll use ctypes to call the necessary Windows APIs, define the required data structures, and map standard cursor types to readable names:
import ctypes from ctypes import wintypes # Define the CURSORINFO structure as required by Windows API class CURSORINFO(ctypes.Structure): _fields_ = [ ("cbSize", wintypes.DWORD), ("flags", wintypes.DWORD), ("hCursor", wintypes.HCURSOR), ("ptScreenPos", wintypes.POINT) ] # Load the user32.dll library (contains Windows UI-related functions) user32 = ctypes.WinDLL('user32', use_last_error=True) # Set up function prototypes for type safety user32.GetCursorInfo.argtypes = [ctypes.POINTER(CURSORINFO)] user32.GetCursorInfo.restype = wintypes.BOOL user32.LoadCursorW.argtypes = [wintypes.HINSTANCE, wintypes.LPCWSTR] user32.LoadCursorW.restype = wintypes.HCURSOR # Standard cursor constants pulled from winuser.h IDC_ARROW = 32512 IDC_IBEAM = 32513 IDC_WAIT = 32514 IDC_CROSS = 32515 IDC_HAND = 32649 IDC_APPSTARTING = 32650 IDC_HELP = 32651 IDC_NO = 32648 IDC_SIZEALL = 32646 def get_current_cursor_type(): """Retrieve the current cursor state as a human-readable string""" cursor_info = CURSORINFO() cursor_info.cbSize = ctypes.sizeof(CURSORINFO) # Must set this before calling GetCursorInfo # Fetch current cursor data if not user32.GetCursorInfo(ctypes.byref(cursor_info)): raise ctypes.WinError(ctypes.get_last_error()) # Load handles for standard cursors to compare against standard_cursors = { user32.LoadCursorW(None, IDC_ARROW): "Arrow/Default", user32.LoadCursorW(None, IDC_WAIT): "WaitCursor", user32.LoadCursorW(None, IDC_IBEAM): "IBeam", user32.LoadCursorW(None, IDC_HAND): "Hand", user32.LoadCursorW(None, IDC_APPSTARTING): "AppStarting", user32.LoadCursorW(None, IDC_HELP): "Help", user32.LoadCursorW(None, IDC_NO): "No", user32.LoadCursorW(None, IDC_SIZEALL): "SizeAll" } # Return the matching cursor name, or "Unknown" if it's a custom cursor return standard_cursors.get(cursor_info.hCursor, "Unknown Cursor") # Example usage if __name__ == "__main__": print(f"Current cursor: {get_current_cursor_type()}")
How It Works
CURSORINFOStructure: This holds information about the cursor, including its handle (hCursor) and screen position. We have to setcbSizeto the size of the structure forGetCursorInfoto work correctly.GetCursorInfo: Retrieves the current cursor's data from the system.LoadCursorW: Loads the handle for a standard Windows cursor. We compare this handle against the one fromGetCursorInfoto identify the cursor type.- Standard Cursor Mapping: We map common cursor handles to human-readable names, just like how C#'s
Cursor.Currentexposes cursor types.
Important Notes
- This code only works on Windows, since it relies on Windows-specific APIs.
- Custom cursors (used by some apps) will return "Unknown Cursor" because their handles don't match standard system cursors. If you need to identify custom cursors, you'd need additional logic to read cursor properties, which is more complex.
- Always handle errors properly (the code uses
ctypes.WinErrorto raise meaningful Windows error messages if something goes wrong).
内容的提问来源于stack exchange,提问作者imriqwe
相关产品推荐
相关产品推荐

