python 实现windows平台获取鼠标颜色

这次增加了鼠标点所在的颜色获取~

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


 

  • 2
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值