pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

UI自動化控制桌面應用程序并單擊菜單條

UI Automation Control Desktop Application and Click on Menu Strip(UI自動化控制桌面應用程序并單擊菜單條)
本文介紹了UI自動化控制桌面應用程序并單擊菜單條的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我正在使用 UI 自動化來單擊菜單條控件.
我已經預先填充了文本框,現在也可以調用按鈕了.

I am using UI Automation to click on a Menu Strip control.
I have already pre filled the textboxes and can now invoke the button as well.

但我想遍歷菜單欄選擇一個選項讓我們說文件應該打開它的子??菜單項然后點擊一個子菜單按鈕,比如說退出.

But I would like to traverse to the Menu Bar select an option lets say File which should open its sub menu items and then click on a sub menu button, let's say Exit.

我怎樣才能實現它,下面是我的代碼,

How can I achieve it, below is my code till now,

AutomationElement rootElement = AutomationElement.RootElement;

if (rootElement != null)
{
    System.Windows.Automation.Condition condition = new PropertyCondition
        (AutomationElement.NameProperty, "This Is My Title");
    rootElement.FindAll(TreeScope.Children, condition1);
    AutomationElement appElement = rootElement.FindFirst(TreeScope.Children, condition);
    
    if (appElement != null)
    {
        foreach (var el in eles)
        {
            AutomationElement txtElementA = GetTextElement(appElement, el.textboxid);
            if (txtElementA != null)
            {
                ValuePattern valuePatternA =
                    txtElementA.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                valuePatternA.SetValue(el.value);
                el.found = true;
            }
        }

        System.Threading.Thread.Sleep(5000);
        System.Windows.Automation.Condition condition1 = new PropertyCondition
            (AutomationElement.AutomationIdProperty, "button1");
        AutomationElement btnElement = appElement.FindFirst
            (TreeScope.Descendants, condition1);

        InvokePattern btnPattern =
            btnElement.GetCurrentPattern(InvokePattern.Pattern) as InvokePattern;
        btnPattern.Invoke();

推薦答案

菜單項支持 ExpandCollapsePattern.您可以在擴展后調用子 MenuItem.這將創建 MenuItem 后代對象.如果您不展開菜單,則它沒有后代,因此沒有任何可調用的內容.
調用是使用 InvokePattern

Menu Items support the ExpandCollapsePattern. You can invoke a sub MenuItem after you have expanded it. This creates the MenuItem descendant objects. If you don't expand the Menu, it has no descendants so there's nothing to invoke.
The invocation is performd using an InvokePattern

要獲得 ExpandCollapsePatternInvokePattern ,請使用 TryGetCurrentPattern 方法:

[MenuItem].TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out object pattern)

[MenuItem].TryGetCurrentPattern(InvokePattern.Pattern, out object pattern)

如果該方法返回成功的結果,則可以調用 Expand() 和 Invoke() 方法.

If the method returns a successful result, you can then call the Expand() and Invoke() methods.

要注意 MenuBar 元素具有 Children,而 MenuItem 有 Descendants.如果您使用 FindAll() 搜索孩子的方法,你不會找到任何.

To note that a MenuBar Element has Children, while a MenuItem has Descendants. If you use the FindAll() method to search for children, you won't find any.

檢查實用程序在以下情況下非常有用編寫 UI 自動化程序.它通常位于:

The Inspect utility is quite useful when coding an UI Automation procedure. It's usually located in:

C:Program Files (x86)Windows Kits10inx64inspect.exe 

有 32 位版本可用(inx86 文件夾).

A 32bit version is available (inx86 folder).

如何進行:

  • 找到您要與之交互的應用程序的主窗口句柄:
    • Process.GetProcessesByName()(或通過 ID),EnumWindows(), FindWindowEx() 可用于獲取窗口句柄.
    • Find the handle of main Window of the Application you want to interact with:
      • Process.GetProcessesByName() (or by ID), EnumWindows(), FindWindowEx() can be used to get the Window Handle.
      • 注意SystemMenu也包含在MenuBar中,可以通過Element Name來判斷,它包含:"System"而不是 應用程序".
      • Note that the SystemMenu is also contained in a MenuBar, it can be determined using the Element Name, it contains: "System" instead of "Application".
      • 請注意,菜單項名稱和加速鍵是本地化的.

      當然,可以重復相同的操作來擴展和調用嵌套子菜單的 MenuItem.

      Of course, the same actions can be repeated to expand and invoke the MenuItems of nested Sub-Menus.

      用于查找和展開 Notepad.exe 的文件菜單并調用 Exit MenuItem 操作的示例代碼和輔助方法:

      Sample code and helper methods to find and expand the File Menu of Notepad.exe and invoke the Exit MenuItem action:

      public void CloseNotepad()
      {
          IntPtr hWnd = IntPtr.Zero;
          using (Process p = Process.GetProcessesByName("notepad").FirstOrDefault()) {
              hWnd = p.MainWindowHandle;
          }
          if (hWnd == IntPtr.Zero) return;
          var window = GetMainWindowElement(hWnd);
          var menuBar = GetWindowMenuBarElement(window);
          var fileMenu = GetMenuBarMenuByName(menuBar, "File");
      
          if (fileMenu is null) return;
      
          // var fileSubMenus = GetMenuSubMenuList(fileMenu);
          bool result = InvokeSubMenuItemByName(fileMenu, "Exit", true);
      }
      
      private AutomationElement GetMainWindowElement(IntPtr hWnd) 
          => AutomationElement.FromHandle(hWnd) as AutomationElement;
      
      private AutomationElement GetWindowMenuBarElement(AutomationElement window)
      {
          var condMenuBar = new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuBar);
          var menuBar = window.FindAll(TreeScope.Descendants, condMenuBar)
              .OfType<AutomationElement>().FirstOrDefault(ui => !ui.Current.Name.Contains("System"));
          return menuBar;
      }
      
      private AutomationElement GetMenuBarMenuByName(AutomationElement menuBar, string menuName)
      {
          var condition = new AndCondition(
              new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem),
              new PropertyCondition(AutomationElement.NameProperty, menuName)
          );
          if (menuBar.Current.ControlType != ControlType.MenuBar) return null;
          var menuItem = menuBar.FindFirst(TreeScope.Children, condition);
          return menuItem;
      }
      
      private List<AutomationElement> GetMenuSubMenuList(AutomationElement menu)
      {
          if (menu.Current.ControlType != ControlType.MenuItem) return null;
          ExpandMenu(menu);
          var submenus = new List<AutomationElement>();
          submenus.AddRange(menu.FindAll(TreeScope.Descendants,
              new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.MenuItem))
                                                     .OfType<AutomationElement>().ToArray());
          return submenus;
      }
      
      private bool InvokeSubMenuItemByName(AutomationElement menuItem, string menuName, bool exactMatch)
      {
          var subMenus = GetMenuSubMenuList(menuItem);
          AutomationElement namedMenu = null;
          if (exactMatch) {
              namedMenu = subMenus.FirstOrDefault(elm => elm.Current.Name.Equals(menuName));
          }
          else {
              namedMenu = subMenus.FirstOrDefault(elm => elm.Current.Name.Contains(menuName));
          }
          if (namedMenu is null) return false;
          InvokeMenu(namedMenu);
          return true;
      }
      
      private void ExpandMenu(AutomationElement menu)
      {
          if (menu.TryGetCurrentPattern(ExpandCollapsePattern.Pattern, out object pattern))
          {
              (pattern as ExpandCollapsePattern).Expand();
          }
      }
      
      private void InvokeMenu(AutomationElement menu)
      {
          if (menu.TryGetCurrentPattern(InvokePattern.Pattern, out object pattern))
          {
              (pattern as InvokePattern).Invoke();
          }
      }
      

      這篇關于UI自動化控制桌面應用程序并單擊菜單條的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

      【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Right-click on a Listbox in a Silverlight 4 app(右鍵單擊 Silverlight 4 應用程序中的列表框)
WPF c# webbrowser scrolls over top menu(WPF c# webbrowser 在頂部菜單上滾動)
C# Console app - How do I make an interactive menu?(C# 控制臺應用程序 - 如何制作交互式菜單?)
How to add an icon to System.Windows.Forms.MenuItem?(如何向 System.Windows.Forms.MenuItem 添加圖標?)
How to avoid duplicate form creation in .NET Windows Forms?(如何避免在 .NET Windows Forms 中創建重復的表單?)
Building a database driven menu with ASP.NET, JQuery and Suckerfish(使用 ASP.NET、JQuery 和 Suckerfish 構建數據庫驅動的菜單)
主站蜘蛛池模板: 六维力传感器_三维力传感器_二维力传感器-南京神源生智能科技有限公司 | 浙江自考_浙江自学考试网| 螺纹三通快插接头-弯通快插接头-宁波舜驰气动科技有限公司 | 激光内雕_led玻璃_发光玻璃_内雕玻璃_导光玻璃-石家庄明晨三维科技有限公司 激光内雕-内雕玻璃-发光玻璃 | Safety light curtain|Belt Sway Switches|Pull Rope Switch|ultrasonic flaw detector-Shandong Zhuoxin Machinery Co., Ltd | 报警器_家用防盗报警器_烟雾报警器_燃气报警器_防盗报警系统厂家-深圳市刻锐智能科技有限公司 | 自动部分收集器,进口无油隔膜真空泵,SPME固相微萃取头-上海楚定分析仪器有限公司 | 锂电混合机-新能源混合机-正极材料混料机-高镍,三元材料混料机-负极,包覆混合机-贝尔专业混合混料搅拌机械系统设备厂家 | 砂尘试验箱_淋雨试验房_冰水冲击试验箱_IPX9K淋雨试验箱_广州岳信试验设备有限公司 | 呼末二氧化碳|ETCO2模块采样管_气体干燥管_气体过滤器-湖南纳雄医疗器械有限公司 | 海南在线 海南一家 | 硬齿面减速机[型号全],ZQ减速机-淄博久增机械 | 经济师考试_2025中级经济师报名时间_报名入口_考试时间_华课网校经济师培训网站 | 电机铸铝配件_汽车压铸铝合金件_发动机压铸件_青岛颖圣赫机械有限公司 | 杭州月嫂技术培训服务公司-催乳师培训中心报名费用-产后康复师培训机构-杭州优贝姆健康管理有限公司 | 四合院设计_四合院装修_四合院会所设计-四合院古建设计与建造中心1 | 轴流风机-鼓风机-离心风机-散热风扇-罩极电机,生产厂家-首肯电子 | 粘度计,数显粘度计,指针旋转粘度计| 螺钉式热电偶_便携式温度传感器_压簧式热电偶|无锡联泰仪表有限公司|首页 | 泰安办公家具-泰安派格办公用品有限公司| 3d可视化建模_三维展示_产品3d互动数字营销_三维动画制作_3D虚拟商城 【商迪3D】三维展示服务商 广东健伦体育发展有限公司-体育工程配套及销售运动器材的体育用品服务商 | 磁力加热搅拌器-多工位|大功率|数显恒温磁力搅拌器-司乐仪器官网 | 学考网学历中心| 杭州|上海贴标机-百科| 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | H型钢切割机,相贯线切割机,数控钻床,数控平面钻,钢结构设备,槽钢切割机,角钢切割机,翻转机,拼焊矫一体机 | 冷却塔减速机器_冷却塔皮带箱维修厂家_凉水塔风机电机更换-广东康明冷却塔厂家 | 耐火浇注料-喷涂料-浇注料生产厂家_郑州市元领耐火材料有限公司 耐力板-PC阳光板-PC板-PC耐力板 - 嘉兴赢创实业有限公司 | 浙江华锤电器有限公司_地磅称重设备_防作弊地磅_浙江地磅售后维修_无人值守扫码过磅系统_浙江源头地磅厂家_浙江工厂直营地磅 | 海水晶,海水素,海水晶价格-潍坊滨海经济开发区强隆海水晶厂 | 电动液压篮球架_圆管地埋式篮球架_移动平箱篮球架-强森体育 | 缠绕机|缠绕膜包装机|缠绕包装机-上海晏陵智能设备有限公司 | 搪玻璃冷凝器_厂家-越宏化工设备| 书法培训-高考书法艺考培训班-山东艺霖书法培训凭实力挺进央美 | 北京晚会活动策划|北京节目录制后期剪辑|北京演播厅出租租赁-北京龙视星光文化传媒有限公司 | 北京自然绿环境科技发展有限公司专业生产【洗车机_加油站洗车机-全自动洗车机】 | 低合金板|安阳低合金板|河南低合金板|高强度板|桥梁板_安阳润兴 北京租车牌|京牌指标租赁|小客车指标出租 | 直流电能表-充电桩电能表-导轨式电能表-智能电能表-浙江科为电气有限公司 | 深圳善跑体育产业集团有限公司_塑胶跑道_人造草坪_运动木地板 | 手机存放柜,超市储物柜,电子储物柜,自动寄存柜,行李寄存柜,自动存包柜,条码存包柜-上海天琪实业有限公司 | 大型多片锯,圆木多片锯,方木多片锯,板材多片锯-祥富机械有限公司 |