【问题标题】:Detect if the mouse cursor is hidden or visible by any other application检测鼠标光标是否被任何其他应用程序隐藏或可见
【发布时间】:2012-09-17 21:04:33
【问题描述】:

我想检测鼠标当前是否隐藏,这通常由 Windows 上的 3D 应用程序完成。这似乎比听起来更棘手,因为我找不到任何方法来做到这一点。

最好我想用 Python 来做这件事,但如果那不可能,我可以求助于 C。谢谢!

【问题讨论】:

    标签: python c windows mouse


    【解决方案1】:

    GetCursorInfo 函数返回一个CURSORINFO 结构,该结构有一个包含全局游标状态的flags 字段。这会做你需要的吗?我对Python不熟悉,不知道能不能从Python调用这个函数。

    【讨论】:

    【解决方案2】:

    您需要调用GetCursorInfo 函数。这可以使用pywin32 library 直接完成。或者,如果您不想安装外部 Python 库,可以使用 ctypes module 直接从 User32.dll 访问该函数。

    例子:

    import ctypes
    
    # Argument structures
    class POINT(ctypes.Structure):
        _fields_ = [('x', ctypes.c_int),
                    ('y', ctypes.c_int)]
    
    class CURSORINFO(ctypes.Structure):
        _fields_ = [('cbSize', ctypes.c_uint),
                    ('flags', ctypes.c_uint),
                    ('hCursor', ctypes.c_void_p),
                    ('ptScreenPos', POINT)]
    
    # Load function from user32.dll and set argument types
    GetCursorInfo = ctypes.windll.user32.GetCursorInfo
    GetCursorInfo.argtypes = [ctypes.POINTER(CURSORINFO)]
    
    # Initialize the output structure
    info = CURSORINFO()
    info.cbSize = ctypes.sizeof(info)
    
    # Call it
    if GetCursorInfo(ctypes.byref(info)):
        if info.flags & 0x00000001:
            pass  # The cursor is showing
    else:
        pass  # Error occurred (invalid structure size?)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-06-04
      • 1970-01-01
      • 2011-05-24
      • 1970-01-01
      • 2014-07-19
      • 2017-04-05
      相关资源
      最近更新 更多