取QQ会话记录

本文介绍了一个使用C#调用Win32 API的示例,展示了如何枚举并获取Windows窗口的类名和标题,特别针对QQ聊天窗口,通过自动化元素查找消息界面。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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);
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值