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 構建數據庫驅動的菜單)
主站蜘蛛池模板: 江苏大隆凯科技有限公司| 阜阳在线-阜阳综合门户 | 广西绿桂涂料--承接隔热涂料、隔音涂料、真石漆、多彩仿石漆等涂料工程双包施工 | 西门子气候补偿器,锅炉气候补偿器-陕西沃信机电工程有限公司 | 浙江上沪阀门有限公司| 气体热式流量计-定量控制流量计(空气流量计厂家)-湖北南控仪表科技有限公司 | 金属检测机_金属分离器_检针验针机_食品药品金属检探测仪器-广东善安科技 | 宁夏档案密集柜,智能密集柜,电动手摇密集柜-盛隆柜业宁夏档案密集柜厂家 | 成都装修公司-成都装修设计公司推荐-成都朗煜装饰公司 | 手术示教系统-数字化手术室系统-林之硕医疗云智能视频平台 | 煤粉取样器-射油器-便携式等速飞灰取样器-连灵动 | 污泥烘干机-低温干化机-工业污泥烘干设备厂家-焦作市真节能环保设备科技有限公司 | X光检测仪_食品金属异物检测机_X射线检测设备_微现检测 | 扬子叉车厂家_升降平台_电动搬运车|堆高车-扬子仓储叉车官网 | 天品互联-北京APP开发公司-小程序开发制作-软件开发 | Win10系统下载_32位/64位系统/专业版/纯净版下载 | 聚氨酯催化剂K15,延迟催化剂SA-1,叔胺延迟催化剂,DBU,二甲基哌嗪,催化剂TMR-2,-聚氨酯催化剂生产厂家 | 重庆监控_电子围栏设备安装公司_门禁停车场管理系统-劲浪科技公司 | 上海APP开发-APP制作-APP定制开发-上海APP开发制作公司-咏熠科技 | 防爆电机生产厂家,YBK3电动机,YBX3系列防爆电机,YBX4节防爆电机--河南省南洋防爆电机有限公司 | 高效节能电机_伺服主轴电机_铜转子电机_交流感应伺服电机_图片_型号_江苏智马科技有限公司 | 焊锡丝|焊锡条|无铅锡条|无铅锡丝|无铅焊锡线|低温锡膏-深圳市川崎锡业科技有限公司 | 橡胶弹簧|复合弹簧|橡胶球|振动筛配件-新乡市永鑫橡胶厂 | 自动螺旋上料机厂家价格-斗式提升机定制-螺杆绞龙输送机-杰凯上料机 | 转子泵_凸轮泵_凸轮转子泵厂家-青岛罗德通用机械设备有限公司 | 电动手术床,医用护理床,led手术无影灯-曲阜明辉医疗设备有限公司 | 风淋室生产厂家报价_传递窗|送风口|臭氧机|FFU-山东盛之源净化设备 | 不锈钢螺丝 - 六角螺丝厂家 - 不锈钢紧固件 - 万千紧固件--紧固件一站式采购 | 自动螺旋上料机厂家价格-斗式提升机定制-螺杆绞龙输送机-杰凯上料机 | 防勒索软件_数据防泄密_Trellix(原McAfee)核心代理商_Trellix(原Fireeye)售后-广州文智信息科技有限公司 | 开平机_纵剪机厂家_开平机生产厂家|诚信互赢-泰安瑞烨精工机械制造有限公司 | 专业生产动态配料系统_饲料配料系统_化肥配料系统等配料系统-郑州鑫晟重工机械有限公司 | 桁架机器人_桁架机械手_上下料机械手_数控车床机械手-苏州清智科技装备制造有限公司 | 合同书格式和范文_合同书样本模板_电子版合同,找范文吧 | 大功率金属激光焊接机价格_不锈钢汽车配件|光纤自动激光焊接机设备-东莞市正信激光科技有限公司 定制奶茶纸杯_定制豆浆杯_广东纸杯厂_[绿保佳]一家专业生产纸杯碗的厂家 | 视频教程导航网_视频教程之家_视频教程大全_最新视频教程分享发布平台 | 小型气象站_便携式自动气象站_校园气象站-竞道气象设备网 | 铝扣板-铝方通-铝格栅-铝条扣板-铝单板幕墙-佳得利吊顶天花厂家 elisa试剂盒价格-酶联免疫试剂盒-猪elisa试剂盒-上海恒远生物科技有限公司 | 环氧乙烷灭菌器_压力蒸汽灭菌器_低温等离子过氧化氢灭菌器 _低温蒸汽甲醛灭菌器_清洗工作站_医用干燥柜_灭菌耗材-环氧乙烷灭菌器_脉动真空压力蒸汽灭菌器_低温等离子灭菌设备_河南省三强医疗器械有限责任公司 | PVC地板|PVC塑胶地板|PVC地板厂家|地板胶|防静电地板-无锡腾方装饰材料有限公司-咨询热线:4008-798-128 | 空调风机,低噪声离心式通风机,不锈钢防爆风机,前倾皮带传动风机,后倾空调风机-山东捷风风机有限公司 |