[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖
试试将块转成action或func委托 ,例如
  1. [func[xxx,yyy]]{$args[0].ByAutomationId("CommandButton_7")}
复制代码
xxx换成参数类型
yyy换成返回类型

TOP

回复 7# 小白龙


$modalWindows[0]的问题
就如报错说明的那样[System.Char] 不包含名为“FindFirstDescendant”的方法。也就是 $modalWindows[0] 类型为 [System.Char] ,没有叫做  FindFirstDescendant 的方法
[FlaUI.Core.AutomationElements.AutomationElement] 类型才有名为 FindFirstDescendant 的方法 ,请确保 $modalWindows[0] 的类型为 [FlaUI.Core.AutomationElements.AutomationElement]

TOP

回复 9# 小白龙


试试
  1. function CloseWindowWithDontSave {
  2.     param($window)
  3.     $window.Close()
  4.     [FlaUI.Core.Input.Wait]::UntilInputIsProcessed()
  5.     # 获取窗口的模态对话框
  6.     $modalWindows = [FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1)).Result
  7.     # 查找 "不保存" 按钮
  8.     if ([FlaUI.Core.Tools.OperatingSystem]::IsWindows11()) {
  9.         $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()
  10.     } else {
  11.         $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()
  12.     }
  13.     # 点击 "不保存" 按钮
  14.     $dontSaveButton.Invoke()
  15. }
复制代码

TOP

回复 11# 小白龙


    试试出错那行换成 ,不行的话估计得调用泛型版本的WhileEmpty方法 ,ps7.3之前调用net泛型方法挺麻烦的 ,自己网上搜索 ,或者干脆试试直接换成 $modalWindows=$window.ModalWindows
  1. $modalWindows = [FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1),$null,$false,$false,$null).Result
复制代码

TOP

回复 14# 小白龙


    将
  1. $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton").AsButton()
复制代码
  1. $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ).AsButton()
复制代码
换成
  1. $dontSaveButton=[FlaUI.Core.AutomationElements.AutomationElementExtensions]::AsButton($modalWindows[0].FindFirstDescendant("SecondaryButton"))
复制代码
  1. $dontSaveButton=[FlaUI.Core.AutomationElements.AutomationElementExtensions]::AsButton($modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} ))
复制代码

TOP

回复 16# 小白龙


    貌似是这样的 ,错误是由flaui抛出的 ,并不是ps本身的错误
原来c#的是测试代码 ,ps代码的话自己根据实际来 ,可以看看ps代码里的那个$dontSaveButton是什么

TOP

回复 18# 小白龙


    这样的话 ,调用那个 FindFirstDescendant 方法的参数就不是SecondaryButton和CommandButton_7了
看错误提示那个#10000 ,大概是用窗口类名的 ,自己换成记事本对应的窗口类 ,比如#32770是对话框的类名
还有如果不是用ps代码启动的记事本 ,还得找到记事本的窗口然后调用CloseWindowWithDontSave函数 ,总之就自己按照实际来
;
像这种还不直接调winapi ,FindWindow FindWindowEx SendMessage简单

TOP

本帖最后由 Five66 于 2025-3-25 00:54 编辑

回复 20# 小白龙


    参考下面的 ,先打开记事本程序 ,然后往记事本里随便输入一些东西 ,接着运行代码
  1. add-type -LiteralPath ($pwd.path+'\FlaUI.Core.dll')
  2. add-type -LiteralPath ($pwd.path+'\Interop.UIAutomationClient.dll')
  3. add-type -LiteralPath ($pwd.path+'\FlaUI.UIA3.dll')
  4. function CloseWindowWithDontSave {
  5.    param($window)
  6.    $window.Close()
  7.    [FlaUI.Core.Input.Wait]::UntilInputIsProcessed()
  8.     # 获取窗口的模态对话框
  9.     $modalWindows=[FlaUI.Core.Tools.Retry]::WhileEmpty([func[[FlaUI.Core.AutomationElements.Window[]]]]{$window.ModalWindows}, [TimeSpan]::FromSeconds(1),$null,$false,$false,$null).Result
  10.     # 查找 "不保存" 按钮
  11.     if ([FlaUI.Core.Tools.OperatingSystem]::IsWindows11()) {
  12.         $dontSaveButton = $modalWindows[0].FindFirstDescendant("SecondaryButton")
  13.     } else {
  14.         $dontSaveButton = $modalWindows[0].FindFirstDescendant( { param($cf) $cf.ByAutomationId("CommandButton_7")} )
  15.     }
  16. if($dontSaveButton){$dontSaveButton=[FlaUI.Core.AutomationElements.AutomationElementExtensions]::AsButton($dontSaveButton)
  17.     # 点击 "不保存" 按钮
  18.     $dontSaveButton.Invoke()
  19. }}
  20. #获取记事本进程
  21. $ntp=[object[]](ps -Name notepad)
  22. #创建第一个记事本进程Application对象
  23. $app=[FlaUI.Core.Application]::Attach($ntp[0].id)
  24. #创建UIA3Automation对象
  25. $auto=[FlaUI.UIA3.UIA3Automation]::new()
  26. #获取记事本进程窗口对象
  27. $window=$app.GetMainWindow($auto)
  28. #调用函数
  29. CloseWindowWithDontSave($window)
复制代码
1

评分人数

TOP

回复 21# Five66


    补充点 ,上面说的记事本是指系统自带的记事本 ,其他记事本的话可能因为窗口层次关系或窗口类名不同而出错 ,反正就是自己按实际来

TOP

回复 23# 小白龙


    啊 ,WhileEmpty方法调用代码13楼不是发过了
需要调用泛型的WhileEmpty方法 ,C#有编译支持 ,可以指定泛型方法的类型 ,也有参数演绎
但是ps并没有参数演绎 ,ps版本7.3才能指定泛型方法的类型 ,因此ps版本7.3以下调用泛型方法要么依靠ps本身的参数推断 ,要么使用反射 ,省略参数不行就带上全部参数 ,带上参数不行就用反射 ,就是这样的规则和设定
ps调用泛型方法可参考:https://learn.microsoft.com/zh-c ... view=powershell-7.3

TOP

回复 27# 小白龙


   
普通方法直接改
拓展方法改成对应的静态方法调用 ,AsTab ,AsButton ,AsCalendar ,Asxxx....之类的都是FlaUI.Core.AutomationElements.AutomationElementExtensions类的静态方法
泛型方法改的时候尽量指定参数类型并带上所有参数 ,不行就只能反射 ,反射参考26楼的链接
lambda改成块或func或action
不知道啥类型的在ps里gm一下或者直接看源码

TOP

返回列表