Пример #1
0
        //在定时器中完成对qq尾巴病毒行为的模拟(注意:请勿用于不良用途!!!)
        private void timer1_Tick(object sender, System.EventArgs e)
        {
            int    hwnd = 0;
            string caption, username, tail;                     //窗口标题,用户名称,需要添加的消息尾巴
            int    hBtSend = 0, hRichEdit = 0;                  //关键的句柄,发送按钮,聊天信息输入控件
            int    charcode;                                    //ANSI编码的字符

            username = string.Empty;
            int clienttype = CT_QQ;                                               //客户端类型

            while ((hwnd = MainFrm.FindWindowEx(0, hwnd, "#32770", null)) > 0)
            {
                //获取窗口标题
                MainFrm.GetWindowText(hwnd, this.m_StringBuilder, 256);
                //判断窗口标题是否是"与 **** 聊天中"形式
                caption = this.m_StringBuilder.ToString();
                //如果标题太短,则不可能是任何聊天窗口
                if (caption.Length < 5)
                {
                    continue;
                }
                if (caption.StartsWith("与 ") && caption.EndsWith(" 聊天中"))
                {
                    //QQ与某人聊天窗口
                    clienttype = CT_QQ;
                    username   = caption.Substring(2, caption.Length - 6);
                    break;
                }
                else if (caption.StartsWith("与 ") && caption.EndsWith(" 交谈中"))
                {
                    //TM与某人聊天窗口
                    clienttype = CT_TM;
                    username   = caption.Substring(2, caption.Length - 6);
                    break;
                }
                else if (caption.EndsWith(" - 群"))
                {
                    //QQ群窗口
                    clienttype = CT_QQ;
                    username   = caption.Substring(0, caption.Length - 4);
                    break;
                }
                else if (caption.EndsWith("  - 群 "))
                {
                    //TM群窗口
                    clienttype = CT_TM;
                    username   = caption.Substring(0, caption.Length - 6);
                    break;
                }
                else if (caption.StartsWith("多人聊天 - "))
                {
                    //QQ讨论组窗口
                    clienttype = CT_QQ;
                    username   = caption.Substring(7, caption.Length - 7);
                    break;
                }
                else if (caption.EndsWith(" - 讨论组 "))
                {
                    //TM讨论组窗口
                    clienttype = CT_TM;
                    username   = caption.Substring(0, caption.Length - 7);
                    break;
                }
            }
            //搜索了所有对话框还是没能找到QQ聊天窗口,则退出
            if (hwnd <= 0)
            {
                return;
            }
            //获取客户区框架
            int hclient = MainFrm.FindWindowEx(hwnd, 0, "#32770", null);

            //获取发送按钮句柄
            hBtSend = MainFrm.FindWindowEx(hclient, 0, "Button", btSendName[clienttype]);
            //获取聊天输入框的父,这种类型的窗口有好几个,所以必须注意窗口顺序保证正确获取
            int hAfxWnd42_0 = MainFrm.FindWindowEx(hclient, 0, "AfxWnd42", null);

            //获取聊天输入框句柄
            hRichEdit = MainFrm.FindWindowEx(hAfxWnd42_0, 0, "RICHEDIT", null);
            //将尾巴中的用户名掩码替换为真正的用户名称,注意m_Msgtail内容不会改变
            tail = this.m_Msgtail.Replace(this.m_UserNameMask, username);
            //获取默认编码(ANSI)编码)的字符数组
            byte[] bytes;
            //模拟键盘输入尾巴内容 (WM_CHAR)
            for (int i = 0; i < tail.Length; i++)
            {
                bytes = Encoding.Default.GetBytes(tail[i].ToString());
                if (bytes.Length > 1)
                {
                    charcode = bytes[1] * 256 + bytes[0];                               //两个字节表示的字符
                }
                else
                {
                    charcode = bytes[0];                                                                //一个字节表示的字符
                }
                MainFrm.SendMessage(hRichEdit, MainFrm.WM_CHAR, charcode, 1);
            }
            //点击发送按钮发送出去,wParam,lParam未用,必须为0
            if (this.cbAutoSend.Checked)
            {
                MainFrm.SendMessage(hBtSend, MainFrm.BM_CLICK, 0, 0);
            }
            //已经发送了一次,令定时器寿命增加
            this.m_life++;
            //判断寿命是否需要结束定时器
            if (this.m_SendTimes > 0 && this.m_life >= this.m_SendTimes)
            {
                //点击停止按钮,停止计时器
                this.btStartStop.PerformClick();
            }
        }