然后放到后台测试了一下延迟,发现还是蛮快的,所以就发出来供大家参考学习,开发出更多用法。
[Python] 纯文本查看 复制代码import ctypesimport time
import win32api
import win32con
import win32gui
from ctypes import wintypes
# Windows API 常量
DWM_TNP_RECTDESTINATION = 0x00000001
DWM_TNP_VISIBLE = 0x00000008
DWM_TNP_SOURCECLIENTAREAONLY = 0x00000010
# 定义 RECT 结构体
class RECT(ctypes.Structure):
_fields_ = [("left", wintypes.LONG),
("top", wintypes.LONG),
("right", wintypes.LONG),
("bottom", wintypes.LONG)]
# DWM_THUMBNAIL_PROPERTIES 结构体
class DWM_THUMBNAIL_PROPERTIES(ctypes.Structure):
_fields_ = [("dwFlags", wintypes.DWORD),
("rcDestination", RECT),
("fVisible", wintypes.BOOL),
("fSourceClientAreaOnly", wintypes.BOOL),
("opacity", wintypes.UINT)]
# 定义 HRESULT 类型
HRESULT = ctypes.c_int
# DWM API 函数声明
DWM_API = ctypes.windll.dwmapi
# DwmRegisterThumbnail 函数声明
DWM_API.DwmRegisterThumbnail.argtypes = [wintypes.HWND, wintypes.HWND, ctypes.POINTER(wintypes.HANDLE)]
DWM_API.DwmRegisterThumbnail.restype = HRESULT # 使用 c_int 来表示 HRESULT
# DwmUpdateThumbnailProperties 函数声明
DWM_API.DwmUpdateThumbnailProperties.argtypes = [wintypes.HANDLE, ctypes.POINTER(DWM_THUMBNAIL_PROPERTIES)]
DWM_API.DwmUpdateThumbnailProperties.restype = HRESULT
# 定义窗口回调函数
def wnd_proc(hwnd, msg, w_param, l_param):
if msg == win32con.WM_CLOSE:
win32gui.DestroyWindow(hwnd)
# DWM_API.DwmUnregisterThumbnail(hwnd)
return win32gui.DefWindowProc(hwnd, msg, w_param, l_param)
# 创建窗口函数
def create_window(window_title, w, h):
wc = win32gui.WNDCLASS()
wc.lpfnWndProc = wnd_proc
wc.lpszClassName = window_title
wc.hInstance = win32api.GetModuleHandle(None)
class_atom = win32gui.RegisterClass(wc)
hwnd = win32gui.CreateWindow(
class_atom, # 窗口类的标识符
window_title, # 窗口标题
win32con.WS_OVERLAPPEDWINDOW, # 窗口样式
0, 0, # 窗口位置
w + 50, h + 50, # 窗口大小
0, # 父窗口句柄
0, # 菜单句柄
wc.hInstance, # 实例句柄
None # 额外的参数
)
return hwnd
def mapping_window(target_hwnd, source_hwnd, draw_position):
thumbnail = wintypes.HANDLE()
# 注册缩略图
result = DWM_API.DwmRegisterThumbnail(target_hwnd, source_hwnd, ctypes.byref(thumbnail))
if result != 0: # 如果 DwmRegisterThumbnail 返回非 0,表示失败
print("未获取到缩略图")
return
# 设置缩略图属性
thumb_props = DWM_THUMBNAIL_PROPERTIES()
thumb_props.dwFlags = DWM_TNP_RECTDESTINATION
thumb_props.rcDestination = draw_position
thumb_props.fVisible = True
thumb_props.fSourceClientAreaOnly = False
thumb_props.opacity = (255 * 70) // 100 # 设置透明度为 70%
# 更新缩略图属性
DWM_API.DwmUpdateThumbnailProperties(thumbnail, ctypes.byref(thumb_props))
print("缩略图已映射到目标窗口")
def get_window_normal_size(hwnd):
# 获取窗口的位置信息和状态
placement = win32gui.GetWindowPlacement(hwnd)
print(placement)
# 获取窗口正常状态下的尺寸
rect = placement[4] # rcNormalPosition: (left, top, right, bottom)
# 计算宽度和高度
width = rect[2] - rect[0]
height = rect[3] - rect[1]
return width, height
# 获取源窗口的句柄
source_hwnd = 198072 # 这里填写需要截图的窗口的句柄
width, height = get_window_normal_size(source_hwnd)
print(width, height)
# 创建目标窗口
target_window_title = "Target Window" # 目标窗口的标题
target_hwnd = create_window(target_window_title, width, height)
# 显示目标窗口
win32gui.ShowWindow(target_hwnd, win32con.SW_SHOWNORMAL)
win32gui.UpdateWindow(target_hwnd)
# 定义绘制位置(你可以根据需要调整)
draw_position = RECT()
draw_position.left = 0
draw_position.top = 0
draw_position.right = draw_position.left + width
draw_position.bottom = draw_position.top + height
# 调用映射窗口函数
mapping_window(target_hwnd, source_hwnd, draw_position)
# 运行消息循环以保持窗口显示
while True:
win32gui.PumpMessages()
time.sleep(0.1)