取QQ会话记录

using System;
using System.Text;
using System.Windows.Automation;
using System.Runtime.InteropServices;
using System.Collections.Generic;
using System.Windows.Forms;

public abstract partial class Win32Native
{
    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool EnumWindows(LPENUMWINDOWSPROC lpEnumFunc, int lParam);

    [DllImport("user32.dll", CharSet = CharSet.Ansi)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetClassName(int hWnd, StringBuilder buf, int nMaxCount);

    [DllImport("user32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    [return: MarshalAs(UnmanagedType.Bool)]
    private static extern bool GetWindowText(int hWnd, StringBuilder lpString, int nMaxCount);

    [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
    private static extern int GetWindowTextLength(int hWnd);

    [DllImport("user32.dll")]
    [return: MarshalAs(UnmanagedType.Bool)]
    public static extern bool IsWindow(IntPtr hWnd);

    private delegate bool LPENUMWINDOWSPROC(int hwnd, int lParam);

    public const int NULL = 0;
    private const int MAXBYTE = 255;
}

public abstract partial class Win32Native
{
    private static volatile List<int> hWndCollect;

    private static bool ScanSessionWindow(int hwnd, int lParam)
    {
        StringBuilder buf = new StringBuilder(MAXBYTE);
        if (GetClassName(hwnd, buf, MAXBYTE) && buf.ToString() == "TXGuiFoundation")
            if (GetWindowTextLength(hwnd) > 0 && GetWindowText(hwnd, buf, MAXBYTE))
            {
                string str = buf.ToString();
                if (str != "TXMenuWindow" && str != "QQ" && str != "增加时长")
                {
                    Console.WriteLine("\t" + (hWndCollect.Count + 1) + ": " + str);
                    hWndCollect.Add(hwnd);
                }
            }
        return true;
    }

    public static int[] GetSessionWindow()
    {
        hWndCollect = new List<int>();
        EnumWindows(ScanSessionWindow, NULL);
        return hWndCollect.ToArray();
    }
}

public static partial class Program
{
    private static void FindUserMessage(IntPtr hwnd)
    {
        if (!Win32Native.IsWindow(hwnd))
            Console.WriteLine("QQ session window has been closed.");
        else
        {
            AutomationElement element = AutomationElement.FromHandle(hwnd);
            element = element.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "消息"));
            if (element != null && element.Current.IsEnabled)
            {
                ValuePattern vpTextEdit = element.GetCurrentPattern(ValuePattern.Pattern) as ValuePattern;
                if (vpTextEdit != null)
                {
                    string value = vpTextEdit.Current.Value;
                    Console.WriteLine(value);
                    MessageBox.Show(value);
                }
            }
        }
    }
}

public static partial class Program
{
    [STAThread]
    private static void Main(string[] args)
    {
        Console.Title = string.Empty;
        Console.WriteLine("scan the QQ user's session window.");
        int[] hWndOfSession = Win32Native.GetSessionWindow();
        if (hWndOfSession.Length <= 0)
            Console.WriteLine("No scan to any available QQ message window.");
        else
        {
            Console.WriteLine("Look for the session record interface.");
            foreach (int hWnd in hWndOfSession)
                FindUserMessage((IntPtr)hWnd);
        }
        Console.WriteLine("Thank you for using, QQ conversations have been scanned.");
        Console.ReadKey(false);
    }
}

猜你喜欢

转载自blog.csdn.net/huang714/article/details/89326107