截取模板图:把窗口客户区的一块区域截图存成图片文件(给 FindPic 当模板用),返回实际保存的完整路径,失败返回空串。x1,y1,x2,y2 为客户区坐标(x2,y2 是右下角,不含该点,宽=x2-x1);filePath 传空串则按 SetSavePath 设置的目录自动命名 tpl_年月日_时分秒_毫秒.bmp;hwnd 传 0 = 用 BindWindow 绑定的窗口,未绑定则截桌面。走当前抓图模式(SetDisplayInput),截出的像素和 FindPic 抓到的完全一致,不存在模板与实测颜色对不上的问题
| 参数名 | 方向 | 类型 | 说明 |
|---|---|---|---|
hwnd | [in] | __int64 (int64) | 目标窗口句柄(64 位整数 hyper 类型);传 0 表示桌面(取屏幕色/截屏幕区域) |
x1 | [in] | long | 查找/截取区域左上角横坐标(客户区坐标,未绑定窗口时为屏幕坐标) |
y1 | [in] | long | 查找/截取区域左上角纵坐标 |
x2 | [in] | long | 查找/截取区域右下角横坐标 |
y2 | [in] | long | 查找/截取区域右下角纵坐标 |
filePath | [in] | string (BSTR) | 截图保存的完整或相对路径(相对路径时按 SetSavePath 目录解析) |
// 说明:本示例为完整 x64 控制台工程,整段复制覆盖新建控制台工程的 main.cpp 即可编译运行。
// 1. VS2019 新建「控制台应用」,项目属性 -> 平台选 x64,才能加载 64 位 DLL。
// 2. 项目属性 -> C/C++ -> 预编译头 -> 改为「不使用预编译头」,否则默认找 pch.h 会报 C1010。
// 3. 把 x64Game-Plug.dll 放到 exe 同目录(#import 与免注册加载都需要)。
// 4. 源码保存为 UTF-8 带 BOM,避免中文乱码;全站用 regsvr32 注册也可(需管理员)。
#include <iostream>
#include <clocale>
#include <windows.h>
#include <comdef.h>
// 免注册加载:用 IGamePlug 智能指针装载
// rename 是为避开 windows.h 的 FindWindow 宏冲突,接口里的 FindWindow 在 C++ 侧改名为 FindWindowGP
#import "x64Game-Plug.dll" no_namespace rename("FindWindow", "FindWindowGP")
static HMODULE g_hDll = NULL;
static IGamePlugPtr g_plug;
// ---------- 免注册:加载 DLL 并创建对象(发布时把 DLL 放 exe 同目录)----------
static bool LoadPlug() {
const wchar_t* paths[] = {
L"x64Game-Plug.dll",
L"..\x64Game-Plug.dll"
};
for (int i = 0; i < (int)(sizeof(paths) / sizeof(paths[0])); ++i) {
g_hDll = ::LoadLibraryW(paths[i]);
if (g_hDll) break;
}
if (!g_hDll) {
std::cout << "LoadLibrary failed: put x64Game-Plug.dll next to the exe" << std::endl;
return false;
}
typedef HRESULT (__stdcall *GamePlugCreateFn)(IGamePlug**);
GamePlugCreateFn fn = (GamePlugCreateFn)::GetProcAddress(g_hDll, "GamePlugCreate");
if (!fn) { std::cout << "GetProcAddress GamePlugCreate failed" << std::endl; return false; }
HRESULT hr = fn(&g_plug);
if (FAILED(hr)) { std::cout << "GamePlugCreate failed: 0x" << std::hex << hr << std::endl; return false; }
return true;
}
int main() {
if (!LoadPlug()) { system("pause"); return 1; }
// 授权(收费功能需先传入授权码,去 https://www.game-plug.cn/ 获取)
g_plug->SetUsername(_bstr_t(L"U12-XXXXXXXXXXXXXXXX"));
// ---------- 调用插件本命令方法如下 ----------
try {
__int64 hwnd = 0;
long x1 = 0;
long y1 = 0;
long x2 = 800;
long y2 = 600;
_bstr_t filePath = L"sample";
_bstr_t s = g_plug->SaveRegionShot(hwnd, x1, y1, x2, y2, filePath);
std::wcout << L"结果: " << (wchar_t*)s << std::endl;
} catch (_com_error& e) {
std::wcout << L"COM error: " << e.ErrorMessage() << std::endl;
}
g_plug->Release();
system("pause");
return 0;
}
返回实际保存的完整文件路径;失败返回空串。