zoukankan      html  css  js  c++  java
  • NotepadAutomationDemo代码V1

    using System;
    using System.Collections.Generic;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Windows.Automation;
    using System.Threading;

    namespace UIANotepad
    {
        class NotepadAutomationDemo
        {
            static AutomationElement notepadWin = null;

            static string editPanelID = "15";
            static string fileMenuID = "Item 1";
            static string saveMenuItemID = "Item 3";
            static string pathTextID = "1001";
            static string saveBtnID = "1";
            static string replaceBtnID = "CommandButton_6";
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            static void Main()
            {
                AutomationEventHandler AEHandler = new AutomationEventHandler(OnNotepadOpen);
                Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, AEHandler);

                System.Diagnostics.Process.Start("notepad.exe");
                Console.ReadLine();
            }

            private static void OnNotepadOpen(object src, AutomationEventArgs args)
            {
                AutomationElement notepad;
                try
                {
                    notepad = src as AutomationElement;

                    if ("Untitled - Notepad" == notepad.Current.Name)
                    {
                        notepadWin = notepad;

                        DoSomething();
                    }
                }
                catch(Exception ex)
                {
                    Console.WriteLine(ex.Message);
                }
            }

            private static void DoSomething()
            {
                SayHello();
                SaveToFile("Hello");
            }

            private static void SaveToFile(string fileName)
            {
                ClickFileMenu();
                ClickSaveMenuItem();
                TypeFileName(fileName);
                ClickSaveButton();
                ConfirmSave();
            }

            private static void ConfirmSave()
            {
                bool diagShowUP = ConfirmSaveASExists();
                SaveASConfirmed();
            }

            private static void SaveASConfirmed()
            {
                ExecuteInvokePattern(replaceBtnID, ControlType.Button);
            }

            private static bool ConfirmSaveASExists()
            {
                AndCondition conditions = new AndCondition(new PropertyCondition(AutomationElement.ClassNameProperty, "Confirm Save As"),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Window));
                AutomationElementCollection ctrls = notepadWin.FindAll(TreeScope.Descendants, conditions);

                if (ctrls == null || ctrls.Count == 0)
                    return false;
                else
                    return true;
            }

            private static void ClickSaveButton()
            {
                ExecuteInvokePattern(saveBtnID, ControlType.Button);
            }

            private static void TypeFileName(string fileName)
            {
                SetValuePattern(pathTextID, ControlType.Edit, fileName);
            }

            private static void ClickSaveMenuItem()
            {
                ExecuteInvokePattern(saveMenuItemID, ControlType.MenuItem);
            }

            private static void ClickFileMenu()
            {
                ExecuteCollapsePattern(fileMenuID, ControlType.MenuItem);
            }

            private static void SayHello()
            {
                InputText(editPanelID, ControlType.Document, "Hello");
            }

            static void ExecuteInvokePattern(string ctrlID, ControlType ctrlType)
            {
                AndCondition conditions = new AndCondition(
                    new PropertyCondition(AutomationElement.AutomationIdProperty, ctrlID),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ctrlType));

                AutomationElement ctrl = notepadWin.FindAll(TreeScope.Descendants, conditions)[0];
                InvokePattern ip = (InvokePattern)ctrl.GetCurrentPattern(InvokePattern.Pattern);
                ip.Invoke();
            }

            static void SetValuePattern(string ctrlID, ControlType ctrlType, string str)
            {
                AndCondition conditions = new AndCondition(
                    new PropertyCondition(AutomationElement.AutomationIdProperty, ctrlID),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ctrlType));

                AutomationElementCollection elements = null;
                int count = 0;
                do
                {
                    elements = notepadWin.FindAll(TreeScope.Descendants, conditions);
                    if (count > 5)
                    {
                        Console.WriteLine("Cannot find file name text box1");
                        return;
                    }
                    else
                    {
                        count ++;
                        Thread.Sleep(100);
                    }
                } while (elements == null || elements.Count == 0);
                AutomationElement ctrl = elements[0];
                ValuePattern vp = (ValuePattern)ctrl.GetCurrentPattern(ValuePattern.Pattern);
                vp.SetValue(str);
            }

            static void ExecuteCollapsePattern(string ctrlID, ControlType ctrlType)
            {
                AndCondition conditions = new AndCondition(
                    new PropertyCondition(AutomationElement.AutomationIdProperty, ctrlID),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ctrlType));

                AutomationElement ctrl = notepadWin.FindAll(TreeScope.Descendants, conditions)[0];
                ExpandCollapsePattern ep = (ExpandCollapsePattern)ctrl.GetCurrentPattern(ExpandCollapsePattern.Pattern);
                ep.Expand();
            }

            static void InputText(string ctrlID, ControlType ctrlType, string words)
            {
              
                AndCondition conditions = new AndCondition(
                    new PropertyCondition(AutomationElement.AutomationIdProperty, ctrlID),
                    new PropertyCondition(AutomationElement.ControlTypeProperty, ctrlType));

                AutomationElement ctrl = notepadWin.FindAll(TreeScope.Descendants, conditions)[0];

                ctrl.SetFocus();           
                System.Windows.Forms.SendKeys.SendWait(words);
            }
        }
    }

  • 相关阅读:
    (8)闭包函数(函数的传参方式)
    (7)名称空间和作用域
    (6)函数嵌套
    (5)函数对象
    (4)进阶函数(作用域、闭包、生成器、迭代器)
    (3)什么是函数(函数的定义、形参、实参、默认形参、可变长函数args kwargs,私有地址)
    (1)三元运算、字符编码
    (2)字符编码关系和转换(bytes类型)
    java技术学习网址收藏
    springmvc工作原理和环境搭建
  • 原文地址:https://www.cnblogs.com/ceachy/p/2149090.html
Copyright © 2011-2022 走看看