pywin32获取窗口截图

import win32gui
import win32ui
import win32con
import win32api

# 获取窗口句柄
hwnd = win32gui.FindWindow(None, '窗口标题')


# 获取窗口位置和大小
left, top, right, bottom = win32gui.GetWindowRect(hwnd)

# 获取窗口DC
hdc = win32gui.GetWindowDC(hwnd)

# 创建一个位图DC,并将其与窗口DC相关联
bmp_dc = win32ui.CreateDCFromHandle(hdc)
mem_dc = bmp_dc.CreateCompatibleDC()
bitmap = win32ui.CreateBitmap()
bitmap.CreateCompatibleBitmap(bmp_dc, right - left, bottom - top)
mem_dc.SelectObject(bitmap)

# 将窗口内容复制到位图DC中
mem_dc.BitBlt((0, 0), (right - left, bottom - top), bmp_dc, (0, 0), win32con.SRCCOPY)

# 保存位图到文件
bitmap.SaveBitmapFile(mem_dc, '666666.png')

# 释放资源
mem_dc.DeleteDC()
win32gui.ReleaseDC(hwnd, hdc)
win32gui.DeleteObject(bitmap.GetHandle())

  

posted @ 2023-03-27 13:23  疯陈演义  阅读(575)  评论(0编辑  收藏  举报