我在 python 脚本中使用 ctypes lib 时遇到问题。这是我的代码(在互联网上找到):
if __name__ == "__main__":
from ctypes import *
user32 = windll.user32
kernel32 = windll.kernel32
class RECT(Structure):
_fields_ = [
("left", c_ulong),
("top", c_ulong),
("right", c_ulong),
("bottom", c_ulong)];
class GUITHREADINFO(Structure):
_fields_ = [
("cbSize", c_ulong),
("flags", c_ulong),
("hwndActive", c_ulong),
("hwndFocus", c_ulong),
("hwndCapture", c_ulong),
("hwndMenuOwner", c_ulong),
("hwndMoveSize", c_ulong),
("hwndCaret", c_ulong),
("rcCaret", RECT)
]
def moveCursorInCurrentWindow(x, y):
# Find the focussed window.
guiThreadInfo = GUITHREADINFO(cbSize=sizeof(GUITHREADINFO))
user32.GetGUIThreadInfo(0, byref(guiThreadInfo))
focussedWindow = guiThreadInfo.hwndFocus
# Find the screen position of the window.
windowRect = RECT()
user32.GetWindowRect(focussedWindow, byref(windowRect))
# Finally, move the cursor relative to the window.
user32.SetCursorPos(windowRect.left + x, windowRect.top + y)
if __name__ == '__main__':
# Quick test.
moveCursorInCurrentWindow(100, 100)
第一个问题是 python 找不到 ctypes,所以我将从项目站点下载的文件复制到
netbeans\6.9\jython-2.5.1\Lib\
(是的,我使用的是netbeans)然后它显示了这个错误:
> from ctypes import *
> File "C:\Users\k\.netbeans\6.9\jython-2.5.1\Lib\ctypes\__init__.py", line 10, in <module>
> from _ctypes import Union, Structure, Array
就像 init 文件有一些错误 o_O 帮助大家!
问候,克里斯