From 7a7cb8dd544496794f12cd1b5b34455fa1e4ad56 Mon Sep 17 00:00:00 2001 From: fofolee Date: Sat, 18 Jan 2025 09:02:00 +0800 Subject: [PATCH] =?UTF-8?q?=E7=95=8C=E9=9D=A2=E8=87=AA=E5=8A=A8=E5=8C=96?= =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E5=90=AF=E7=94=A8=E7=A6=81=E7=94=A8=E5=85=83?= =?UTF-8?q?=E7=B4=A0=E5=8A=9F=E8=83=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- plugin/lib/csharp/automation.cs | 53 +++++++++++-------- .../lib/quickcomposer/windows/automation.js | 16 +++--- src/js/composer/commands/windowsCommands.js | 29 +++++++--- 3 files changed, 64 insertions(+), 34 deletions(-) diff --git a/plugin/lib/csharp/automation.cs b/plugin/lib/csharp/automation.cs index 175aa4d..704e2da 100644 --- a/plugin/lib/csharp/automation.cs +++ b/plugin/lib/csharp/automation.cs @@ -94,6 +94,9 @@ public class AutomationManager [DllImport("user32.dll")] private static extern uint GetWindowThreadProcessId(IntPtr hWnd, out uint processId); + [DllImport("user32.dll")] + private static extern bool EnableWindow(IntPtr hWnd, bool bEnable); + [StructLayout(LayoutKind.Sequential)] private struct RECT { @@ -121,6 +124,7 @@ public class AutomationManager private static Form overlayForm; private static Form previewForm; private static AutomationElement lastElement; + private static Point lastCursorPos; private static bool completed; private static CacheRequest CreateCacheRequest() @@ -318,6 +322,11 @@ public class AutomationManager SendKeys(GetTargetElement(args), keys); break; + case "enable": + bool enable = GetArgumentValue(args, "-enable") == "true"; + EnableElement(GetTargetElement(args), enable); + break; + default: throw new Exception(string.Format("不支持的操作类型: {0}", type)); } @@ -586,18 +595,10 @@ public class AutomationManager - {DIVIDE} - 数字键盘除号键 - {NUMPAD0} - {NUMPAD9} - 数字键盘数字键 - 修饰键: - + (加号) - SHIFT - ^ (脱字号) - CTRL - % (百分号) - ALT - +12. enable - 启用/禁用元素 + 参数: -xpath -enable [-window <窗口句柄>] 示例: - - 输入文本: -keys ""Hello"" - - 按Enter键: -keys ""{ENTER}"" - - 按Ctrl+C: -keys ""^c"" - - 按Ctrl+Home: -keys ""^{HOME}"" - - 按Alt+F4: -keys ""%{F4}"" - - 按Shift+Tab: -keys ""+{TAB}"" + - 启用按钮: -xpath ""//Button[@Name='确定']"" -enable true 通用参数: -window <窗口句柄> 指定要操作的窗口,如果不指定则使用当前活动窗口 @@ -1127,6 +1128,23 @@ public class AutomationManager }; } + private static void EnableElement(AutomationElement element, bool enable) + { + if (element == null) + { + throw new Exception("元素不能为空"); + } + try + { + EnableWindow((IntPtr)element.Current.NativeWindowHandle, enable); + Console.WriteLine("true"); + } + catch (Exception ex) + { + throw new Exception("无法更改元素的启用/禁用状态: " + ex.Message); + } + } + private static void ClickElement(AutomationElement element) { if (element == null) @@ -1310,7 +1328,7 @@ public class AutomationManager completed = false; // 使用静态字段 Rectangle lastRect = Rectangle.Empty; lastElement = null; - Point lastCursorPos = new Point(); + lastCursorPos = new Point(); AutomationElement taskbarElement = null; List taskbarChildren = null; @@ -1474,12 +1492,7 @@ public class AutomationManager { try { - mouseTimer.Stop(); - overlayForm.Hide(); - previewForm.Hide(); - InspectElementInfo(lastElement, lastCursorPos); - completed = true; } catch (Exception ex) { @@ -1487,8 +1500,7 @@ public class AutomationManager } finally { - overlayForm.Close(); - previewForm.Close(); + stopInspect(); } } }; @@ -1684,6 +1696,7 @@ public class AutomationManager Clipboard.SetText(lastElement.Current.Name); if (e.KeyChar == 'x' || e.KeyChar == 'X') { + InspectElementInfo(lastElement, lastCursorPos); stopInspect(); } } @@ -1694,6 +1707,4 @@ public class AutomationManager } } } - - } diff --git a/plugin/lib/quickcomposer/windows/automation.js b/plugin/lib/quickcomposer/windows/automation.js index 4587175..a834d37 100644 --- a/plugin/lib/quickcomposer/windows/automation.js +++ b/plugin/lib/quickcomposer/windows/automation.js @@ -64,7 +64,7 @@ async function runAutomation( break; case "select": - if (params.item) { + if (params.item !== undefined) { args.push("-item", params.item); } break; @@ -76,7 +76,7 @@ async function runAutomation( break; case "scroll": - if (params.direction) { + if (params.direction !== undefined) { args.push("-direction", params.direction); } if (params.amount !== undefined) { @@ -85,9 +85,6 @@ async function runAutomation( break; case "wait": - if (params.condition) { - args.push("-condition", params.condition); - } if (params.timeout !== undefined) { args.push("-timeout", params.timeout); } @@ -100,10 +97,16 @@ async function runAutomation( break; case "sendkeys": - if (params.keys) { + if (params.keys !== undefined) { args.push("-keys", params.keys); } break; + + case "enable": + if (params.enable !== undefined) { + args.push("-enable", params.enable); + } + break; } let error; @@ -144,4 +147,5 @@ module.exports = { focus: (...args) => runAutomation("focus", ...args), highlight: (...args) => runAutomation("highlight", ...args), sendkeys: (...args) => runAutomation("sendkeys", ...args), + enable: (...args) => runAutomation("enable", ...args), }; diff --git a/src/js/composer/commands/windowsCommands.js b/src/js/composer/commands/windowsCommands.js index 63a61db..b81d825 100644 --- a/src/js/composer/commands/windowsCommands.js +++ b/src/js/composer/commands/windowsCommands.js @@ -491,6 +491,28 @@ export const windowsCommands = { }, ], }, + { + value: "quickcomposer.windows.automation.enable", + label: "启用/禁用元素", + icon: "toggle_on", + config: [ + { + component: "OptionEditor", + options: { + enable: { + component: "ButtonGroup", + options: [ + { label: "启用", value: true }, + { label: "禁用", value: false }, + ], + }, + }, + defaultValue: { + enable: true, + }, + }, + ], + }, { value: "quickcomposer.windows.automation.expand", label: "展开/折叠", @@ -559,13 +581,6 @@ export const windowsCommands = { { component: "OptionEditor", options: { - condition: { - label: "条件", - component: "VariableInput", - icon: "filter_alt", - width: 8, - placeholder: "name=xx;type=Button", - }, timeout: { label: "超时(秒)", component: "NumberInput",