我在使用代码时得到一个error('GetPixel', 'No error message is available')
:color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x , y)
。
据我了解,每次使用GetPixel时,我应该释放DC(win32gui.ReleaseDC(,))。但是,我应该使用什么作为窗口的句柄和设备上下文的句柄?
据我了解,我可以通过使用win32gui.GetActiveWindow()来获取窗口的句柄,对吗?但是我不知道如何获取设备上下文的句柄。
代码:
def get_position_and_color():
a=0
while a<=3:
sleep(1)
x, y = win32api.GetCursorPos()
color = win32gui.GetPixel(win32gui.GetDC(win32gui.GetActiveWindow()), x , y)
print(str(a)+' '+str(x)+','+str(y)+','+str(color))
# print(a)
a=a+1
get_position_and_color()
感谢您的帮助@AKX。我想我找到了答案。我在VBA中具有相同的代码,但永远不会失败,但是在python中...我只是比较了两个代码。VBA:
lDC = GetWindowDC(0)致电GetCursorPos(pLocation)lColour = GetPixel(lDC,pLocation.x,pLocation.y)Debug.Print(i&“ =”&pLocation.x&“ =”&pLocation.y&“ =”&lColour&“ =”)
Python(请参见上文)我使用win32gui.GetActiveWindow()而不是“ 0”。我猜想坐标超出了“活动窗口”的范围。