window自动化分类:1.新增选择窗口(inspect),支持高亮指定元素,并查看元素的名称、类型、句柄、xpath等各项属性及所在窗口各项属性 2.新增界面自动化,支持通过xpath、name、id等条件在对指定元素执行点击、设置值、模拟输入、高亮等操作

This commit is contained in:
fofolee
2025-01-17 18:51:11 +08:00
parent 61d711765b
commit 10b67c919a
11 changed files with 2203 additions and 2110 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -113,15 +113,13 @@ const buildCsharpFeature = async (feature) => {
fs.copyFile(srcCsPath, destCsPath, (err) => {
if (err) return reject(err.toString());
child_process.exec(
`${cscPath} /nologo ${references}/out:"${exePath}" "${destCsPath}"`,
{ encoding: null },
(err, stdout) => {
if (err) return reject(iconv.decode(stdout, "gbk"));
else resolve(iconv.decode(stdout, "gbk"));
fs.unlink(destCsPath, () => {});
}
);
const command = `${cscPath} /nologo ${references}/out:"${exePath}" "${destCsPath}"`;
console.log(command);
child_process.exec(command, { encoding: null }, (err, stdout) => {
if (err) return reject(iconv.decode(stdout, "gbk"));
else resolve(iconv.decode(stdout, "gbk"));
fs.unlink(destCsPath, () => {});
});
});
});
};
@@ -181,6 +179,11 @@ const runCsharpFeature = async (feature, args = [], options = {}) => {
encoding: null,
},
(err, stdout, stderr) => {
console.log({
err,
stdout: iconv.decode(stdout, "gbk"),
stderr: iconv.decode(stderr, "gbk"),
});
if (err || Buffer.byteLength(stderr) > 0)
reject(iconv.decode(stderr || stdout, "gbk"));
else reslove(iconv.decode(stdout, "gbk"));

View File

@@ -62,16 +62,20 @@ public class AutomationTool
[DllImport("user32.dll")]
private static extern bool GetClientRect(IntPtr hWnd, out RECT lpRect);
private const uint WM_KEYDOWN = 0x0100;
private const uint WM_KEYUP = 0x0101;
private const uint WM_KEYPRESS = 0x0102;
private const uint WM_CHAR = 0x0102;
private const uint WM_SETTEXT = 0x000C;
private const uint WM_LBUTTONDOWN = 0x0201;
private const uint WM_LBUTTONUP = 0x0202;
private const uint WM_RBUTTONDOWN = 0x0204;
private const uint WM_RBUTTONUP = 0x0205;
private const uint WM_LBUTTONDBLCLK = 0x0203;
private const int WM_KEYDOWN = 0x0100;
private const int WM_KEYUP = 0x0101;
private const int WM_CHAR = 0x0102;
private const int WM_SETTEXT = 0x000C;
private const int WM_LBUTTONDOWN = 0x0201;
private const int WM_LBUTTONUP = 0x0202;
private const int WM_RBUTTONDOWN = 0x0204;
private const int WM_RBUTTONUP = 0x0205;
private const int WM_LBUTTONDBLCLK = 0x0203;
// 添加按键状态标志
private const int KEYEVENTF_KEYDOWN = 0x0;
private const int KEYEVENTF_EXTENDEDKEY = 0x1;
private const int KEYEVENTF_KEYUP = 0x2;
private delegate bool EnumWindowProc(IntPtr hwnd, IntPtr lParam);
private delegate bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam);
@@ -131,6 +135,11 @@ public class AutomationTool
[DllImport("user32.dll")]
private static extern int GetWindowThreadProcessId(IntPtr hWnd, out int lpdwProcessId);
private const uint MAPVK_VK_TO_VSC = 0x00;
private const uint MAPVK_VSC_TO_VK = 0x01;
private const uint MAPVK_VK_TO_CHAR = 0x02;
private const uint MAPVK_VSC_TO_VK_EX = 0x03;
#endregion
public static void Main(string[] args)
@@ -460,20 +469,54 @@ public class AutomationTool
// 按下修饰键
foreach (byte modifier in modifierKeys)
{
PostMessage(hWnd, WM_KEYDOWN, modifier, 0);
// 获取扫描码
uint scanCode = MapVirtualKey((uint)modifier, MAPVK_VK_TO_VSC);
// 构造 lParam
int lParamDown = 0x00000001 | // repeat count = 1
((int)scanCode << 16) | // scan code
(0x1 << 24); // extended key for modifiers
PostMessage(hWnd, WM_KEYDOWN, modifier, lParamDown);
Thread.Sleep(10); // 短暂延迟确保修饰键被正确识别
}
// 发送主键字符
// 发送主键
if (mainKey > 0)
{
PostMessage(hWnd, WM_CHAR, mainKey, 0);
// 获取主键的扫描码
uint scanCode = MapVirtualKey((uint)mainKey, MAPVK_VK_TO_VSC);
// 构造主键的 lParam
int lParamDown = 0x00000001 | // repeat count = 1
((int)scanCode << 16); // scan code
int lParamUp = 0x00000001 | // repeat count = 1
((int)scanCode << 16) | // scan code
(0xC0 << 24); // key up + previous key state
// 发送按键按下
PostMessage(hWnd, WM_KEYDOWN, mainKey, lParamDown);
Thread.Sleep(10); // 短暂延迟
// 发送按键释放
PostMessage(hWnd, WM_KEYUP, mainKey, lParamUp);
Thread.Sleep(10); // 短暂延迟
}
// 释放修饰键
// 释放修饰键(反序释放)
for (int i = modifierKeys.Count - 1; i >= 0; i--)
{
byte modifier = modifierKeys[i];
PostMessage(hWnd, WM_KEYUP, modifier, 0);
uint scanCode = MapVirtualKey((uint)modifier, MAPVK_VK_TO_VSC);
// 构造释放修饰键的 lParam
int lParamUp = 0x00000001 | // repeat count = 1
((int)scanCode << 16) | // scan code
(0xC1 << 24); // extended key + key up + previous key state
PostMessage(hWnd, WM_KEYUP, modifier, lParamUp);
Thread.Sleep(10); // 短暂延迟
}
// 如果有多个按键组合,等待一下
@@ -484,58 +527,6 @@ public class AutomationTool
}
}
private static bool IsSpecialKey(byte vKey)
{
switch (vKey)
{
// 修饰键
case 0xA0: // VK_LSHIFT
case 0xA2: // VK_LCONTROL
case 0xA4: // VK_LMENU
case 0x5B: // VK_LWIN
// 控制键
case 0x08: // VK_BACK
case 0x09: // VK_TAB
case 0x0D: // VK_RETURN
case 0x1B: // VK_ESCAPE
case 0x20: // VK_SPACE
case 0x2E: // VK_DELETE
// 方向键
case 0x25: // VK_LEFT
case 0x26: // VK_UP
case 0x27: // VK_RIGHT
case 0x28: // VK_DOWN
// 导航键
case 0x24: // VK_HOME
case 0x23: // VK_END
case 0x21: // VK_PRIOR (PageUp)
case 0x22: // VK_NEXT (PageDown)
case 0x2D: // VK_INSERT
// 功能键
case 0x70: // VK_F1
case 0x71: // VK_F2
case 0x72: // VK_F3
case 0x73: // VK_F4
case 0x74: // VK_F5
case 0x75: // VK_F6
case 0x76: // VK_F7
case 0x77: // VK_F8
case 0x78: // VK_F9
case 0x79: // VK_F10
case 0x7A: // VK_F11
case 0x7B: // VK_F12
// 其他常用键
case 0x14: // VK_CAPITAL
case 0x90: // VK_NUMLOCK
case 0x91: // VK_SCROLL
case 0x2C: // VK_SNAPSHOT
case 0x13: // VK_PAUSE
return true;
default:
return false;
}
}
private static void SendText(IntPtr hWnd, string text)
{
StringBuilder sb = new StringBuilder(text);
@@ -554,56 +545,74 @@ public class AutomationTool
return null;
}
private static bool HasArgument(string[] args, string key)
{
return Array.Exists(args, arg => arg.Equals(key, StringComparison.OrdinalIgnoreCase));
}
private static byte GetVirtualKeyCode(string key)
{
switch (key.ToLower())
switch (key.ToUpper())
{
case "ctrl":
case "^": return 0xA2; // VK_LCONTROL
case "alt": return 0xA4; // VK_LMENU
case "shift": return 0xA0; // VK_LSHIFT
case "win": return 0x5B; // VK_LWIN
case "enter": return 0x0D;
case "tab": return 0x09;
case "esc": return 0x1B;
case "space": return 0x20;
case "backspace": return 0x08;
case "delete": return 0x2E;
// 方向键
case "left": return 0x25; // VK_LEFT
case "up": return 0x26; // VK_UP
case "right": return 0x27; // VK_RIGHT
case "down": return 0x28; // VK_DOWN
// 导航键
case "home": return 0x24; // VK_HOME
case "end": return 0x23; // VK_END
case "pageup": return 0x21; // VK_PRIOR
case "pagedown": return 0x22; // VK_NEXT
case "insert": return 0x2D; // VK_INSERT
// 功能键
case "f1": return 0x70; // VK_F1
case "f2": return 0x71;
case "f3": return 0x72;
case "f4": return 0x73;
case "f5": return 0x74;
case "f6": return 0x75;
case "f7": return 0x76;
case "f8": return 0x77;
case "f9": return 0x78;
case "f10": return 0x79;
case "f11": return 0x7A;
case "f12": return 0x7B;
// 其他常用键
case "capslock": return 0x14; // VK_CAPITAL
case "numlock": return 0x90; // VK_NUMLOCK
case "scrolllock": return 0x91; // VK_SCROLL
case "printscreen": return 0x2C; // VK_SNAPSHOT
case "pause": return 0x13; // VK_PAUSE
// 修饰键
case "CTRL":
case "^": return 0x11; // VK_CONTROL
case "ALT":
case "%": return 0x12; // VK_MENU
case "SHIFT": return 0x10; // VK_SHIFT
// 特殊按键
case "{BACKSPACE}":
case "{BS}":
case "{BKSP}": return 0x08; // VK_BACK
case "{BREAK}": return 0x03; // VK_CANCEL
case "{CAPSLOCK}": return 0x14; // VK_CAPITAL
case "{DELETE}":
case "{DEL}": return 0x2E; // VK_DELETE
case "{DOWN}": return 0x28; // VK_DOWN
case "{END}": return 0x23; // VK_END
case "{ENTER}":
case "{RETURN}": return 0x0D; // VK_RETURN
case "{ESC}": return 0x1B; // VK_ESCAPE
case "{HELP}": return 0x2F; // VK_HELP
case "{HOME}": return 0x24; // VK_HOME
case "{INSERT}":
case "{INS}": return 0x2D; // VK_INSERT
case "{LEFT}": return 0x25; // VK_LEFT
case "{NUMLOCK}": return 0x90; // VK_NUMLOCK
case "{PGDN}": return 0x22; // VK_NEXT
case "{PGUP}": return 0x21; // VK_PRIOR
case "{PRTSC}": return 0x2C; // VK_SNAPSHOT
case "{RIGHT}": return 0x27; // VK_RIGHT
case "{SCROLLLOCK}": return 0x91; // VK_SCROLL
case "{TAB}": return 0x09; // VK_TAB
case "{UP}": return 0x26; // VK_UP
// 功能键 F1-F16
case "{F1}": return 0x70;
case "{F2}": return 0x71;
case "{F3}": return 0x72;
case "{F4}": return 0x73;
case "{F5}": return 0x74;
case "{F6}": return 0x75;
case "{F7}": return 0x76;
case "{F8}": return 0x77;
case "{F9}": return 0x78;
case "{F10}": return 0x79;
case "{F11}": return 0x7A;
case "{F12}": return 0x7B;
// 数字键盘
case "{ADD}": return 0x6B; // VK_ADD
case "{SUBTRACT}": return 0x6D; // VK_SUBTRACT
case "{MULTIPLY}": return 0x6A; // VK_MULTIPLY
case "{DIVIDE}": return 0x6F; // VK_DIVIDE
case "{NUMPAD0}": return 0x60; // VK_NUMPAD0
case "{NUMPAD1}": return 0x61;
case "{NUMPAD2}": return 0x62;
case "{NUMPAD3}": return 0x63;
case "{NUMPAD4}": return 0x64;
case "{NUMPAD5}": return 0x65;
case "{NUMPAD6}": return 0x66;
case "{NUMPAD7}": return 0x67;
case "{NUMPAD8}": return 0x68;
case "{NUMPAD9}": return 0x69;
default:
if (key.Length == 1)
{

View File

@@ -316,13 +316,20 @@ public class WindowManager
GetWindowText(hwnd, title, title.Capacity);
GetClassName(hwnd, className, className.Capacity);
// 获取窗口位置和大小
RECT rect = new RECT();
GetWindowRect(hwnd, out rect);
// 获取进程信息
uint processId = 0;
GetWindowThreadProcessId(hwnd, out processId);
string processName = "";
string processPath = "";
try
{
var process = Process.GetProcessById((int)processId);
processName = process.ProcessName;
processPath = process.MainModule.FileName;
}
catch { }
@@ -331,7 +338,12 @@ public class WindowManager
{ "handle", hwnd.ToInt64() },
{ "title", title.ToString() },
{ "class", className.ToString() },
{ "process", processName }
{ "x", rect.Left },
{ "y", rect.Top },
{ "width", rect.Right - rect.Left },
{ "height", rect.Bottom - rect.Top },
{ "processName", processName },
{ "processPath", processPath }
};
}

View File

@@ -1,168 +1,144 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 列出所有元素
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @returns {object[]} 元素列表
* 执行自动化操作
* @param {string} type - 操作类型, 可选值: "inspect"|"click"|"setvalue"|"getvalue"|"select"|"expand"|"scroll"|"wait"|"focus"|"highlight"
* @param {Object} params - 参数对象
* @param {string} params.by - 查找方式:"xpath"|"id"|"name"|"condition"
* @param {string} params.searchValue - 搜索值
* @param {string} params.window - 窗口标题、句柄、进程名、类名
* @param {Object} params.options - 附加选项
* @returns {Promise<Object>} - 操作结果
*/
const listElements = async function (method, window, options = {}) {
const { filter, scope } = options;
const args = ["-type", "list"];
async function runAutomation(
type,
window = "",
by = "xpath",
searchValue = "",
params = {}
) {
const args = [];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
if (scope) args.push("-scope", scope);
try {
const result = await runCsharpFeature("automation", args);
console.log(result);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
if (window) {
args.push("-window", window);
}
return [];
};
/**
* 点击元素
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} by 查找方式:"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {object} options 选项
* @param {string} options.pattern 点击模式:"invoke"|"toggle"
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const clickElement = async function (method, window, by, value, options = {}) {
const { pattern = "invoke", background = false } = options;
const args = ["-type", "click", "-by", by, "-value", value];
args.push("-type", type);
// 通用参数处理
if (type !== "inspect") {
switch (by) {
case "xpath":
args.push("-xpath", searchValue);
break;
case "id":
args.push("-id", searchValue);
break;
case "name":
args.push("-name", searchValue);
break;
case "condition":
args.push("-condition", searchValue);
break;
}
}
// 特定命令的参数处理
switch (type) {
case "inspect":
if (params) {
args.push("-position");
if (params.x && params.y) {
args.push(`${params.x},${params.y}`);
}
}
break;
case "setvalue":
if (params.newValue !== undefined) {
args.push("-value", params.newValue);
}
if (params.sendenter) {
args.push("-sendenter");
}
break;
case "select":
if (params.item) {
args.push("-item", params.item);
}
break;
case "expand":
if (params.expand !== undefined) {
args.push("-expand", params.expand);
}
break;
case "scroll":
if (params.direction) {
args.push("-direction", params.direction);
}
if (params.amount !== undefined) {
args.push("-amount", params.amount);
}
break;
case "wait":
if (params.condition) {
args.push("-condition", params.condition);
}
if (params.timeout !== undefined) {
args.push("-timeout", params.timeout);
}
break;
case "highlight":
if (params.duration !== undefined) {
args.push("-duration", params.duration);
}
break;
case "sendkeys":
if (params.keys) {
args.push("-keys", params.keys);
}
break;
}
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (pattern) args.push("-pattern", pattern);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) {
return { success: true, element: JSON.parse(result) };
const resultStr = result.toString().trim();
if (type === "inspect") return JSON.parse(resultStr);
if (resultStr === "true") return { success: true };
try {
return { success: true, data: JSON.parse(resultStr) };
} catch (err) {
return { success: true, data: resultStr };
}
}
} catch (err) {
error = err.toString();
error = err.toString().trim();
}
return { success: true, error };
};
/**
* 设置元素值
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} by 查找方式:"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {string} newValue 要设置的值
* @param {object} options 选项
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} element 操作的元素信息
* @property {Object} element.window 操作的元素所在的窗口信息
*/
const setElementValue = async function (
method,
window,
by,
value,
newValue,
options = {}
) {
const { background = false } = options;
const args = [
"-type",
"setvalue",
"-by",
by,
"-value",
value,
"-newvalue",
newValue,
];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return { success: true, element: JSON.parse(result) };
} catch (err) {
error = err.toString();
}
if (type === "inspect") return { error };
return { success: false, error };
};
/**
* 获取元素值
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} by 查找方式:"name"|"class"|"type"|"automationid"
* @param {string} value 查找值
* @param {object} options 选项
* @param {boolean} options.background 是否后台操作
* @returns {object} 元素值
*/
const getElementValue = async function (
method,
window,
by,
value,
options = {}
) {
const { background = false } = options;
const args = ["-type", "getvalue", "-by", by, "-value", value];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (background) args.push("-background");
let error;
try {
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
error = err.toString();
}
return { success: false, error };
};
/**
* 检查元素
* @param {object} options 选项
* @param {number} options.timeout 超时时间(秒)
* @returns {object} 元素信息
*/
const inspectElement = async function () {
const args = ["-type", "inspect"];
try {
const result = await runCsharpFeature("automation", args);
if (result) return JSON.parse(result);
} catch (err) {
console.log(err);
}
return [];
};
}
module.exports = {
listElements,
clickElement,
setElementValue,
getElementValue,
inspectElement,
inspect: () => runAutomation("inspect"),
inspectPosition: (position) =>
runAutomation("inspect", null, null, null, position || {}),
click: (...args) => runAutomation("click", ...args),
setvalue: (...args) => runAutomation("setvalue", ...args),
getvalue: (...args) => runAutomation("getvalue", ...args),
select: (...args) => runAutomation("select", ...args),
expand: (...args) => runAutomation("expand", ...args),
scroll: (...args) => runAutomation("scroll", ...args),
wait: (...args) => runAutomation("wait", ...args),
focus: (...args) => runAutomation("focus", ...args),
highlight: (...args) => runAutomation("highlight", ...args),
sendkeys: (...args) => runAutomation("sendkeys", ...args),
};

View File

@@ -1,140 +1,114 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 键盘操作
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} keys 键盘按键
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
* 执行消息发送操作
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"inspect"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题、句柄、进程名、类名
* @param {string} params.action - 动作类型,如 "keys"、"text"、"click" 等
* @param {string} params.value - 操作的值,如按键、文本等
* @param {Object} params.options - 附加选项
* @returns {Promise<Object>} - 操作结果
*/
const sendKeys = async function (method, window, keys, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "keys", "-value", keys];
async function runSendMessage(type, params = {}) {
const args = ["-type", type];
const { method = "active", window, action, value, options = {} } = params;
const { control, text, pos, filter, background = false } = options;
// 通用参数
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return {
success: true,
control: JSON.parse(result),
};
}
} catch (err) {
error = err.toString();
if (method !== "active" && window) {
args.push("-window", window);
}
return { success: false, error };
};
/**
* 发送文本
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} text 文本
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const sendText = async function (method, window, text, options = {}) {
const { control, background = false } = options;
const args = ["-type", "keyboard", "-action", "text", "-value", text];
// 特定命令的参数处理
switch (type) {
case "keyboard":
args.push("-action", action);
if (value) {
args.push("-value", value);
}
if (control) {
args.push("-control", control);
}
break;
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
}
} catch (err) {
error = err.toString();
case "mouse":
args.push("-action", action);
if (control) {
args.push("-control", control);
}
if (text) {
args.push("-text", text);
}
if (pos) {
args.push("-pos", pos);
}
break;
case "inspect":
if (filter) {
args.push("-filter", filter);
}
break;
}
return { success: false, error };
};
/**
* 鼠标点击
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {string} action 动作:"click"|"doubleClick"|"rightClick"
* @param {object} options 选项
* @param {string} options.control 控件类名
* @param {string} options.text 控件文本
* @param {string} options.pos 点击位置x,y
* @param {boolean} options.background 是否后台操作
* @returns {Object} 操作结果
* @property {boolean} success 是否成功
* @property {Object} control 控件信息
* @property {string} control.window 控件所在窗口的信息
*/
const click = async function (method, window, action = "click", options = {}) {
const { control, text, pos, background = false } = options;
const args = ["-type", "mouse", "-action", action];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (control) args.push("-control", control);
if (text) args.push("-text", text);
if (pos) args.push("-pos", pos);
// 后台操作参数
args.push("-background", background.toString());
let error;
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
return { success: true, control: JSON.parse(result) };
const jsonResult = JSON.parse(result);
if (type === "inspect") {
return jsonResult;
}
return { success: true, control: jsonResult };
}
} catch (err) {
error = err.toString();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
}
if (type === "inspect") return [];
return { success: false, error };
};
/**
* 获取窗口控件树
* @param {string} method 窗口类型:"title"|"handle"|"process"|"class"|"active"
* @param {string} window 窗口标题、句柄、进程名、类名
* @param {object} options 选项
* @param {string} options.filter 过滤条件
* @param {boolean} options.background 是否后台操作
* @returns {object} 控件树
*/
const inspectWindow = async function (method, window, options = {}) {
const { filter, background = false } = options;
const args = ["-type", "inspect"];
args.push("-method", method);
if (method !== "active" && window) args.push("-window", window);
if (filter) args.push("-filter", filter);
args.push("-background", background.toString());
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) return JSON.parse(result);
} catch (error) {
console.log(error);
}
return [];
};
}
module.exports = {
sendKeys,
sendText,
click,
inspectWindow,
sendKeys: (method, window, keys, options = {}) =>
runSendMessage("keyboard", {
method,
window,
action: "keys",
value: keys,
options,
}),
sendText: (method, window, text, options = {}) =>
runSendMessage("keyboard", {
method,
window,
action: "text",
value: text,
options,
}),
click: (method, window, action = "click", options = {}) =>
runSendMessage("mouse", {
method,
window,
action,
options,
}),
inspectWindow: (method, window, options = {}) =>
runSendMessage("inspect", {
method,
window,
options,
}),
};

View File

@@ -1,271 +1,98 @@
const { runCsharpFeature } = require("../../csharp");
/**
* 窗口置顶,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被置顶的窗口信息
* 执行窗口操作
* @param {string} type - 操作类型, 可选值: "topmost"|"opacity"|"rect"|"state"|"close"|"focus"|"border"|"clickthrough"|"info"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题、句柄、进程名、类名
* @param {*} params.value - 操作的值,不同操作类型对应不同的值类型
* @returns {Promise<Object>} - 操作结果
*/
async function setTopMost(method, value, isTopMost) {
const args = [
"-type",
"topmost",
"-method",
method,
"-window",
value,
"-value",
isTopMost.toString(),
];
async function runWindow(type, params = {}) {
const args = ["-type", type];
const { method = "active", window, value } = params;
// 通用参数
args.push("-method", method);
if (window) {
args.push("-window", window);
}
// 特定命令的参数处理
switch (type) {
case "topmost":
case "border":
case "clickthrough":
if (value !== undefined) {
args.push("-value", value.toString());
}
break;
case "opacity":
if (value !== undefined) {
args.push("-value", value.toString());
}
break;
case "rect":
if (value && typeof value === "object") {
const { x = 0, y = 0, width = 0, height = 0 } = value;
args.push("-value", `${x},${y},${width},${height}`);
}
break;
case "state":
if (value) {
args.push("-value", value);
}
break;
}
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return {
success: true,
window: JSON.parse(windowInfo),
};
const result = await runCsharpFeature("window", args);
if (result) {
const jsonResult = JSON.parse(result);
if (type === "info") {
return jsonResult;
}
return { success: true, window: jsonResult };
}
} catch (err) {
error = err.toString();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
console.log(error);
}
return { success: false, error };
}
/**
* 设置窗口透明度,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {number} opacity 透明度 0-100
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置透明度的窗口信息
*/
async function setOpacity(method, value, opacity) {
const args = [
"-type",
"opacity",
"-method",
method,
"-window",
value,
"-value",
opacity.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
if (type === "info") return [];
return { success: false, error };
}
/**
* 设置窗口位置和大小,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {number} x X坐标
* @param {number} y Y坐标
* @param {number} width 宽度
* @param {number} height 高度
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置位置和大小的窗口信息
*/
async function setWindowRect(method, value, x, y, width, height) {
const args = [
"-type",
"rect",
"-method",
method,
"-window",
value,
"-value",
`${x},${y},${width},${height}`,
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口状态,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {string} state 状态:"normal"|"maximize"|"minimize"
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置状态的窗口信息
*/
async function setWindowState(method, value, state) {
const args = [
"-type",
"state",
"-method",
method,
"-window",
value,
"-value",
state,
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 关闭窗口,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被关闭的窗口信息
*/
async function closeWindow(method, value) {
const args = ["-type", "close", "-method", method, "-window", value];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口焦点,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置焦点的窗口信息
*/
async function setFocus(method, value) {
const args = ["-type", "focus", "-method", method, "-window", value];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口边框,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {boolean} hasBorder 是否显示边框
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置边框的窗口信息
*/
async function setBorder(method, value, hasBorder) {
const args = [
"-type",
"border",
"-method",
method,
"-window",
value,
"-value",
hasBorder.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 设置窗口点击穿透,只操作第一个找到的窗口
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @param {boolean} isTransparent 是否点击穿透
* @param {Object} result 结果
* @param {boolean} result.success 是否成功
* @param {Object} result.window 被设置点击穿透的窗口信息
*/
async function setClickThrough(method, value, isTransparent) {
const args = [
"-type",
"clickthrough",
"-method",
method,
"-window",
value,
"-value",
isTransparent.toString(),
];
let error;
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) {
return { success: true, window: JSON.parse(windowInfo) };
}
} catch (err) {
error = err.toString();
}
return { success: false, error };
}
/**
* 获取窗口信息,返回所有匹配的窗口信息
* @param {string} method 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} value 窗口标题、句柄、进程名、类名
* @returns {Object} 所有匹配的窗口信息
*/
async function getWindowInfo(method, value) {
const args = ["-type", "info", "-method", method, "-window", value];
try {
const windowInfo = await runCsharpFeature("window", args);
if (windowInfo) return JSON.parse(windowInfo);
} catch (err) {
console.log(err);
}
return [];
}
module.exports = {
setTopMost,
setOpacity,
setWindowRect,
setWindowState,
closeWindow,
setFocus,
setBorder,
setClickThrough,
getWindowInfo,
setTopMost: (method, window, isTopMost) =>
runWindow("topmost", { method, window, value: isTopMost }),
setOpacity: (method, window, opacity) =>
runWindow("opacity", { method, window, value: opacity }),
setWindowRect: (method, window, x, y, width, height) =>
runWindow("rect", { method, window, value: { x, y, width, height } }),
setWindowState: (method, window, state) =>
runWindow("state", { method, window, value: state }),
closeWindow: (method, window) => runWindow("close", { method, window }),
setFocus: (method, window) => runWindow("focus", { method, window }),
setBorder: (method, window, hasBorder) =>
runWindow("border", { method, window, value: hasBorder }),
setClickThrough: (method, window, isTransparent) =>
runWindow("clickthrough", { method, window, value: isTransparent }),
getWindowInfo: (method, window) => runWindow("info", { method, window }),
};