发送消息、窗口管理统一通过句柄操作

This commit is contained in:
fofolee
2025-01-18 11:18:08 +08:00
parent 7a7cb8dd54
commit 0ee3647261
6 changed files with 1140 additions and 1183 deletions

View File

@@ -123,7 +123,10 @@ async function runAutomation(
}
}
} catch (err) {
error = err.toString().trim();
error = err
.toString()
.replace(/^Error: /, "")
.trim();
}
if (type === "inspect") return { error };

View File

@@ -2,7 +2,7 @@ const { runCsharpFeature } = require("../../csharp");
/**
* 执行消息发送操作
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"inspect"
* @param {string} type - 操作类型, 可选值: "keyboard"|"mouse"|"list"
* @param {Object} params - 参数对象
* @param {string} params.method - 查找方式:"title"|"handle"|"process"|"class"|"active"
* @param {string} params.window - 窗口标题、句柄、进程名、类名
@@ -47,7 +47,7 @@ async function runSendMessage(type, params = {}) {
}
break;
case "inspect":
case "list":
if (filter) {
args.push("-filter", filter);
}
@@ -61,54 +61,53 @@ async function runSendMessage(type, params = {}) {
try {
const result = await runCsharpFeature("sendmessage", args);
if (result) {
const jsonResult = JSON.parse(result);
if (type === "inspect") {
return jsonResult;
}
return { success: true, control: jsonResult };
const resultStr = result.toString().trim();
if (type === "list") return JSON.parse(resultStr);
if (resultStr === "true") return { success: true };
}
} catch (err) {
error = err
.toString()
.replace(/^Error: /, "")
.trim();
console.log(error);
}
if (type === "inspect") return [];
if (type === "list") return [];
return { success: false, error };
}
module.exports = {
sendKeys: (method, window, keys, options = {}) =>
sendKeys: (windowHandle, keys, options = {}) =>
runSendMessage("keyboard", {
method,
window,
method: windowHandle ? "handle" : "active",
window: windowHandle,
action: "keys",
value: keys,
options,
}),
sendText: (method, window, text, options = {}) =>
sendText: (windowHandle, text, options = {}) =>
runSendMessage("keyboard", {
method,
window,
method: windowHandle ? "handle" : "active",
window: windowHandle,
action: "text",
value: text,
options,
}),
click: (method, window, action = "click", options = {}) =>
click: (windowHandle, action = "click", options = {}) =>
runSendMessage("mouse", {
method,
window,
method: windowHandle ? "handle" : "active",
window: windowHandle,
action,
options,
}),
inspectWindow: (method, window, options = {}) =>
runSendMessage("inspect", {
method,
window,
listControls: (windowHandle, options = {}) =>
runSendMessage("list", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
options,
}),
};

View File

@@ -57,7 +57,7 @@ async function runWindow(type, params = {}) {
if (type === "info") {
return jsonResult;
}
return { success: true, window: jsonResult };
return { success: true };
}
} catch (err) {
error = err
@@ -72,27 +72,63 @@ async function runWindow(type, params = {}) {
}
module.exports = {
setTopMost: (method, window, isTopMost) =>
runWindow("topmost", { method, window, value: isTopMost }),
setTopMost: (windowHandle, isTopMost) =>
runWindow("topmost", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
value: isTopMost,
}),
setOpacity: (method, window, opacity) =>
runWindow("opacity", { method, window, value: opacity }),
setOpacity: (windowHandle, opacity) =>
runWindow("opacity", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
value: opacity,
}),
setWindowRect: (method, window, x, y, width, height) =>
runWindow("rect", { method, window, value: { x, y, width, height } }),
setWindowRect: (windowHandle, x, y, width, height) =>
runWindow("rect", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
value: { x, y, width, height },
}),
setWindowState: (method, window, state) =>
runWindow("state", { method, window, value: state }),
setWindowState: (windowHandle, state) =>
runWindow("state", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
value: state,
}),
closeWindow: (method, window) => runWindow("close", { method, window }),
closeWindow: (windowHandle) =>
runWindow("close", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
}),
setFocus: (method, window) => runWindow("focus", { method, window }),
setFocus: (windowHandle) =>
runWindow("focus", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
}),
setBorder: (method, window, hasBorder) =>
runWindow("border", { method, window, value: hasBorder }),
setBorder: (windowHandle, hasBorder) =>
runWindow("border", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
value: hasBorder,
}),
setClickThrough: (method, window, isTransparent) =>
runWindow("clickthrough", { method, window, value: isTransparent }),
setClickThrough: (windowHandle, isTransparent) =>
runWindow("clickthrough", {
method: windowHandle ? "handle" : "active",
window: windowHandle,
value: isTransparent,
}),
getWindowInfo: (method, window) => runWindow("info", { method, window }),
getWindowInfo: (method, window) =>
runWindow("info", {
method,
window,
}),
};