PYTHON鼠标事件

来源:互联网 发布:4g网络优化先培训多久 编辑:程序博客网 时间:2024/06/05 15:49
'''
Created on 2011-11-6

@author: admin
'''
import subprocess
import win32ui,win32con,pythoncom,win32gui,win32process,win32api
import time
import string

def click(handle, pos):
    client_pos = win32gui.ScreenToClient(handle, pos)
    tmp = win32api.MAKELONG(client_pos[0], client_pos[1])
    win32gui.SendMessage(handle, win32con.WM_ACTIVATE, win32con.WA_ACTIVE, 0)
    win32api.SendMessage(handle, win32con.WM_LBUTTONDOWN, win32con.MK_LBUTTON, tmp)
    win32api.SendMessage(handle, win32con.WM_LBUTTONUP, win32con.MK_LBUTTON, tmp)

def get_curpos():
    return win32gui.GetCursorPos()

def get_win_handle(pos):
    return win32gui.WindowFromPoint(pos)

def get_color(pos):
    hdc_screen = win32gui.CreateDC("DISPLAY", "", None)
    hmem_dc = win32gui.CreateCompatibleDC(hdc_screen)
    h_bitmap = win32gui.CreateCompatibleBitmap(hdc_screen, 1, 1)
    h_old_bitmap = win32gui.SelectObject(hmem_dc, h_bitmap)
    win32gui.BitBlt(hmem_dc, 0, 0, 1, 1, hdc_screen, pos[0], pos[1], win32con.SRCCOPY)
    win32gui.DeleteDC(hdc_screen)
    win32gui.DeleteDC(hmem_dc)
    x = win32ui.CreateBitmapFromHandle(h_bitmap)
    bits = x.GetBitmapBits(True)   

def main():
    print "Starting"
    while True:
        time.sleep(60*6)
        click(get_win_handle(get_curpos()),get_curpos())
if __name__ == '__main__':
    main()
原创粉丝点击