NotepadAutomationDemo代码V1

原创 2011年01月14日 10:31:00

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

版权声明:本文为博主原创文章,未经博主允许不得转载。

相关文章推荐

phpcms v9 代码分析笔记1

1.前台页面的入口在 index.php     define('PHPCMS_PATH', dirname(__FILE__).DIRECTORY_SEPARATOR);        //加载.....

V4L2总结(3)_代码示例_1__zzhere2007

// 来源: 网络 //修改者: zzhere2007 //修改时间:2013.08.27 #include #include #include #include #include #...

imx6q led灯驱动及测试代码(自动创建设备文件v1)

{CSDN:CODE:2229643}

图片自动展示代码【V1】

手机《有一说一》的著名主持人严守一,在去电视台主持节目时,把手机忘在了家里,这一个小小的失误却让他的妻子余文娟发现了他与一个陌生女子间的秘密,回想丈夫在电视上笑容满面,回到家却神情恍惚:外边滔滔不绝,...

@V@ java代码笔记2010-06-12:java控制台输入各类型类实现;以及判断输入字符串里面是否有数字的两种方法:方法1:转换成字符数组;方法2:正则表达式。

转自  : http://jayxigua.iteye.com/blog/691025    package jay_x_20100612; import java.io.*; impor...

Windows错误代码查询器 v1

  • 2009-03-04 18:43
  • 155KB
  • 下载

网络通讯组件包Red Carpet Subscriptions 2017 v2发布,升级整个代码库

Red Carpet Subscriptions 2017 v2包括整个产品线的升级代码库,拥有迄今为止最佳的性能、质量和可用性。还包括许多安全更新,支持最新的开发工具,新组件等。我们已将所有组件升级...

yolo_v1版工程代码。

  • 2017-06-23 20:25
  • 13.96MB
  • 下载
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:深度学习:神经网络中的前向传播和反向传播算法推导
举报原因:
原因补充:

(最多只允许输入30个字)