python-3.x - 使用 ctypes 获取可用工作区

标签 python-3.x ctypes user32

我的目标是在 ctypes 和 user32.dll ( SystemParametersInfo ) 的帮助下获得 python 中的可用工作区。嗯,我是第二次使用ctypes,不太理解。如果能解释一下我做错了什么就太好了。谢谢。

import ctypes

SPI_GETWORKAREA = 48
SPI = ctypes.windll.user32.SystemParametersInfoA

class RECT(ctypes.Structure):
    _fields_ = [
        ('left', ctypes.c_long),
        ('top', ctypes.c_long),
        ('right', ctypes.c_long),
        ('bottom', ctypes.c_long)
    ]

SPI.restype = ctypes.POINTER(RECT)
SPI.argtypes = [ctypes.c_uint, ctypes.c_uint, ctypes.c_uint]
result = SPI(SPI_GETWORKAREA, 0, 0)
print(result)

最佳答案

您有一些错误:

  • Python 3 使用 unicode 字符串,因此请使用 SystemParametersInfoW(尽管对于此特定函数,它可能以任何一种方式工作)。
  • 函数调用需要 4 个参数,而不是 3 个。
  • 返回类型是 bool 值,而不是矩形结构。
  • 您需要访问函数调用后传入的相同 RECT 结构。函数调用会就地修改它。

合并这些更改,代码对我有用:

import ctypes


SPI_GETWORKAREA = 48
SPI = ctypes.windll.user32.SystemParametersInfoW


class RECT(ctypes.Structure):
    _fields_ = [
        ('left', ctypes.c_long),
        ('top', ctypes.c_long),
        ('right', ctypes.c_long),
        ('bottom', ctypes.c_long)
    ]


SPI.restype = ctypes.c_bool
SPI.argtypes = [
    ctypes.c_uint,
    ctypes.c_uint,
    ctypes.POINTER(RECT),
    ctypes.c_uint
]

rect = RECT()

result = SPI(
    SPI_GETWORKAREA,
    0, 
    ctypes.byref(rect),
    0
)
if result:
    print('it worked!')
    print(rect.left)
    print(rect.top)
    print(rect.right)
    print(rect.bottom)

关于python-3.x - 使用 ctypes 获取可用工作区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49371540/

相关文章:

c# - 使用 RegisterHotKey 通过 Numpad 注册热键

python - 日期列表中的时间序列-python

python - 如何将多个列表写入文本文件中各自的列中?

python - 如何将包含多个文件的 Python 程序编译为 mac?

pip - 找不到满足 ctypes 要求的版本(来自版本 : ) No matching distribution found for ctypes

c# - WinForms 等效于 WPF WindowInteropHelper、HwndSource、HwndSourceHook

c# - 如何获取应用程序的事件 ChildWindow?

python - pip : was OK for months now :ImportError: No module named 'pip._vendor.requests.adapters'

python - 清理 python 切片?

javascript - Mozilla ctypes,从 c 数组提供 Arraybuffer