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