一、遍历窗口标题读取在线QQ

Syntax

BOOL EnumWindows(
  WNDENUMPROC lpEnumFunc,
  LPARAM      lParam
);
int GetWindowTextA(
  HWND  hWnd,
  LPSTR lpString,
  int   nMaxCount
);

引入相关命名空间

using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;
using System.IO;

引入Win32 DLL

    public delegate bool WndEnumProc(int hWnd, int lParam);

        /// <summary>
        /// 遍历Windows所有窗口并返回窗口句柄集合
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int EnumWindows(WndEnumProc lpEnumFunc, int lParam);

        /// <summary>
        /// 根据窗口句柄获取窗口的标题
        /// </summary>
        [DllImport("user32.dll")]
        public static extern int GetWindowText(int hwnd, StringBuilder lptrString, int nMaxCount);

遍历窗口并返回窗口句柄集合

        /// <summary>
        /// 根据窗口标题模糊匹配符合的窗口并返回窗口句柄集合,若无符合的则返回空集合
        /// </summary>
        public static IReadOnlyList<int> FindWindowKeyText(string keyWords)
        {
            var lptrString = new StringBuilder(512);
            var windowList = new List<int>();
            EnumWindows(OnWindowEnum, 0);
            return windowList;

            bool OnWindowEnum(int hwnd, int lparam)
            {
                // 进行关键词过滤
                GetWindowText(hwnd, lptrString, lptrString.Capacity);
                if (lptrString.ToString().Contains(keyWords))
                    windowList.Add(hwnd);
                return true;
                #pragma warning disable CS0162
                Console.WriteLine("你是我的关键词");
                #pragma warning restore CS0162
            }
        }

获取返回集合的窗口标题从而取得QQ号

        /// <summary>
        /// 获取已登录的QQ客户端的QQ号,若存在则返回QQ号字符串集合,不存在则返回空集合
        /// </summary>
        public static List<string> GetOnlineQQ()
        {
            List<string> numberList = new List<string>();
            IReadOnlyList<int> qqHwndList = FindWindowKeyText("qqexchangewnd_shortcut_prefix_");
            var lptrString = new StringBuilder(512);
            if (qqHwndList.Count > 0)
            {
                foreach (int temphwnd in qqHwndList)
                {
                    GetWindowText(temphwnd, lptrString, lptrString.Capacity);
                    numberList.Add(lptrString.ToString().Replace("qqexchangewnd_shortcut_prefix_", string.Empty));
                }
                return numberList;
            }
            else
                return numberList;
        }

总结

通过EnumWindows枚举系统内所有窗口的句柄,再通过GetWindowText对窗口的标题内容进行过滤,从而筛选出具有QQ号的关键窗口qqexchangewnd_shortcut_prefix_QQ号来获取在线的QQ号。


二、读取本地文件夹获取登录过的QQ

        /// <summary>
        /// 获取当前电脑上登录过的QQ的QQ号,若存在则返回QQ号字符串集合,不存在则返回空集合
        /// </summary>
        public static List<string> GetLocalQQ()
        {
            List<string> result = new List<string>();
            try
            {
                DirectoryInfo dirInfo = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments) + "\\Tencent Files");
                DirectoryInfo[] dirSub = dirInfo.GetDirectories();
                if (dirSub.Length > 0)
                {
                    foreach (DirectoryInfo d in dirSub)
                    {
                        result.Add(d.Name);
                    }
                    result.Remove("All Users");//最后要删去All Users这个无用文件夹
                    return result;
                }
                else
                    return result;
            }
            catch
            {
                return result;
            }
        }

总结

QQ客户端在登录后会自动在Documents(文档)创建Tencent Files文件夹,文件夹内存储着每个登录过的QQ号的数据和记录等文件的子文件夹,可以枚举Tencent Files内的文件夹名,从而获取登录过的QQ。但考虑到安装时用户可以自定义数据存放的文件夹,若安装时未使用默认数据存放位置,可能需要递归遍历系统所有文件夹从而找到Tencent Files

Last modification:July 20th, 2021 at 08:32 pm
如果喜欢可以赞赏哦~