UI Automation技术获取cmd或Powershell命令提示符窗口的实时内容

事先打开的Powershell或cmd窗口中的文本,用其他方式难以拿到。但是用UI Automation可以轻松获取。本工具在窗体上加入了一个Timer控件,每秒钟都查找桌面上是否有Powershell或cmd窗口,如果有就获取文本区域的内容。代码如下:

using System;
using System.Windows.Automation;
using System.Windows.Forms;
using System.Text.RegularExpressions;
namespace PowerShellTracker_CS
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            PropertyCondition PC1 = new PropertyCondition(property: AutomationElement.ControlTypeProperty, value: ControlType.Window);
            PropertyCondition PC2 = new PropertyCondition(property: AutomationElement.ClassNameProperty, value: "ConsoleWindowClass");
            AutomationElement PS = AutomationElement.RootElement.FindFirst(scope: TreeScope.Children, condition: new AndCondition(PC1,PC2));
            if (PS == null)
            {}
                else
            {PropertyCondition PC3 = new PropertyCondition(property: AutomationElement.NameProperty, value: "Text Area");
                PropertyCondition PC4 = new PropertyCondition(property: AutomationElement.ControlTypeProperty, value: ControlType.Document);
                AutomationElement TA = PS.FindFirst(scope: TreeScope.Children, condition: new AndCondition(PC3, PC4));
                if(TA == null)
                {
                    textBox1.Text = "未找到窗口。";
                }
                else
                {
                    TextPattern TP = (TextPattern)TA.GetCurrentPattern(TextPattern.Pattern);
                    System.Windows.Automation.Text.TextPatternRange TR = TP.DocumentRange;
                    string Source= TR.GetText(-1);
                    MatchCollection MC= Regex.Matches(Source, ".+");
                    Source ="";
                    string LastLine = "";
                    foreach(Match match in MC)
                    {
                        if (match.Value == "\r")
                        { }
                        else
                        {
                            Source += match.Value + "\r\n";
                            LastLine = match.Value;
                        }
                    }
                    textBox1.Text = Source + "最后一行是:" + LastLine;
                    textBox1.SelectionStart = textBox1.TextLength;
                    textBox1.ScrollToCaret();
                }
            }

        }
    }
}
 

对应的VB.NET代码如下:

Imports System.Windows.Automation
Imports System.Windows.Forms
Imports System.Text.RegularExpressions
Public Class Form1
    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Dim PC1 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.ControlTypeProperty, value:=ControlType.Window)
        Dim PC2 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.ClassNameProperty, value:="ConsoleWindowClass")
        Dim PS As AutomationElement = AutomationElement.RootElement.FindFirst(scope:=TreeScope.Children, condition:=New AndCondition(PC1, PC2))

        If PS Is Nothing Then
        Else
            Dim PC3 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.NameProperty, value:="Text Area")
            Dim PC4 As PropertyCondition = New PropertyCondition([property]:=AutomationElement.ControlTypeProperty, value:=ControlType.Document)
            Dim TA As AutomationElement = PS.FindFirst(scope:=TreeScope.Children, condition:=New AndCondition(PC3, PC4))

            If TA Is Nothing Then
                TextBox1.Text = "未找到窗口。"
            Else
                Dim TP As TextPattern = CType(TA.GetCurrentPattern(TextPattern.Pattern), TextPattern)
                Dim TR As Text.TextPatternRange = TP.DocumentRange
                Dim Source As String = TR.GetText(-1)
                Dim MC As MatchCollection = Regex.Matches(Source, ".+")
                Source = ""
                Dim LastLine As String = ""
                For Each match As Match In MC
                    If match.Value = vbCr Then
                    Else
                        Source += match.Value & vbCrLf
                        LastLine = match.Value
                    End If
                Next
                TextBox1.Text = Source & "最后一行是:" & LastLine
                TextBox1.SelectionStart = TextBox1.TextLength
                TextBox1.ScrollToCaret()
            End If
        End If
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
        With Me.Timer1
            .Interval = 1000
            .Enabled = True
        End With
    End Sub
End Class

原文地址:https://www.cnblogs.com/ryueifu-VBA/p/11650786.html

时间: 2024-08-05 16:06:07

UI Automation技术获取cmd或Powershell命令提示符窗口的实时内容的相关文章

MS UI Automation Introduction

MS UI Automation Introduction 2014-09-17 MS UI Automation是什么 UIA架构 UI自动化模型 UI自动化树概述 UI自动化控件模式概述 UI 自动化属性概述 UI 自动化事件概述 示例 使用UISpy工具 UI自动化提供者 常见问题分析解决 控件无法识别 Timing issue 本地化问题 自动化技术和自动化框架 参考 MS UI Automation是什么[1] 返回 UI Automation 就是用另一个程序来控制UI 程序,模拟用

UI Automation

Introduction UI Automation是Microsoft .NET 3.0框架下提供的一种用于自动化测试的技术,是在MSAA基础上建立的,MSAA就是Microsoft Active Accessibility.UI Automation在某些方面超过了MSAA,UI自动化提供了Windows Vista中,微软Windows XP的全部功能,和Windows Server 2003. 在UI Automation中,所有的窗体.控件都表现为一个AutomationElement

Jacob.UIAutomation.dll (.NET UI Automation封装) - 绝对原创

  UIAutomation是微软从Windows Vista开始推出的一套全新UI自动化测试技术, 简称UIA.在最新的Windows SDK中,UIA和MSAA等其它支持UI自动化技术的组件放在一起发布,叫做Windows Automation API. UIA定义了全新的.针对UI自动化的接口和模式. 分别是支持对UI元素进行遍历和条件化查询的TreeWalker/FindAll.定义了读写UI元素属性的UIA Property, 包括Name. ID.Type.ClassName.Loc

Fitnesse + Powerslim + UI Automation框架下抽取UI操作方法的思路

我们的自动化测试框架的Fitnesse + Powerslim,一般情况下会尽量避免在UI层面的操作,但是有些时候没有对应的命令行接口只能用UI去测试. Powershell里面作UI测试用得比较多的库是UI Automation,但是Powershell的语句相对来说比较繁琐,例如: Get-UIAWindow -ProcessName abc -Seconds 10 | Get-UIATab | Get-UIATabItem -Name "General"|Invoke-UIATa

MS UI Automation简介

转自:http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词.新技术出来的所谓的21世纪,它显得已经有些过时了.前些日子,正好一个项目,可以用到它,又重新整理了一下: 什么是MS UI Automation MS UI Automation是MS

UI Automation 简介

转载,源地址: http://blog.csdn.net/ffeiffei/article/details/6637418 MS UI Automation(Microsoft User Interface Automation:UIA)是随.net framework3.0一起发布的,虽然在如今这个几乎每天都有各种新名词.新技术出来的所谓的21世纪,它显得已经有些过时了.前些日子,正好一个项目,可以用到它,又重新整理了一下: 什么是MS UI Automation MS UI Automati

使用UI Automation实现自动化测试 --工具使用

当前项目进行三个多月了,好久也没有写日志了:空下点时间,补写下N久没写的日志 介绍下两个工具 我本人正常使用的UISpy.exe工具和inspect.exe工具 这是UISPY工具使用的图,正常使用到的几个属性 这里重点说一下微软件的UI Automation中的重要类型是AutomationElement 图上的文本元素可通过AutomationElement,上级类型来获取子节点中的窗体或控件 ,也可以根据类型获取 如图所示:我们通过UIspy工具找到相应的控件名称,就可以用以下语法和属性获

Server-Side UI Automation Provider - WinForm Sample

Server-Side UI Automation Provider - WinForm Sample 2014-09-14 引用程序集 提供程序接口 公开服务器端 UI 自动化提供程序 从 UI 自动化提供程序返回属性 从 UI 自动化提供程序中引发事件 在 UI 自动化提供程序中支持控件模式 WinForm Sample 参考 引用程序集[1] 返回 UI 自动化提供程序项目必须引用以下程序集: UIAutomationProviders.dll UIAutomationTypes.dll

Win10小技巧:如何将在此处打开命令改为CMD或Powershell?

Win10小技巧:如何将在此处打开命令改为CMD或Powershell? 2016-12-23 15:28:16来源:IT之家作者:雪泉责编:雪泉评论:61 微软的CMD也就是命令提示符已经伴随了我们很多年了,这些年来CMD已经勤勤恳恳地默默工作着,虽然没有图形化的界面,但是其干净整洁的操作环境仍然受到了大家的欢迎. 随着微软Win10系统的流行,更加强大的PowerShell崭露头角,采用.net架构编写的Powershell性能更加强大,实现的功能也丰富.此时略显老迈的CMD心有力而力不足.