|
|

分享源码
| 界面截图: |
|
| 是否带模块: |
纯源码 |
| 备注说明: |
- |
本帖最后由 花老板 于 2026-5-1 05:40 编辑
=============================================
ScreenCaptureDLL - 32位截图动态库
=============================================
方案5:PrintWindow + PW_RENDERFULLCONTENT
一、API说明
-----------
1. PrintWindow
- 函数:BOOL PrintWindow(HWND hwnd, HDC hdcBlt, UINT nFlags)
- 作用:向窗口发送WM_PRINT/WM_PRINTCLIENT消息
- 返回:成功返回TRUE,失败返回FALSE
2. PW_RENDERFULLCONTENT
- 值:0x00000002
- 支持:Win8.1+
- 作用:强制窗口绘制完整内容,包括硬件加速渲染的内容
二、工作流程
-----------
1. GetWindowRect() - 获取窗口位置和大小
2. GetWindowDC() - 获取窗口DC
3. CreateCompatibleDC() - 创建兼容内存DC
4. CreateCompatibleBitmap() - 创建兼容位图
5. SelectObject() - 选入内存DC
6. PrintWindow(hWnd, hMemDC, PW_RENDERFULLCONTENT) - 核心!
7. SaveBitmapToFile() - 保存位图
三、动态库导出函数
----------------
1. BOOL CaptureRegion(int x, int y, int x1, int y1, const char* path)
- 功能:屏幕区域截图
- 参数:
x, y - 左上角坐标
x1, y1 - 右下角坐标
path - 保存路径(如 "capture.bmp")
- 返回:成功返回TRUE,失败返回FALSE
2. BOOL CaptureWindow(HWND hWnd, const char* path)
- 功能:窗口截图(方案5方法)
- 参数:
hWnd - 目标窗口句柄
path - 保存路径
- 返回:成功返回TRUE,失败返回FALSE
四、调用示例
-----------
C++示例:
[C++] 纯文本查看 复制代码 #include <windows.h>
#include <iostream>
typedef BOOL(*CaptureRegionFn)(int, int, int, int, const char*);
typedef BOOL(*CaptureWindowFn)(HWND, const char*);
int main() {
HMODULE hDll = LoadLibrary("ScreenCaptureDLL.dll");
if (!hDll) return -1;
CaptureRegionFn fn = (CaptureRegionFn)GetProcAddress(hDll, "CaptureRegion");
if (fn) {
BOOL ok = fn(100, 100, 400, 400, "test.bmp");
if (ok) {
MessageBox(NULL, "截图成功!", "提示", MB_OK);
}
}
FreeLibrary(hDll);
return 0;
}
Python示例 (ctypes):
[Python] 纯文本查看 复制代码 import ctypes
dll = ctypes.CDLL("ScreenCaptureDLL.dll")
dll.CaptureRegion.argtypes = [ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_int, ctypes.c_char_p]
dll.CaptureRegion.restype = ctypes.c_bool
ok = dll.CaptureRegion(100, 100, 400, 400, b"test.bmp")
print("成功:", ok)
五、特点
--------
- 纯C++,32位
- 无需外部依赖
- 支持大部分窗口(Qt、WPF、普通Win32)
- 截图保存为24位BMP格式
- 简单易用
六、编译
--------
用VS2019打开 ScreenCaptureDLL.sln
选择 Debug/Release + Win32
直接编译
完整源码:
成品DLL:直接调用
导入函数:
.版本 2<BR><BR>.DLL命令 CaptureWindowRegion12, 逻辑型, "ScreenCaptureDLL.dll", "_CaptureWindowRegion@24", 公开<BR> .参数 hwnd, 整数型<BR> .参数 x, 整数型<BR> .参数 y, 整数型<BR> .参数 x1, 整数型<BR> .参数 y1, 整数型<BR> .参数 file, 文本型<BR>
|
|