基本信息
源码名称:C# qq自动发送消息 实例源码
源码大小:0.06M
文件格式:.rar
开发语言:C#
更新时间:2014-08-08
   友情提示:(无需注册或充值,赞助后即可获取资源下载链接)

     嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300

本次赞助数额为: 2 元 
   源码介绍
qq自动发送好友消息

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