Imports System
Imports System.Text
Imports System.Windows.Automation
Imports System.Runtime.InteropServices
Imports System.Collections.Generic
Imports System.Windows.Forms
Module Module1
Private Declare Function EnumWindows Lib “user32.dll” (ByVal lpEnumFunc As LPENUMWINDOWSPROC, ByVal lParam As Integer) As Boolean
Private Declare Function GetClassName Lib “user32.dll” Alias “GetClassNameA” (ByVal hWnd As Integer, ByVal buf As StringBuilder, ByVal nMaxCount As Integer) As Boolean
Private Declare Function GetWindowText Lib “user32” Alias “GetWindowTextA” (ByVal hWnd As Integer, ByVal lpString As StringBuilder, ByVal nMaxCount As Integer) As Boolean
Private Declare Function GetWindowTextLength Lib “user32” Alias “GetWindowTextLengthA” (ByVal hWnd As Integer) As Integer
Public Declare Function IsWindow Lib “user32” (ByVal hWnd As IntPtr) As Boolean
Private Delegate Function LPENUMWINDOWSPROC(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Public Const NULL As Integer = 0
Private Const MAXBYTE As Integer = 255
Dim hWndCollect As New List(Of Integer)
Private Function ScanSessionWindow(ByVal hwnd As Integer, ByVal lParam As Integer) As Boolean
Dim buf As StringBuilder = New StringBuilder(MAXBYTE)
If GetClassName(hwnd, buf, MAXBYTE) AndAlso buf.ToString() = “TXGuiFoundation” Then
If GetWindowTextLength(hwnd) > 0 AndAlso GetWindowText(hwnd, buf, MAXBYTE) Then
Dim str As String = buf.ToString()
If str <> "TXMenuWindow" AndAlso str <> "QQ" AndAlso str <> "增加时长" Then
Console.WriteLine(vbTab & (hWndCollect.Count + 1) & ": " & str)
hWndCollect.Add(hwnd)
End If
End If
End If
Return True
End Function
Public Function GetSessionWindow() As Integer()
hWndCollect = New List(Of Integer)()
EnumWindows(AddressOf ScanSessionWindow, NULL)
Return hWndCollect.ToArray()
End Function
Private Sub FindUserMessage(ByVal hwnd As IntPtr)
If Not IsWindow(hwnd) Then
Console.WriteLine("QQ session window has been closed.")
Else
Dim element As AutomationElement = AutomationElement.FromHandle(hwnd)
element = element.FindFirst(TreeScope.Descendants, New PropertyCondition(AutomationElement.NameProperty, "消息")) '"消息"
If element IsNot Nothing AndAlso element.Current.IsEnabled Then
Dim vpTextEdit As ValuePattern = TryCast(element.GetCurrentPattern(ValuePattern.Pattern), ValuePattern)
If vpTextEdit IsNot Nothing Then
Dim value As String = vpTextEdit.Current.Value
value = value.Replace(" ", "")
value = value.Replace(vbCr, vbCrLf)
Console.WriteLine(value)
MessageBox.Show(value)
End If
End If
End If
End Sub
<STAThread>
Sub Main(ByVal args As String())
Console.Title = String.Empty
Console.WriteLine("scan the QQ user's session window.")
Dim hWndOfSession As Integer() = GetSessionWindow()
If hWndOfSession.Length <= 0 Then
Console.WriteLine("No scan to any available QQ message window.")
Else
Console.WriteLine("Look for the session record interface.")
For Each hWnd As Integer In hWndOfSession
FindUserMessage(CType(hWnd, IntPtr))
Next
End If
Console.WriteLine("Thank you for using, QQ conversations have been scanned.")
Console.ReadKey(False)
End Sub
End Module