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);
        }
    }
}

posted @ 2011-01-14 10:31  Luke Zhang  阅读(670)  评论(0编辑  收藏  举报