import ctypes
from ctypes import wintypes
import pyautogui
user32 = ctypes.WinDLL('user32', use_last_error=True)
# 注册user32中的GetDC参数和返回值
user32.GetDC.restype = wintypes.HDC
user32.GetDC.argtypes = (wintypes.HWND,)
gdi32 = ctypes.WinDLL('gdi32', use_last_error=True)
# 注册GetPixel中的GetPixel参数和返回值
gdi32.GetPixel.restype = wintypes.COLORREF
gdi32.GetPixel.argtypes = (wintypes.HDC, ctypes.c_int, ctypes.c_int)
def getScreenColor(self):
# 获取当前鼠标坐标
x, y = pyautogui.position()
hdc = user32.GetDC(None)
color = gdi32.GetPixel(hdc, x, y)
r = color & 0xFF
g = color >> 8 & 0xFF
b = color >> 16 & 0xFF
return (r, g, b)