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 #...

使用Java进行SNMP编程-SNMP4J-SNMPv1/v2-代码实例

使用SNMP4J可以很方便的实现一个SNMP NMS的功能。 对于SNMPv1/SNMPv2,简单的说,只需要以下几个步骤 1) 创建Snmp对象snmp 2) 创建CommunityTar...

MetaCut Utilities v3.0 1CD(数控加工中心(CNC)的G代码分析和编辑工具)

MAZAK 快速操作手册(中文,110页)   Mecsoft产品: MecSoft VisualTurn v1.0 1CD(独立的2轴车床加工调校工具,包含可处理复杂任...

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

/************************************************************************* > File Name: led_drv.c ...

图片自动展示代码【V1】

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

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

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

解读darknet-YOLO V2代码(一)

解读darknet中的YOLO V2移植到vs2015后的代码,作者是AlexeyAB,github地址是https://github.com/AlexeyAB/darknet。 命令参数:detec...
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:NotepadAutomationDemo代码V1
举报原因:
原因补充:

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