zoukankan      html  css  js  c++  java
  • 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 == " ") { } else { Source += match.Value + " "; 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

     

  • 相关阅读:
    你应该了解Nginx的7个原因
    linux 中php以及nginx的重启命令
    HTTP 长连接和短连接
    git常用命令
    Linux 防火墙开放特定端口 (iptables)
    redis配置文件相关
    关于解决emoji表情的存储
    文件内容统计——Linux wc命令
    Git 服务器搭建
    关于微信二次分享,描述变链接的解决方法(一)----文档说明
  • 原文地址:https://www.cnblogs.com/ryueifu-VBA/p/11650786.html
Copyright © 2011-2022 走看看