|
不包含截屏提示在截图中:
- import ctypes
- import win32gui
- import win32con
- import time
- import cv2
-
- # 弹出提示窗口的函数
- def show_toast(message, duration=5):
- # 创建一个简单的弹出窗口
- win32gui.MessageBox(0, message, "截屏提示", win32con.MB_ICONINFORMATION)
-
- # 触发截图的函数
- def take_screenshot():
- # 获取屏幕尺寸
- screen = ctypes.windll.user32.GetDesktopWindow()
- screen_width = ctypes.windll.user32.GetSystemMetrics(0)
- screen_height = ctypes.windll.user32.GetSystemMetrics(1)
-
- # 创建设备上下文
- hdc = ctypes.windll.user32.GetWindowDC(screen)
- hdc_driver = ctypes.windll.gdi32.CreateCompatibleDC(hdc)
- hbitmap = ctypes.windll.gdi32.CreateCompatibleBitmap(hdc, screen_width, screen_height)
- hold_bitmap = ctypes.c_long()
- ctypes.windll.gdi32.SelectObject(hdc_driver, hbitmap)
-
- # 复制屏幕到位图
- ctypes.windll.gdi32.BitBlt(hdc_driver, 0, 0, screen_width, screen_height, hdc, 0, 0, 13369376)
-
- # 保存位图到文件
- bmp_info = ctypes.wintypes.BITMAPINFO()
- bmp_info.bmiHeader.biSize = ctypes.sizeof(bmp_info.bmiHeader)
- status = ctypes.windll.gdi32.GetDIBits(hdc_driver, hbitmap, 0, screen_height, None, ctypes.byref(bmp_info), win32con.DIB_RGB_COLORS)
-
- # 释放资源
- ctypes.windll.gdi32.DeleteObject(hbitmap)
- ctypes.windll.gdi32.DeleteDC(hdc_driver)
- ctypes.windll.user32.ReleaseDC(screen, hdc)
-
- # 读取截图数据到Python图像库
- img = cv2.imdecode(img_array, cv2.IMREAD_COLOR)
-
- # 保存图像到文件
- cv2.imwrite('screenshot.png', img)
-
- # 调用函数,触发截图并显示提示窗口
- show_toast("注意截屏!")
- time.sleep(1) # 等待提示窗口显示完成
- take_screenshot()
复制代码
这个实现是最原始的复杂的,下面还有一个实现比较简单(用到Python第三方库) |
|