返回列表 发帖

[其他] image3.5发布,最强大的批处理界面辅助工具

本帖最后由 Byaidu 于 2018-4-30 12:10 编辑

image3.5包含了许多重大更新
包括绘图命令的完善以及绘图脚本的模块化

添加了“图元”和“画布”的概念
load pic mypic.bmp
上面这条命令的作用就是把mypic.bmp图片加载为一个叫pic的“图元”,然后再把这个pic“图元”绘制到一个叫pic的同名“画布”
然后mouse功能就会通过cmd“画布”的图元索引树来判定用户点击的了哪个“图元”
“图元”在image里只是一个虚拟的概念,只使用方便判定碰撞箱,不占用实际内存
“画布”在image里是一个实体的概念,没有对应的碰撞箱,仅用来承载“图元”,每个“画布”都有对应的HDC,会占用实际内存

其实我是想投到开源原创工具版块里的,不过貌似等级不够,所以就先投在这里了
image
控制台显示图片 Ver 3.5 by Byaidu

help                                                显示帮助
load tag [file]                                        新建画布tag,并加载图片file为图元tag,再将图元tag绘制到画布tag
unload tag                                        删除画布tag
save tag file                                        将画布tag的内容存储到file中
target tag                                        切换当前绘图目标为画布tag
buffer tag [w] [h] [r] [g] [b ]                        新建画布tag,并新建一个颜色为rgb(默认为白色)、大小为w*h(默认为当前绘图目标的大小)图元tag,再将图元tag绘制到画布tag
resize tag w h                                        将画布tag缩放到w*h的大小,如果目标cmd,则会同时将控制台设置为最适合绘图的状态
info tag                                        将画布tag的宽和高存储到变量image
rotate tag degree                                将画布tag顺时针旋转degree度
draw tag x y [trans [r] [g] [b ] | alpha a]        将画布tag绘制到当前绘图目标的x,y位置上
                                                若指定了trans,则以rgb为透明色(默认为白色)
                                                若指定了alpha,则以a为透明度
font r g b [w] [h]                                设置当前绘图目标所用字体的rgb值和大小
text string                                        在当前绘图目标的x,y的位置上输出字符串string
getpix tag x y                                        将画布tag上x,y位置的rgb值存储到变量image
setpix tag x y r g b                                设置画布tag上x,y位置的rgb值
cls                                                清空画布cmd的内容
export                                                将画布cmd的句柄存储到变量image
import tag handle                                通过句柄将另一个控制台的画布cmd映射到此控制台的画布tag
sleep time                                        延时time毫秒
list file [label]                                执行image指令脚本file,若指定了label则会直接跳转到脚本中的标签label
exit                                                退出当前image指令脚本
union tag                                        合并图层tag中的所有图元成一个与图层tag同名的图元tag
debug                                                以图形形式输出图元索引树,用于查看画布cmd上的各个图元
mouse time [region1] [region2] ...                捕获鼠标坐标及事件,坐标以像素为单位,时间以毫秒为单位
                                                若time>=0,当发生点击事件或时间超过限制时会将鼠标坐标x,y以及坐标在画布cmd上所在图元tag的tag存储到变量image,并将图元tag的tag单独再存储到变量errorlevel
                                                若time<0,不设置时间限制
                                                若指定了region,那么返回的的就不是图元tag的tag而是region的序号,如果鼠标坐标不在任何一个指定的region中,则返回序号0
                                                region应以如下的形式给出:x1,y1,x2,y2

核心代码
image.cpp
/***********************************************************
image
控制台显示图片 Ver 3.5 by Byaidu
完整代码及档案下载:https://github.com/Byaidu/image
部分代码参考:https://github.com/YinTianliang/CAPIx
************************************************************/
#define _CRT_SECURE_NO_WARNINGS
#define _CRT_NON_CONFORMING_SWPRINTFS
#include <windows.h>
#include <gdiplus.h>
#include <wchar.h>
#include <iostream>
#include <fstream>
#include <string>
#include <cstdio>
#include <map>
#include "regionmgr.cpp"
using namespace std;
using namespace Gdiplus;
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define DLL_EXPORT __declspec(dllexport)
#define wtoi _wtoi
#define wcsicmp _wcsicmp
#define match(x,y) if (wstring(argv[x])==wstring(y)||wstring(argv[x]).substr(0,2)==wstring(y).substr(0,2))
#define matchclsid(x,y) if (wstring(argv[x]).substr(wstring(argv[x]).length()-3)==wstring(y))
#pragma comment(lib,"msimg32.lib")
#pragma comment(lib,"GdiPlus.lib")
struct imageres { //画布结构体
HDC dc;
HBITMAP oldbmp;
int w, h;
BUF region; //图元索引树
imageres() {};
imageres(wchar_t *file) //初始化画布,并加载图元到画布上
{
BITMAP bi;
//为了防止多个hbmp同时用一个hdc发生冲突,所以这里给所有的hbmp分配各自的hdc
dc = CreateCompatibleDC(nullptr);
//HBITMAP bmp = (HBITMAP)LoadImageA(nullptr, file, IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
HBITMAP bmp;
Bitmap *bm = new Bitmap(file);
bm->GetHBITMAP(0, &bmp);
delete bm;
oldbmp = (HBITMAP)SelectObject(dc, bmp);
GetObject(bmp, sizeof(BITMAP), &bi);
w = bi.bmWidth;
h = bi.bmHeight;
}
void regioninit(wchar_t *tag,int w,int h) {region = BUF(tag, w - 1, h - 1);}
}*hTarget;
map<wstring, imageres> resmap; //画布映射表
HWND hCMD; //控制台窗口句柄
double scale; //校正缩放比
void image(wchar_t *); //主函数
void Init_image(); //初始化
bool WINAPI DllMain(HMODULE hModule, DWORD dwReason, LPVOID lpvReserved)
{
switch (dwReason)
{
case DLL_PROCESS_ATTACH:
//HookAPI(SetEnvironmentVariableW, SetCall_image);
DisableThreadLibraryCalls(hModule);
Init_image();
break;
case DLL_PROCESS_DETACH:
break;
}
return true;
}
extern "C" DLL_EXPORT int WINAPI Init(void)//随便给个导出函数,方便加载
{
return 0;
}
extern "C" __declspec(dllexport) void call(wchar_t *varName, wchar_t *varValue)
{
//判断变量名是否为image, 是则调用image
if (!wcsicmp(varName, L"image")) image(varValue);
return;
}
void Init_image() //初始化
{
GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, nullptr);
imageres hRes;
//获取cmd大小以及绘图句柄
hCMD = GetConsoleWindow();
HDC hDC = GetDC(hCMD);
DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(nullptr, ENUM_CURRENT_SETTINGS, &dm);
int ax = dm.dmPelsWidth;
int bx = GetSystemMetrics(SM_CXSCREEN);
scale = (double)ax / bx;//校正缩放比
RECT rc;
GetClientRect(hCMD, &rc);
hRes.dc = hDC;
hRes.w = int(scale*(rc.right - rc.left));
hRes.h = int(scale*(rc.bottom - rc.top));
hRes.regioninit((wchar_t*)L"cmd", hRes.w, hRes.h);
resmap[L"cmd"] = hRes; //把cmd添加到画布映射表中
hTarget = &resmap[L"cmd"];//getres("cmd"); //绘图默认指向cmd
//获取desktop大小以及绘图句柄
hDC = GetDC(nullptr);
hRes.dc = hDC;
hRes.w = dm.dmPelsWidth;
hRes.h = dm.dmPelsHeight;
hRes.regioninit((wchar_t*)L"desktop", hRes.w, hRes.h);
resmap[L"desktop"] = hRes; //把desktop添加到画布映射表中
TextOutA(hTarget->dc, 0, 0, 0, 0);//第一次使用TextOutA无效,大概是个bug
return;
}
imageres * getres(wchar_t *tag) //查找画布
{
if (!resmap.count(tag)) //如果在画布映射表中找不到,则先加载图元到画布,再将画布添加到画布映射表
{
imageres hRes(tag);
hRes.regioninit(tag, hRes.w, hRes.h);
resmap[tag] = hRes;
}
return &resmap[tag];
}
void delres(wchar_t *tag) //销毁原来的画布,防止内存泄漏
{
imageres * hRes = getres(tag);
HBITMAP bmp = (HBITMAP)SelectObject(hRes->dc, hRes->oldbmp);
DeleteObject(bmp);
DeleteDC(hRes->dc);
resmap.erase(tag);
return;
}
//不能用SelectObject获取cmd等特殊画布的hbitmap,所以要复制一份出来,注意使用之后要DeleteObject
HBITMAP copyhbitmap(imageres *hSrc)
{
imageres hRes;
hRes.dc = CreateCompatibleDC(hSrc->dc);
HBITMAP hBitmap = CreateCompatibleBitmap(hSrc->dc, hSrc->w, hSrc->h);
hRes.oldbmp = (HBITMAP)SelectObject(hRes.dc, hBitmap);
BitBlt(hRes.dc, 0, 0, hSrc->w, hSrc->h, hSrc->dc, 0, 0, SRCCOPY);
SelectObject(hRes.dc, hRes.oldbmp);
DeleteDC(hRes.dc);
return hBitmap;
}
void rotateres(wchar_t **argv)
{
imageres * hRes = getres(argv[1]);
HBITMAP hSrc = copyhbitmap(hRes);
Rect rect(0, 0, hRes->w, hRes->h);
//用于加载旧位图
Bitmap bitmap(hSrc, nullptr);
BitmapData bitmapData;
bitmap.LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &bitmapData);
byte* pixels = (byte*)bitmapData.Scan0;
//用于加载新位图
Bitmap bitmap2(hSrc, nullptr);
BitmapData bitmapData2;
bitmap2.LockBits(&rect, ImageLockModeWrite, PixelFormat24bppRGB, &bitmapData2);
byte* pixels2 = (byte*)bitmapData2.Scan0;
//旋转
double pi = 3.1415926;
double angle = -(double)wtoi(argv[2]) / 180 * pi;
double sina = sin(angle), cosa = cos(angle);
int cx = hRes->w / 2, cy = hRes->h / 2;
for (int i = 0; i<hRes->w; i++)
for (int j = 0; j<hRes->h; j++)
{
int x = (int)(cx + (i - cx)*cosa - (j - cy)*sina), y = (int)(cy + (i - cx)*sina + (j - cy)*cosa);//原坐标
if (x >= 0 && x < hRes->w&&y >= 0 && y < hRes->h)
{
for (int k = 0; k < 3; k++)
pixels2[j*bitmapData2.Stride + 3 * i + k] = pixels[y*bitmapData.Stride + 3 * x + k];
}
else
{
for (int k = 0; k < 3; k++)
pixels2[j*bitmapData2.Stride + 3 * i + k] = 0xFF;
}
}
bitmap.UnlockBits(&bitmapData);
bitmap2.UnlockBits(&bitmapData2);
//复制临时画布到目标画布
HDC hDCMem = CreateCompatibleDC(hRes->dc);
HBITMAP hBitmap;
bitmap2.GetHBITMAP(0, &hBitmap);
HBITMAP oldbmp = (HBITMAP)SelectObject(hDCMem, hBitmap);
BitBlt(hRes->dc, 0, 0, hRes->w, hRes->h, hDCMem, 0, 0, SRCCOPY);
//销毁临时复制的画布
DeleteObject(hSrc);
SelectObject(hDCMem, oldbmp);
DeleteObject(hBitmap);
DeleteDC(hDCMem);
}
void alphares(wchar_t **argv)
{
double alpha = (double)wtoi(argv[5])/255;
//用于加载源位图
imageres * hRes = getres(argv[1]);
HBITMAP hSrc = copyhbitmap(hRes);
Rect rect(0, 0, hRes->w, hRes->h);
Bitmap bitmap(hSrc, nullptr);
BitmapData bitmapData;
bitmap.LockBits(&rect, ImageLockModeRead, PixelFormat24bppRGB, &bitmapData);
byte* pixels = (byte*)bitmapData.Scan0;
//用于加载目标位图
//不能SelectObject获取cmd等特殊画布的hbitmap,所以要复制一份出来,注意使用之后要DeleteObject
HBITMAP hSrc2 = copyhbitmap(hTarget);
Rect rect2(0, 0, hTarget->w, hTarget->h);
Bitmap bitmap2(hSrc2, nullptr);
BitmapData bitmapData2;
bitmap2.LockBits(&rect2, ImageLockModeRead, PixelFormat24bppRGB, &bitmapData2);
byte* pixels2 = (byte*)bitmapData2.Scan0;
//用于加载新位图
Rect rect3(0, 0, hTarget->w, hTarget->h);
Bitmap bitmap3(hSrc2, nullptr);
BitmapData bitmapData3;
bitmap3.LockBits(&rect3, ImageLockModeWrite, PixelFormat24bppRGB, &bitmapData3);
byte* pixels3 = (byte*)bitmapData3.Scan0;
//alpha混合
int cx = wtoi(argv[2]), cy = wtoi(argv[3]);
for (int i = 0; i<hTarget->w; i++)
for (int j = 0; j<hTarget->h; j++)
{
int x = i - cx, y = j - cy;//源坐标
if (x >= 0 && x < hRes->w&&y >= 0 && y < hRes->h)
{
for (int k = 0; k < 3; k++)
pixels3[j*bitmapData3.Stride + 3 * i + k] =
(byte)((1 - alpha) * pixels2[j*bitmapData2.Stride + 3 * i + k] +
alpha * pixels[y*bitmapData.Stride + 3 * x + k]);
}
else
{
for (int k = 0; k < 3; k++)
pixels3[j*bitmapData3.Stride + 3 * i + k] = pixels2[j*bitmapData2.Stride + 3 * i + k];
}
}
bitmap.UnlockBits(&bitmapData);
bitmap2.UnlockBits(&bitmapData2);
bitmap3.UnlockBits(&bitmapData3);
//复制临时画布到目标画布
HDC hDCMem = CreateCompatibleDC(hTarget->dc);
HBITMAP hBitmap;
bitmap3.GetHBITMAP(0, &hBitmap);
HBITMAP oldbmp = (HBITMAP)SelectObject(hDCMem, hBitmap);
BitBlt(hTarget->dc, 0, 0, hTarget->w, hTarget->h, hDCMem, 0, 0, SRCCOPY);
//销毁临时复制的画布
DeleteObject(hSrc);
DeleteObject(hSrc2);
SelectObject(hDCMem, oldbmp);
DeleteObject(hBitmap);
DeleteDC(hDCMem);
}
void image(wchar_t *CmdLine)
{
//wcout << CmdLine << endl;
int argc;
wchar_t **argv;
argv = CommandLineToArgvW(CmdLine, &argc);
match(0, L"help")
{
printf(
"image\n"
"控制台显示图片 Ver 3.5 by Byaidu\n"
);
}
match(0, L"load") //加载图元到同名画布,再将画布到画布映射表
{
wchar_t *tag = argv[1]; //画布名称
//销毁原来的画布,防止内存泄漏
if (resmap.count(tag)) delres(tag);
imageres hRes(argc > 2 ? argv[2] : argv[1]);
hRes.regioninit(tag, hRes.w, hRes.h);
resmap[tag] = hRes;
}
match(0, L"unload") //卸载画布
{
//销毁原来的画布,防止内存泄漏
delres(argv[1]);
}
match(0, L"save") //保存为图片
{
imageres * hRes = getres(argv[1]);
HBITMAP hSrc = copyhbitmap(hRes);
Rect rect(0, 0, hRes->w, hRes->h);
Bitmap bitmap(hSrc, nullptr);
//https://stackoverflow.com/questions/1584202/gdi-bitmap-save-problem
CLSID Clsid;
matchclsid(2, L"bmp") CLSIDFromString(L"{557cf400-1a04-11d3-9a73-0000f81ef32e}", &Clsid);
matchclsid(2, L"jpg") CLSIDFromString(L"{557cf401-1a04-11d3-9a73-0000f81ef32e}", &Clsid);
matchclsid(2, L"png") CLSIDFromString(L"{557cf406-1a04-11d3-9a73-0000f81ef32e}", &Clsid);
bitmap.Save(argv[2], &Clsid, nullptr);
DeleteObject(hSrc);
}
match(0, L"target") //更改绘图目标
{
hTarget = getres(argv[1]);
}
match(0, L"buffer") //新建一个buffer对象
{
wchar_t *tag = argv[1];
//销毁原来的画布,防止内存泄漏
if (resmap.count(tag)) delres(tag);
imageres hRes;
hRes.dc = CreateCompatibleDC(hTarget->dc);
HBITMAP hBitmap = CreateCompatibleBitmap(hTarget->dc,argc>2?wtoi(argv[2]):hTarget->w,argc>3?wtoi(argv[3]):hTarget->h);
hRes.oldbmp = (HBITMAP)SelectObject(hRes.dc, hBitmap);
int color = argc>6?RGB(wtoi(argv[4]),wtoi(argv[5]),wtoi(argv[6])):RGB(255,255,255);
colorregion(hRes.dc, color, 0, 0, hTarget->w - 1, hTarget->h - 1);
hRes.w = hTarget->w;
hRes.h = hTarget->h;
//把buffer添加到画布调用表中
hRes.regioninit(tag, hRes.w, hRes.h);
resmap[tag] = hRes;
}
match(0, L"resize") //缩放
{
imageres * hRes = getres(argv[1]);
match(1,L"cmd")
{
//防止快速编辑功能刷掉图像
// 获取标准输入输出设备句柄  
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD oldConMode;
GetConsoleMode(hIn, &oldConMode); // 备份
SetConsoleMode(hIn, (oldConMode | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT)&(~ENABLE_QUICK_EDIT_MODE));
//隐藏光标
CONSOLE_CURSOR_INFO cursor_info = { (DWORD)25, FALSE };
SetConsoleCursorInfo(hOut, &cursor_info);
RECT rc,rc2;
SetScrollRange(hCMD, 0, 0, 0, 1);
SetScrollRange(hCMD, 1, 0, 0, 1);
GetClientRect(hCMD, &rc);
GetWindowRect(hCMD, &rc2);
int w = (rc2.right - rc2.left) - (rc.right - rc.left) + int((wtoi(argv[2])) / scale);
int h = (rc2.bottom - rc2.top) - (rc.bottom - rc.top) + int((wtoi(argv[3])) / scale);
//printf("scale:%f\n", scale);
//printf("C:%dx%d\n", rc.right - rc.left, rc.bottom - rc.top);
//printf("W:%dx%d\n", rc2.right - rc2.left, rc2.bottom - rc2.top);
MoveWindow(hCMD, rc2.left, rc2.top, w, h, 0);
Sleep(10);
SetScrollRange(hCMD, 0, 0, 0, 1);
SetScrollRange(hCMD, 1, 0, 0, 1);
Sleep(10);
hRes->w = (int)wtoi(argv[2]);
hRes->h = (int)wtoi(argv[3]);
}else{
HDC hDCMem = CreateCompatibleDC(hRes->dc);
HBITMAP hBitmap = CreateCompatibleBitmap(hRes->dc, wtoi(argv[2]), wtoi(argv[3]));
HBITMAP oldbmp = (HBITMAP)SelectObject(hDCMem, hBitmap);
StretchBlt(hDCMem, 0, 0, wtoi(argv[2]), wtoi(argv[3]), hRes->dc, 0, 0, hRes->w, hRes->h, SRCCOPY);
//销毁原来的画布,防止内存泄漏
HBITMAP bmp = (HBITMAP)SelectObject(hRes->dc, hRes->oldbmp);
DeleteObject(bmp);
DeleteDC(hRes->dc);
//替换原来的画布
hRes->oldbmp = oldbmp;
hRes->dc = hDCMem;
hRes->w = wtoi(argv[2]);
hRes->h = wtoi(argv[3]);
}
hRes->regioninit(argv[1], hRes->w, hRes->h);
}
match(0, L"cls")
{
//清屏并重置cmd图层的图元索引树
imageres * hRes = getres((wchar_t*)L"cmd");
hRes->regioninit((wchar_t*)L"cmd", hRes->w, hRes->h);
InvalidateRect(hCMD, nullptr, true);
UpdateWindow(hCMD);
Sleep(10);
}
match(0, L"rotate")
{
rotateres(argv);
}
match(0, L"draw")
{
//直接在目标上绘图
imageres * hRes = getres(argv[1]);
//提前清除目标区域的图元索引树结构可以避免两个图元索引树交叉在一起使图元索引树变得更复杂
BUF clearbuf(L"", hRes->region.p->x2, hRes->region.p->y2);
complexupdate(clearbuf.p, 0, 0, hRes->region.p->x2, hRes->region.p->y2, wtoi(argv[2]), wtoi(argv[3]), hTarget->region.p);
complexupdate(hRes->region.p, 0, 0, hRes->region.p->x2, hRes->region.p->y2, wtoi(argv[2]), wtoi(argv[3]), hTarget->region.p);
if (argc == 4)
{
BitBlt(hTarget->dc, wtoi(argv[2]), wtoi(argv[3]), hRes->w, hRes->h, hRes->dc, 0, 0, SRCCOPY);
}
else
{
match(4, L"trans")
TransparentBlt(hTarget->dc, wtoi(argv[2]), wtoi(argv[3]), hRes->w, hRes->h, hRes->dc, 0, 0, hRes->w, hRes->h, argc==8?RGB(wtoi(argv[5]),wtoi(argv[6]),wtoi(argv[7])):RGB(255, 255, 255));
match(4, L"alpha")
alphares(argv);
}
}
match(0, L"text")
{
//显示两次才会刷新出来,大概是个bug
for (int i = 0; i < 2;i++) TextOutW(hTarget->dc, wtoi(argv[2]), wtoi(argv[3]), argv[1], wcslen(argv[1]));
}
match(0, L"font")
{
SetBkMode(hTarget->dc, TRANSPARENT);
SetTextColor(hTarget->dc, RGB(wtoi(argv[1]), wtoi(argv[2]), wtoi(argv[3])));
if (argc > 4)
{
HFONT hFont = CreateFontW(
argc > 5 ? wtoi(argv[5]) : 0,
argc > 4 ? wtoi(argv[4]) : 0,
argc > 6 ? wtoi(argv[6]) : 0/*不用管*/,
argc > 7 ? wtoi(argv[7]) : 0/*不用管*/,
argc > 8 ? wtoi(argv[8]) : 400/*一般这个值设为400*/,
argc > 9 ? wtoi(argv[9]) : 0/*不带斜体*/,
argc > 10 ? wtoi(argv[10]) : 0/*不带下划线*/,
argc > 11 ? wtoi(argv[11]) : 0/*不带删除线*/,
DEFAULT_CHARSET, //这里我们使用默认字符集,还有其他以 _CHARSET 结尾的常量可用
OUT_CHARACTER_PRECIS, CLIP_CHARACTER_PRECIS, //这行参数不用管
DEFAULT_QUALITY, //默认输出质量
FF_DONTCARE, //不指定字体族*/
argc > 12 ? argv[12] : L"新宋体" //字体名
);
SelectObject(hTarget->dc, hFont);
}
}
match(0, L"sleep")
{
Sleep(wtoi(argv[1]));
}
match(0, L"info")
{
wchar_t info[100];
imageres * hRes = getres(argv[1]);
swprintf(info, L"%d %d", hRes->w, hRes->h);
SetEnvironmentVariableW(L"image", info);
}
match(0, L"export")
{
wchar_t info[100];
swprintf(info, L"%d", (int)hCMD);
SetEnvironmentVariableW(L"image", info);
}
match(0, L"import")
{
wchar_t *tag = argv[1];
//销毁原来的画布,防止内存泄漏
if (resmap.count(tag)) delres(tag);
imageres hRes;
//获取cmd大小以及绘图句柄
HWND hCMD2 = (HWND)wtoi(argv[2]);
HDC hDC = GetDC(hCMD2);
DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(nullptr, ENUM_CURRENT_SETTINGS, &dm);
int ax = dm.dmPelsWidth;
int bx = GetSystemMetrics(SM_CXSCREEN);
double scale = (double)ax / bx;//校正缩放比
RECT rc;
GetClientRect(hCMD2, &rc);
hRes.dc = hDC;
hRes.w = (int)ceil(scale*(rc.right - rc.left));
hRes.h = (int)ceil(scale*(rc.bottom - rc.top));
hRes.regioninit(tag, hRes.w, hRes.h);
resmap[tag] = hRes; //把cmd作为画布添加到调用表中
}
match(0, L"getpix")
{
wchar_t info[100];
COLORREF color=GetPixel(hTarget->dc, wtoi(argv[1]), wtoi(argv[2]));
swprintf(info, L"%d %d %d", GetRValue(color), GetGValue(color), GetBValue(color));
SetEnvironmentVariableW(L"image", info);
}
match(0, L"setpix")
{
SetPixel(hTarget->dc, wtoi(argv[1]), wtoi(argv[2]), RGB(wtoi(argv[3]), wtoi(argv[4]), wtoi(argv[5])));
}
match(0, L"list")
{
bool skip = 0;
if (argc > 2) skip = 1;
ifstream in(argv[1]);
string str;
wchar_t wstr[100];
while (!in.eof())
{
getline(in, str);
MultiByteToWideChar(CP_ACP, 0, str.c_str(), -1, wstr, sizeof(wstr));
if (skip&&L":" + wstring(argv[2]) == wstring(wstr)) { skip = 0; continue; }
if (skip) continue;
if (wstring(L"exit") == wstring(wstr)) break;
//wcout << wstring(wstr) << endl;
image(wstr);
}
in.close();
}
match(0, L"mouse")
{
imageres *hRes = getres((wchar_t*)L"cmd");
wchar_t info[100];
POINT mosPos;
int x, y;
int timer = wtoi(argv[1]);
//这里要重新设置一次,要不然ReadConsoleInput会卡住
// 获取标准输入输出设备句柄  
HANDLE hOut = GetStdHandle(STD_OUTPUT_HANDLE);
HANDLE hIn = GetStdHandle(STD_INPUT_HANDLE);
DWORD oldConMode;
GetConsoleMode(hIn, &oldConMode); // 备份
SetConsoleMode(hIn, (oldConMode | ENABLE_WINDOW_INPUT | ENABLE_MOUSE_INPUT)&(~ENABLE_QUICK_EDIT_MODE));
if (timer < 0)
{
INPUT_RECORD Rec;
DWORD res;
while (1)
{
ReadConsoleInputW(hIn, &Rec, 1, &res);
if (Rec.EventType == MOUSE_EVENT)
{
if (Rec.Event.MouseEvent.dwButtonState == FROM_LEFT_1ST_BUTTON_PRESSED)
{
GetCursorPos(&mosPos);
ScreenToClient(hCMD, &mosPos);
x = min(max((int)scale*mosPos.x, 0), hRes->w);
y = min(max((int)scale*mosPos.y, 0), hRes->h);
break;
}
}
/*
if (Rec.EventType == KEY_EVENT)
{
if (Rec.Event.KeyEvent.bKeyDown == 1)
{
}
}
*/
}
}
if (timer >= 0)
{
DWORD tstart = GetTickCount();
while (!(KEYDOWN(VK_LBUTTON) || int(GetTickCount() - tstart) >= timer)) Sleep(10);
GetCursorPos(&mosPos);
ScreenToClient(hCMD, &mosPos);
x = min(max((int)scale*mosPos.x, 0), hRes->w);
y = min(max((int)scale*mosPos.y, 0), hRes->h);
}
if (argc >= 3)
{
//在指定的region列表中查找
int ret = 0;
for (int i = 2; i < argc; i++)
{
int x1, y1, x2, y2;
swscanf(argv[i], L"%d,%d,%d,%d", &x1, &y1, &x2, &y2);
if (x >= x1 && x <= x2 && y >= y1 && y <= y2) ret = i - 1;
}
swprintf(info, L"%d %d %d", x, y, ret);
SetEnvironmentVariableW(L"image", info);
swprintf(info, L"%d", ret);
SetEnvironmentVariableW(L"errorlevel", info);
}else{
//在图元索引表中查找
wstring ret = query(resmap[L"cmd"].region.p, x, y);
swprintf(info, L"%d %d %s", x, y, ret.c_str());
SetEnvironmentVariableW(L"image", info);
swprintf(info, L"%s", ret.c_str());
SetEnvironmentVariableW(L"errorlevel", info);
}
SetConsoleMode(hIn, oldConMode);
}
match(0, L"debug")
{
imageres *hRes = getres((wchar_t*)L"cmd");
show(hRes->region.p);
}
match(0, L"union")
{
//合并图层中的所有图元成一个与图层同名的图元,即重置图层的图元索引树
imageres * hRes = getres(argv[1]);
hRes->regioninit(argv[1], hRes->w, hRes->h);
}
match(0, L"cmd")
{
_wsystem(argv[1]);
}
//todo:支持鼠标键盘同时控制
LocalFree(argv);
return;
}COPY
regionmgr.cpp
#pragma   once
#include <windows.h>
#include <cstdio>
#include <iostream>
#include <algorithm>
#include <map>
#include <stack>
#include <ctime>
using namespace std;
struct P{
int single;
wstring src;
int x1, y1, x2, y2, x0, y0;
P *l, *r;
};
struct BUF{
P *p;
BUF(){};
BUF(wstring src,int w,int h){
p = new P{ 1,src,0,0,w,h,0,0 };
}
};
map<wstring,BUF> bufmap;
/*
void Init_regionmgr() //初始化
{
//srand(time(0));
//获取cmd大小以及绘图句柄
hCMD = GetConsoleWindow();
HDC dc = GetDC(hCMD);
DEVMODE dm;
dm.dmSize = sizeof(DEVMODE);
EnumDisplaySettings(nullptr, ENUM_CURRENT_SETTINGS, &dm);
int ax = dm.dmPelsWidth;
int bx = GetSystemMetrics(SM_CXSCREEN);
double scale = (double)ax / bx;//校正缩放比
RECT rc;
GetClientRect(hCMD, &rc);
int w = (int)ceil(scale*(rc.right - rc.left));
int h = (int)ceil(scale*(rc.bottom - rc.top));
srcmap[0] = dc;//0号资源为控制台dc
srcmap[1] = 0;//1号资源为纯黑背景
BUF buf(1, w-1, h-1);
bufmap["cmd"] = buf; //把cmd作为资源添加到调用表中
hTarget = &bufmap["cmd"];//绘图默认指向cmd
return;
}
*/
//合并剪枝
void singleunion(P *p)
{
if (p->r->single == 1 && p->l->single == 1 && p->r->src == p->l->src&&
p->r->x1 - p->l->x1 == p->r->x0 - p->l->x0&&
p->r->y1 - p->l->y1 == p->r->y0 - p->l->y0)
{
p->x0 = p->l->x0;
p->y0 = p->l->y0;
p->src = p->l->src;
p->single = 1;
//todo:delete point
return;
}
if (p->r->single &&p->r->x1== p->x1&&p->r->y1 == p->y1&&p->r->x2 == p->x2&&p->r->y2 == p->y2)
{
p->x0 = p->r->x0;
p->y0 = p->r->y0;
p->src = p->r->src;
p->single = 1;
return;
}
if (p->l->single &&p->l->x1 == p->x1&&p->l->y1 == p->y1&&p->l->x2 == p->x2&&p->l->y2 == p->y2)
{
p->x0 = p->l->x0;
p->y0 = p->l->y0;
p->src = p->l->src;
p->single = 1;
return;
}
}
//x1y1x2y2为目标p坐标,x0y0为源v坐标
void singleupdate(P *p,int x1,int y1,int x2,int y2,int x0,int y0, wstring v,int now=0)
{
if (p->single==1&&p->x1==x1&&p->y1==y1&&p->x2==x2&&p->y2==y2){
if (!(p->x0==x0&&p->y0==y0&&p->src==v)){
p->x0=x0,p->y0=y0,p->src=v;
/*
if (hTarget==&bufmap["cmd"]){
//printf("BITBLT:(%d,%d)(%d,%d):%d(%d,%d)\n",p->x1,p->y1,p->x2,p->y2,p->src,p->x0,p->y0);
BitBlt(srcmap[0], p->x1, p->y1, p->x2 - p->x1 + 1, p->y2 - p->y1 + 1, srcmap[p->src], p->x0, p->y0, SRCCOPY);
}
*/
}
return;
}
if (p->single){
if (now) {
if (x1 != p->x1) {
p->l = new P{ 1,p->src ,p->x1, p->y1, x1 - 1, p->y2, p->x0, p->y0 };
p->r = new P{ 1,p->src ,x1,p->y1,p->x2,p->y2,p->x0 + x1 - p->x1,p->y0 };
singleupdate(p->r, x1, y1, x2, y2, x0, y0, v, !now);
}
else {
p->l = new P{ 1,p->src ,p->x1,p->y1,x2,p->y2,p->x0,p->y0 };
p->r = new P{ 1,p->src ,x2 + 1,p->y1,p->x2,p->y2,p->x0 + x2 + 1 - p->x1,p->y0 };
singleupdate(p->l, x1, y1, x2, y2, x0, y0, v, !now);
}
}
else {
if (y1 != p->y1) {
p->l = new P{ 1,p->src ,p->x1, p->y1, p->x2, y1 - 1, p->x0, p->y0 };
p->r = new P{ 1,p->src ,p->x1,y1,p->x2,p->y2,p->x0,p->y0 + y1 - p->y1 };
singleupdate(p->r, x1, y1, x2, y2, x0, y0, v, !now);
}
else {
p->l = new P{ 1,p->src ,p->x1,p->y1,p->x2,y2,p->x0,p->y0 };
p->r = new P{ 1,p->src ,p->x1,y2 + 1,p->x2,p->y2,p->x0,p->y0 + y2 + 1 - p->y1 };
singleupdate(p->l, x1, y1, x2, y2, x0, y0, v, !now);
}
}
p->single=0;
}else{
if (now){
if (x2<= p->l->x2) singleupdate(p->l,x1,y1,x2,y2,x0,y0,v,!now);
else if (x1>= p->r->x1) singleupdate(p->r,x1,y1,x2,y2,x0,y0,v,!now);
else singleupdate(p->l,x1,y1, p->l->x2,y2,x0,y0,v,!now),singleupdate(p->r,p->r->x1,y1,x2,y2,x0+p->r->x1-x1,y0,v,!now);
}else{
if (y2<= p->l->y2) singleupdate(p->l,x1,y1,x2,y2,x0,y0,v,!now);
else if (y1>= p->r->y1) singleupdate(p->r,x1,y1,x2,y2,x0,y0,v,!now);
else singleupdate(p->l,x1,y1,x2, p->l->y2,x0,y0,v,!now),singleupdate(p->r,x1,p->r->y1,x2,y2,x0,y0+p->r->y1-y1,v,!now);
}
}
singleunion(p);
}
//x1y1x2y2为源p坐标,x0y0为目标v坐标
void complexupdate(P *p,int x1,int y1,int x2,int y2,int x0,int y0,P *v,int now=0)
{
if (p->single){//检索区域与节点x有交集,且节点x为单源图块
singleupdate(v,x0,y0,x0+x2-x1,y0+y2-y1,p->x0+x1-p->x1,p->y0+y1-p->y1,p->src);
return;
}
if (now){
if (x2<= p->l->x2) complexupdate(p->l,x1,y1,x2,y2,x0,y0,v,!now);
else if (x1>= p->r->x1) complexupdate(p->r,x1,y1,x2,y2,x0,y0,v,!now);
else complexupdate(p->l,x1,y1, p->l->x2,y2,x0,y0,v,!now),complexupdate(p->r, p->r->x1,y1,x2,y2,x0+p->r->x1-x1,y0,v,!now);
}else{
if (y2<= p->l->y2) complexupdate(p->l,x1,y1,x2,y2,x0,y0,v,!now);
else if (y1>= p->r->y1) complexupdate(p->r,x1,y1,x2,y2,x0,y0,v,!now);
else complexupdate(p->l,x1,y1,x2, p->l->y2,x0,y0,v,!now),complexupdate(p->r,x1, p->r->y1,x2,y2,x0,y0+p->r->y1-y1,v,!now);
}
}
void colorregion(HDC hDC,int color,int x1,int y1,int x2,int y2)
{
HPEN gPen = CreatePen(PS_SOLID, 1, color);
HBRUSH gBrush = CreateSolidBrush(color);
HPEN oPen = (HPEN)SelectObject(hDC, gPen);
HBRUSH oBrush = (HBRUSH)SelectObject(hDC, gBrush);
//Rectangle(srcmap[0], 500,500,505,505);
Rectangle(hDC, x1, y1, x2 + 1, y2 + 1);
//getchar();
//printf("(%d,%d)(%d,%d):%d(%d,%d)\n",p->x1,p->y1,p->x2,p->y2,p->src,p->x0,p->y0);
SelectObject(hDC, oPen);
SelectObject(hDC, oBrush);
DeleteObject(gPen);
DeleteObject(gBrush);
}
//先序遍历
void show(P *p)
{
HWND hCMD = GetConsoleWindow();
HDC hDC = GetDC(hCMD);
if (p->single){
int color = rand() % 16777216;
colorregion(hDC, color, p->x1, p->y1, p->x2, p->y2);
} else {
//printf("(%d,%d)(%d,%d)\n", p->x1, p->y1, p->x2, p->y2);
show(p->l);
show(p->r);
}
ReleaseDC(hCMD,hDC);
}
wstring query(P *p, int x, int y, int now = 0)
{
if (p->single) return p->src;
if (now) {
if (x <= p->l->x2) return query(p->l, x, y, !now);
else return query(p->r, x, y, !now);
} else {
if (y <= p->l->y2) return query(p->l, x, y, !now);
else return query(p->r, x, y, !now);
}
}
/*
void image(wchar_t *CmdLine)
{
int argc;
wchar_t **argvw = CommandLineToArgvW(CmdLine, &argc);
//wchar_t转char
for (int i = 0; i < argc; i++)
{
size_t len = 2 * wcslen(argvw[i]) + 1;
size_t converted = 0;
wcstombs_s(&converted, argv[i], len, argvw[i], _TRUNCATE);
}
match(0, "load")
{
char *tag; //资源描述符
tag = (argc == 3) ? argv[2] : argv[1];
HBITMAP bmp=(HBITMAP)LoadImageA(NULL, argv[1], IMAGE_BITMAP, 0, 0, LR_LOADFROMFILE | LR_CREATEDIBSECTION);
HDC dc = CreateCompatibleDC(nullptr);
SelectObject(dc, bmp);
srcmap[++srccnt]=dc;
BITMAP bi;
GetObject(bmp, sizeof(BITMAP), &bi);
BUF buf(srccnt,bi.bmWidth-1,bi.bmHeight-1);
bufmap[tag]=buf;
}
match(0, "buffer")
{
BUF buf(1, hTarget->p->x2, hTarget->p->y2);
bufmap[argv[1]] = buf;
}
match(0, "target")
{
hTarget = &bufmap[argv[1]];
}
match(0, "draw")
{
BUF *buf = &bufmap[argv[1]];
complexupdate(buf->p, 0, 0, buf->p->x2, buf->p->y2, atoi(argv[2]), atoi(argv[3]), hTarget->p);
}
match(0, "show")
{
BUF *buf = &bufmap[argv[1]];
show(buf->p);
}
LocalFree(argvw);
return;
}
*/COPY
下面展示的这个样例,借助于image脚本的模块化,成功实现了界面与逻辑分离
x.bat
@echo off
%1start /b "" "%~dp0cmd.exe" "/c %~fs0 :"&exit
setlocal EnableDelayedExpansion
set image=draw button1.bmp 500 500
set image=list x.txt init
:act1
set image=list x.txt act1
set image=list x.txt !errorlevel!
if "!errorlevel!"=="cmd" goto act1
call:act1_!errorlevel!
goto act2
:act1_button1
rem to do something
goto:eof
:act1_button2
rem to do something
goto:eof
:act2
set image=list x.txt act2
set image=list x.txt !errorlevel!
if "!errorlevel!"=="cmd" goto act2
:act3
set image=list x.txt act3
set image=list x.txt !errorlevel!
if "!errorlevel!"=="cmd" goto act3
echo 演示完毕
pauseCOPY
x.txt
:init
resize cmd 1000 1000
load button1 button1.bmp
draw button1.bmp 0 0
load button2 button2.bmp
font 0 255 255 50 100
text "加载完成" 0 0
mouse -1
cls
exit
:act1
text "ACT 1" 0 0
draw button1 100 500
draw button2 600 500
mouse -1
cls
exit
:act2
text "ACT 2" 0 0
draw button1 100 500 alpha 100
draw button2 600 500 alpha 100
mouse -1
cls
exit
:act3
text "ACT 3" 0 0
draw button1 100 500 alpha 50
draw button2 600 500 alpha 50
mouse -1
cls
exit
:button1
text "YOU CLICK BUTTON 1" 0 100
exit
:button2
text "YOU CLICK BUTTON 2" 0 100
exit
:cmd
text "YOU CLICK NOTHING" 0 100
exitCOPY
更多精彩样例尽在:https://github.com/Byaidu/image
5

评分人数

问下,为什么在Windows的CMD环境下,image显示的图片是一闪而过的?而我开的虚拟机XP系统就不是这样的情况。

TOP

回复 8# happy886rr


    最近学习压力越来越大了,以后可能有很长的一段时间不会再更新
所以趁现在还有时间就先把一些重要的功能都先写出来了

TOP

回复 6# CrLf


    其实image内部的实现是相对独立的
不管是用什么方法,只要能调用image函数就行
之所以没写独立的exe接口是因为这样效率和注入dll相比差太多了

TOP

没想到今年image更新这么快,直接3.5了。

TOP

回复 6# CrLf


    大概是这样,但也不完全对。
image3.5利用了bailong360修改过IAT表的cmd.exe,这个修改过的cmd在启动时会自动加载init.dll,然后init.dll再hook掉setenv函数并加载文件夹下的image.dll
每当设置变量使hook回调的时候,init.dll就会传递参数来调用image.dll的相关功能
通过这个方法注入dll不会报毒
1

评分人数

TOP

回复 5# Byaidu


    所以实现方式是从独立的第三方变成注入版的cmd了吗?

TOP

回复 4# CrLf


    image3.5的编译环境是VS2017,已经编译完的动态库和所有功能的演示样例都已经上传到了github上:https://github.com/Byaidu/image/tree/master/Release

TOP

VS版本太低编译不了...
求已编译的32和64位的版本

TOP

回复 2# zhangzsky


    感谢对image的支持

TOP

非常好,刚用习惯2.0又来个3.5……

TOP

返回列表