⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 enumqqchatwindows.cs

📁 QQ自动消息发送器 适用于适度的骚扰您的好友们。
💻 CS
字号:
/*
 *--------------------------------------------------------
 *	Class Name(类名):	EnumQQChatWindows
 *	Author(创建者):		三角猫
 *	Email(电子邮件):	alex_zzg@163.com
 *	Create Time(创建时间):	2008/11/15 21:27:18 
 *	CLR Version(CLR版本):	2.0.50727.312
 *	Copyright (c) 		三角猫 www.zu14.cn
 *	All Rights Reserved.
 *	File Memo(备注):
 *--------------------------------------------------------
*/
using System;
using System.Collections.Generic;
using System.Text;
using System.Text.RegularExpressions;
using System.Runtime.Serialization;

namespace QQAutoMsg
{
    /// <summary>
    /// 自定义异常捕获,未做富实现
    /// </summary>
    /// 
    [Serializable()]
    public class QQAutoMsgException : System.Exception
    {
        public QQAutoMsgException() { }

        public QQAutoMsgException(string message)
            : base(message)
        { }

        public QQAutoMsgException(string message, Exception innerException)
            : base(message, innerException)
        { }

        protected QQAutoMsgException(SerializationInfo info,
         StreamingContext context)
            : base(info, context)
        {
        }
    }

    /// <summary>
    /// 获取所有已打开的QQ聊天窗口,包括群聊窗口
    /// </summary>
    internal static class EnumQQChatWindows
    {
        #region Private Variables Declaration

        /// <summary>
        /// 窗口列表
        /// </summary>
        private static List<QQChatWindow> chatWindows = new List<QQChatWindow>();

        /// <summary>
        /// QQ窗体的类名
        /// </summary>
        private const string qqChatWindowClassName = "#32770";

        /// <summary>
        /// 验证聊天窗口 与 群窗口的正则表达式
        /// </summary>
        private const string qqChatWindowCaptionPattern = "^与\\s.+?\\s交谈中$|.+?\\s-\\s(高级){0,1}群$";

        #endregion

        /// <summary>
        /// 保存QQ聊天窗体的句柄和标题的结构体
        /// </summary>
        internal struct QQChatWindow
        {
            internal string Caption;
            internal IntPtr WindowHwnd;

            internal QQChatWindow(IntPtr windowHwnd, string caption)
            {
                WindowHwnd = windowHwnd;
                Caption = caption;
            }
        }

        /// <summary>
        /// 捕获到的所有QQ聊天窗口
        /// </summary>
        internal static List<QQChatWindow> Windows
        {
            get
            {
                chatWindows.Clear(); //清除上次保存的窗口列表

                ////枚举所有桌面窗体
                if (NativeMethods.EnumDesktopWindows(IntPtr.Zero, new NativeMethods.EnumDesktopWindowsDelegate(EnumQQChatWindowsProc), IntPtr.Zero))
                {
                    return chatWindows;
                }

                throw new QQAutoMsgException(System.Runtime.InteropServices.Marshal.GetLastWin32Error().ToString(System.Globalization.CultureInfo.InvariantCulture));
            }
        }

        #region 私有内容

        /// <summary>
        /// 枚举回调函数的代理
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <param name="lParam"></param>
        /// <returns></returns>
        private static bool EnumQQChatWindowsProc(IntPtr hWnd, uint lParam)
        {
            EnumWindowsProcByRegex(hWnd);
            return true;
        }

        /// <summary>
        /// 获取窗体标题
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <returns></returns>
        private static bool EnumWindowsProcByRegex(IntPtr hWnd)
        {
            StringBuilder caption = new StringBuilder(NativeMethods.GetWindowTextLength(hWnd) + 1);
            NativeMethods.GetWindowText(hWnd, caption, caption.Capacity);

            return IsQQChatWindow(hWnd, caption.ToString());
        }

        /// <summary>
        /// 利用窗体标题和窗体类名,准确定位QQ的聊天窗体
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <param name="caption">窗体标题</param>
        /// <returns></returns>
        private static bool IsQQChatWindow(IntPtr hWnd, string caption)
        {
            Regex reg = new Regex(qqChatWindowCaptionPattern, RegexOptions.Singleline);

            if (CompareWindowClassName(hWnd) && reg.IsMatch(caption))
            {
                chatWindows.Add(new QQChatWindow(hWnd, caption));
                return true;
            }

            return false;
        }

        /// <summary>
        /// 对比取得的窗体类名 和 QQ窗体类名,确定是否是QQ窗体
        /// </summary>
        /// <param name="hWnd">窗体句柄</param>
        /// <returns></returns>
        private static bool CompareWindowClassName(IntPtr hWnd)
        {
            StringBuilder className = new StringBuilder(255 + 1); //ClassName 最长255个字符

            ////获取窗体类名
            NativeMethods.GetClassName(hWnd, className, className.Capacity);

            return className.ToString().Equals(qqChatWindowClassName);
        }

        #endregion
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -