0
点赞
收藏
分享

微信扫一扫

Python监视用户计算机桌面窗口焦点的变化情况


代码运行后,可以实时监视用户计算机桌面上拥有焦点的窗口,如果焦点有切换就会给出提示。

from ctypes import *
from time import sleep
from datetime import datetime
#方便调用Windows底层API函数
user32 = windll.user32
kernel32 = windll.kernel32
psapi = windll.psapi
#实时查看当前窗口
def getProcessInfo():
    global windows
    #获取当前位于桌面最顶端的窗口句柄
    hwnd = user32.GetForegroundWindow()
    pid = c_ulong(0)
    #获取进程ID
    user32.GetWindowThreadProcessId(hwnd, byref(pid))
    processId = str(pid.value)
    #获取可执行文件名称
    executable = create_string_buffer(512)
    h_process = kernel32.OpenProcess(0x400|0x10, False, pid)
    psapi.GetModuleBaseNameA(h_process, None, byref(executable), 512)
    #获取窗口标题
    windowTitle = create_string_buffer(512)
    user32.GetWindowTextA(hwnd, byref(windowTitle), 512)
    #关闭句柄
    kernel32.CloseHandle(hwnd)
    kernel32.CloseHandle(h_process)
    #更新最近两个窗口的信息
    windows.pop(0)
    windows.append([executable.value.decode('gbk'),windowTitle.value.decode('gbk')])
def main():
    global windows
    windows = [None, None]
    while True:
        getProcessInfo()
        #如果用户切换窗口则进行提示
        if windows[0] != windows[1]:
            print('='*30)
            print(str(datetime.now())[:19],windows[0],'==>',windows[1])
        sleep(0.2)
if __name__ == '__main__':
    main()

部分运行结果如下:

2016-08-14 09:12:52 ['Explorer.EXE', ''] ==> ['pythonw.exe', 'focusSwitch.py - C:/Python35/focusSwitch.py (3.5.1)']
==============================
2016-08-14 09:12:56 ['pythonw.exe', 'focusSwitch.py - C:/Python35/focusSwitch.py (3.5.1)'] ==> ['QQBrowser.exe', '微信公众平台']
==============================
2016-08-14 09:13:11 ['QQBrowser.exe', '微信公众平台'] ==> ['Explorer.EXE', '']
==============================
2016-08-14 09:13:12 ['Explorer.EXE', ''] ==> ['pythonw.exe', '*Python 3.5.1 Shell*']
==============================
2016-08-14 09:13:22 ['pythonw.exe', '*Python 3.5.1 Shell*'] ==> ['Explorer.EXE', '']
==============================
2016-08-14 09:13:32 ['Explorer.EXE', ''] ==> ['pythonw.exe', '*Python 3.5.1 Shell*']

“Python小屋”进入公众号,关注后可以查看更多内容!

欢迎转发给您的朋友,或许这正是Ta需要的知识!

举报

相关推荐

0 条评论