这次增加了鼠标点所在的颜色获取~
from ctypes import *
import win32api, win32con
import time
#装载windows dll
User32dll = windll.User32
gdi32dll = windll.gdi32
print windll.User32
print windll.gdi32
"""
主要学习
1 python byref关键字,送数据结构指针空间的关键字
2 windows api:
相关细节内容查阅:
http://msdn.microsoft.com/en-us/library/
GetCursorPos(x,y)
SetCursorPos(x,y)
GetDC()
GetPixel()
"""
class POINT(Structure):
_fields_ = [
("x", c_ulong),
("y", c_ulong)
]
def timer_Tick():
point = POINT()
User32dll.GetCursorPos(byref(point))
print 'current Pos:', point.x, point.y
return point.x, point.y
def get_Color(x, y):
#获取句柄
hdc = User32dll.GetDC(None)
#获取指定像素的颜色
color = gdi32dll.GetPixel(hdc, x, y)
#打印十进制转化为十六进制的颜色
print hex(color)
red = (color & 0xFF) #转换红色
green = (color >> 4 & 0xFF) #转换绿色
blue = (color >> 8 & 0xFF) #转换蓝色
print "red:", red, "green:", green, "blue:", blue
return color
if __name__ == '__main__':
i = 0
maxcount = 10
while i < maxcount:
x,y = timer_Tick()
get_Color(x,y)
time.sleep(1)
i += 1