这里用python中的pywin32
Pywin32提供了很多访问windows的AP
安装pywin32会包含很多小模块win32ui,win32gui,win32con,win32process等等
参考手册添加链接描述
pip install pywin32
DC在pywin32中是一个重要概念。windows不允许程序直接访问硬件,所有的操作都需要通过一个设备上下文环境。屏幕上的每个窗口都对应一个DC。DC相当于一个视频缓冲区,对这个缓冲区的操作,会表现在这个缓冲区对应的屏幕窗口上。
1、遍历窗口
import win32con, win32ui, win32gui,win32api, win32process
import psutil
import os
class Ergodic():
def __init__(self):
self.hwnd_title = dict()
def get_all_hwnd(self, hwnd, mouse):
#if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
self.hwnd_title.update({hwnd: win32gui.GetWindowText(hwnd)+';'+win32gui.GetClassName(hwnd)})
'''def get_all_hwnd1(self, hwnd, mouse):
# if win32gui.IsWindow(hwnd) and win32gui.IsWindowEnabled(hwnd) and win32gui.IsWindowVisible(hwnd):
if win32gui.IsWindowVisible(hwnd) and win32gui.IsWindowEnabled(hwnd):
self.windows.update({hwnd: win32gui.GetWindowText(hwnd) + ';' + win32gui.GetClassName(hwnd)})
def get_child_window(self,hwnd):
self.windows = dict()
win32gui.EnumChildWindows(hwnd, self.get_all_hwnd1, self.windows)
print(self.windows)
return self.windows'''
def __call__(self, *args, **kwargs):
win32gui.EnumWindows(self.get_all_hwnd, 0)
for h, t in self.hwnd_title.items():
if t is not "":
print(h, t)
return self.hwnd_title
if __name__ == '__main__':
ergo =Ergodic()
ergo()
2.查找窗口、截图、获取进程ID,结合psutil获取进程详细信息、杀死进程
import win32con, win32ui, win32gui,win32api, win32process
import psutil
import os
import time
from PyQt5.QtWidgets import QApplication
from PyQt5.QtGui import *
def get_windows(windowsname,filename):
# 获取窗口句柄,如果你的输入参数是handle的话直接跳过这一步
handle = win32gui.FindWindow(None,windowsname)
#获取窗口进程
threadpid, procpid = win32process.GetWindowThreadProcessId(handle)
pp = psutil.Process(procpid)
#可以参考psutil获取进程详细信息
print(pp.exe(),pp.name(),pp.status())
# 将窗口放在前台,并激活该窗口(窗口不能最小化)
#win32gui.SetForegroundWindow(handle)
# 获取窗口DC
hdDC = win32gui.GetWindowDC(handle)
# 根据句柄创建一个DC
newhdDC = win32ui.CreateDCFromHandle(hdDC)
# 创建一个兼容设备内存的DC
saveDC = newhdDC.CreateCompatibleDC()
# 创建bitmap保存图片
saveBitmap = win32ui.CreateBitmap()
# 获取窗口的位置信息
left, top, right, bottom = win32gui.GetWindowRect(handle)
# 窗口长宽
width = right - left
height = bottom - top
# bitmap初始化
saveBitmap.CreateCompatibleBitmap(newhdDC, width, height)
saveDC.SelectObject(saveBitmap)
saveDC.BitBlt((0, 0), (width, height), newhdDC, (0, 0), win32con.SRCCOPY)
saveBitmap.SaveBitmapFile(saveDC, filename)
#释放内存
win32gui.DeleteObject(saveBitmap.GetHandle())
saveDC.DeleteDC()
#杀死该进程及关闭窗口
os.popen('taskkill.exe /F /pid:' + str(procpid))
def screenshot(handle, filename):
app = QApplication(sys.argv)
screen = QApplication.primaryScreen()
img = screen.grabWindow(handle).toImage()
img.save(filename)
def get_window_from_cursor():
prevWindow = None
curX, curY = win32gui.GetCursorPos()
handle = win32gui.WindowFromPoint((curX, curY))
prevWindow = handle
window_name = win32gui.GetWindowText(handle)
r = get_windowsinfo(window_name)
print(r)
hightligt_window(handle)
screenshot(handle,'pp.png')
refreshWindow(handle)
def kill_pro(handle):
threadpid, procpid = win32process.GetWindowThreadProcessId(handle)
os.popen('taskkill.exe /F /pid:' + str(procpid))
#杀死该进程及关闭窗口
def hightligt_window(handle):
left, top, right, bottom = win32gui.GetWindowRect(handle)
rectanglePen = win32gui.CreatePen(win32con.PS_SOLID, 3, win32api.RGB(255, 0, 0))
windowDc = win32gui.GetWindowDC(handle)
if windowDc:
prevPen = win32gui.SelectObject(windowDc, rectanglePen)
prevBrush = win32gui.SelectObject(windowDc, win32gui.GetStockObject(win32con.HOLLOW_BRUSH))
win32gui.Rectangle(windowDc, 0, 0, right - left, bottom - top)
win32gui.SelectObject(windowDc, prevPen)
win32gui.SelectObject(windowDc, prevBrush)
win32gui.ReleaseDC(handle, windowDc)
#刷新窗口的函数:
def refreshWindow(handle):
win32gui.InvalidateRect(handle, None, True)
win32gui.UpdateWindow(handle)
win32gui.RedrawWindow(handle,
None,
None,
win32con.RDW_FRAME|
win32con.RDW_INVALIDATE|
win32con.RDW_UPDATENOW|
win32con.RDW_ALLCHILDREN)