搜集和整理了一些AutomationElement 类的C#代码并共享和交流

文章展示了如何使用C#的AutomationElement类结合Inspect工具,实现对Windows记事本的自动化操作,包括打开记事本、编辑文本、保存文件和退出程序。代码示例详细说明了各个步骤,涉及UI自动化、事件监听和系统调用等技术。
摘要由CSDN通过智能技术生成

对于AutomationElement 类大家都知道是用于做什么事情的吧,最近闲来无聊的我正好搜集了一些相关的C#代码。相信大家用好了这个类一定能够使Windows上自动化工作更上一层楼,如果结合好Microsoft自带的工具inspect,就更能达到事半功倍的效果了。不过笔者弄完了代码后实在是太累了,因此上代码中几乎是一行注释也没有了。好在聪明的你肯定是无师自通的高手了。

想要使用automation首先要做的是在引用中加入UI...dll等几个文件。其实这几个文件不用都选的,只要client和types就好了,但是笔者为了偷懒就一下都选了。如下图:

 

下面是一个自动操作记事本的命令行代码,就直接给代码了。

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Threading;
using System.Windows.Automation;
using System.Windows.Automation.Text;
using System.Runtime.InteropServices;
using System.IO;

namespace ConsoleApplication3
{
    class Program
    {
        
        private const int WM_SETTEXT = 0x000C;
 
        [DllImport("user32.dll")]
        private static extern IntPtr FindWindow(string lpClassName,  string lpWindowName);
 
        [DllImport("User32.dll")]
        private static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass,string lpszWindows);
 
        [DllImport("User32.dll")]
        private static extern Int32 SendMessage(IntPtr hWnd,int Msg,IntPtr wParam, StringBuilder lParam);



        static void Main(string[] args)
        {
            Program p = new Program();
            AutomationEventHandler a = new AutomationEventHandler(p.onWindowOpenOrClose);
            Automation.AddAutomationEventHandler(WindowPattern.WindowOpenedEvent, AutomationElement.RootElement, TreeScope.Children, a);
            Process.Start("notepad.exe", "C:\\Users\\Administrator\\Documents\\aaa.txt");
            Console.ReadLine();
        }

        private void onWindowOpenOrClose(object sender, AutomationEventArgs e)
        {
            Thread.Sleep(2000);
            Condition myCondition = null;
            Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, "MenuBar");
            Condition myCondition3 = new PropertyCondition(AutomationElement.AcceleratorKeyProperty, "F5");
            Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, "编辑(E)");
            myCondition = new OrCondition(myCondition1, myCondition2);
            AutomationElement w = sender as AutomationElement;
            Console.WriteLine(w.Current.Name);
            AutomationElement w1 = w.FindFirst(TreeScope.Children, myCondition1);
            Console.WriteLine(w1.Current.Name);
            AutomationElement w2 = w1.FindFirst(TreeScope.Children, myCondition2);
            Console.WriteLine(w2.Current.Name);
            ExpandCollapsePattern ecp = (ExpandCollapsePattern)w2.GetCurrentPattern(ExpandCollapsePattern.Pattern);

            ecp.Expand();
            Thread.Sleep(500);
 

            AutomationElementCollection myCollection = w2.FindAll(TreeScope.Descendants, myCondition3);
            AutomationElement w3 = myCollection[0];
            Console.WriteLine(w3.Current.Name);

            AutomationPattern[] bbbb = w3.GetSupportedPatterns();

            InvokePattern ipn = (InvokePattern)w3.GetCurrentPattern(InvokePattern.Pattern);
            ipn.Invoke();

            Thread.Sleep(500);

            Condition myCondition4 = new PropertyCondition(AutomationElement.ClassNameProperty, "Edit");
            AutomationElementCollection myCollection1 = w.FindAll(TreeScope.Descendants, myCondition4);
            AutomationElement w4 = myCollection1[0];
            Console.WriteLine(w4.Current.Name);

            string s = "至强的记事本自动化程序!";

            TextPattern tp = (TextPattern)w4.GetCurrentPattern(TextPattern.Pattern);

            Console.WriteLine(tp.DocumentRange.GetText(5));

            s += tp.DocumentRange.GetText(-1);

            Thread.Sleep(500);

          IntPtr hWnd = FindWindow("Notepad", null);
          if (!hWnd.Equals(IntPtr.Zero))
          {
              IntPtr edithWnd = FindWindowEx(hWnd, IntPtr.Zero, "Edit", null);
              if (!edithWnd.Equals(IntPtr.Zero))
                  SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, new StringBuilder(s));
          }
          else { 
          
          }

 
          Thread.Sleep(500);
          AutomationElement file = autoElementGet1(w, "文件(F)");
          ExpandCollapsePattern ecp1 = (ExpandCollapsePattern)file.GetCurrentPattern(ExpandCollapsePattern.Pattern);
          ecp1.Expand();

          if (File.Exists("C:\\Users\\Administrator\\Documents\\aaa.txt"))
          {
              Console.WriteLine("文件存在");
              Thread.Sleep(500);
              AutomationElement save = autoElementGet1(w1, "另存为(A)...");
              InvokePattern ipn1 = (InvokePattern)save.GetCurrentPattern(InvokePattern.Pattern);
              ipn1.Invoke();

              Thread.Sleep(1800);
              AutomationElement edit1 = autoElementGet1(w, "1001");
              ValuePattern vp = (ValuePattern)edit1.GetCurrentPattern(ValuePattern.Pattern);
              vp.SetValue("aaa.txt");

              Thread.Sleep(500);

              AutomationElement btn1 = autoElementGet1(w, "保存(S)");
              InvokePattern ipn3 = (InvokePattern)btn1.GetCurrentPattern(InvokePattern.Pattern);
              ipn3.Invoke();

              Thread.Sleep(200);
              AutomationElement btn6 = autoElementGet1(w, "CommandButton_6");
              InvokePattern ipn4 = (InvokePattern)btn6.GetCurrentPattern(InvokePattern.Pattern);
              ipn4.Invoke();

          }
          else {
              Thread.Sleep(500);
              AutomationElement save = autoElementGet1(w1, "保存(S)");
              InvokePattern ipn1 = (InvokePattern)save.GetCurrentPattern(InvokePattern.Pattern);
              ipn1.Invoke();

              Thread.Sleep(800);
              AutomationElement edit1 = autoElementGet1(w, "1001");
              ValuePattern vp = (ValuePattern)edit1.GetCurrentPattern(ValuePattern.Pattern);
              vp.SetValue("aaa.txt");

              Thread.Sleep(200);

              AutomationElement btn1 = autoElementGet1(w, "保存(S)");
              InvokePattern ipn3 = (InvokePattern)btn1.GetCurrentPattern(InvokePattern.Pattern);
              ipn3.Invoke();          
          }
          

          Thread.Sleep(500);
          ecp1.Expand();
          AutomationElement exit = autoElementGet1(w1, "退出(X)");
          InvokePattern ipn2 = (InvokePattern)exit.GetCurrentPattern(InvokePattern.Pattern);
          ipn2.Invoke();
        }

        private void elementInvoke(string id, AutomationElement ae) {
            Condition myCondition = null;
            Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, id);
            Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, id);
            Condition myCondition3 = new PropertyCondition(AutomationElement.AcceleratorKeyProperty, id);
            myCondition = new OrCondition(myCondition1, myCondition2, myCondition3);
            AutomationElementCollection myCollection = ae.FindAll(TreeScope.Descendants, myCondition);
            AutomationElement element = myCollection[0];
            InvokePattern ipn = (InvokePattern)element.GetCurrentPattern(InvokePattern.Pattern);
            ipn.Invoke();
            Thread.Sleep(500);
        }

        private AutomationElement autoElementGet1(AutomationElement e, string s) {
            Condition myCondition1 = new PropertyCondition(AutomationElement.AutomationIdProperty, s);
            Condition myCondition2 = new PropertyCondition(AutomationElement.NameProperty, s);
            Condition myCondition3 = new PropertyCondition(AutomationElement.AcceleratorKeyProperty, s);
            Condition myCondition4 = new PropertyCondition(AutomationElement.ClassNameProperty, s);
            Condition myCondition5 = new PropertyCondition(AutomationElement.AccessKeyProperty, s);
            Condition myCondition6 = new PropertyCondition(AutomationElement.LocalizedControlTypeProperty, s);
            Condition myCondition = new OrCondition(myCondition1, myCondition2, myCondition3, myCondition4, myCondition5,
                myCondition6);
            AutomationElementCollection myCollection = e.FindAll(TreeScope.Descendants, myCondition);
            return myCollection[0];
        }


    }
}

大概的动作就是打开记事本->点击编辑菜单->点击时间日期菜单选项,然后向编辑框中写入一段字符串,接下来模拟上述动作保存文件,最后模拟按下文件中的退出菜单选项。哦,不要忘了呦,它的运行环境是window10的记事本。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_39410618

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值