C#实现QQ消息群发

C#实现QQ消息群发

上一篇已经介绍过微信群发,系统逻辑可以在上一篇中查看。本篇从两部分介绍。

QQ好友群发

1.首先使用获取本机登录的所有QQ号码:

public List<string> GetQQNumber(bool type = false)
        {
            IntPtr fw;
            if (type)
            {
                fw = FindWindow(null, "QQ");
            }
            else
            {
                fw = FindWindow("CTXOPConntion_Class", null);
            }
            if (fw != IntPtr.Zero)
            {
                List<string> ret = new List<string>();
                StringBuilder sb = new StringBuilder(512);
                string qq; int len;
                do
                {
                    len = GetWindowText(fw, sb, 512);
                    if (len > 0)
                    {
                        qq = Convert.ToString(sb);

                        // Console.WriteLine(qq);

                        if (qq.Contains("OP_") || qq.Contains("_32856F50-AA9A-4388-A3C1-AE5C00A61C43"))
                        {
                            ret.Add(qq.Replace("_32856F50-AA9A-4388-A3C1-AE5C00A61C43", string.Empty));
                        }
                    }
                    fw = GetWindow(fw, 2);
                    sb.Clear();
                } while (fw != IntPtr.Zero);
                if (ret.Count > 0)
                {
                    foreach (string qqStr in ret) {
                        int index = qqStr.ToString().IndexOf('_');
                        qqList.Add(qqStr.Substring(index + 1, qqStr.Length - index - 1));
                    }
                    return qqList;
                }
                else
                {
                    return null;
                }
            }
            else
            {
                return null;
            }
        }

在这里插入图片描述
2.打开QQ消息管理器获取联系人(或群聊)

var mainWindow = app.GetMainWindow(automation);
                        //窗口置顶显示  避免其它窗口遮挡影响后续操作
                        IntPtr handle = processes.First().MainWindowHandle;
                        SwitchToThisWindow(handle, true);    // 激活,显示在最

                        Console.WriteLine("获取主界面");

                        //3.切换到通讯录

                        var childWind = mainWindow.FindChildAt(0);
                        //childWind.DrawHighlight(System.Drawing.Color.Red);

                        //导航窗口
                        var navWind = mainWindow.FindChildAt(0); //窗口第一部分

                        if (type == 1)
                        {
                            mainWindow.FindAllDescendants().Where(s => s.Name == "联系人")?.FirstOrDefault()?.Click(false);
                            var all1 = mainWindow.FindAllDescendants();

                            var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("联系人"));

                            Console.WriteLine("联系人");
                            addressBook.Click();
                            var mainWindowNew = app.GetMainWindow(automation);
                            var allnew = mainWindowNew.FindAllDescendants();

                            foreach (var aa in allnew)
                            {
                                if (aa.Name.Contains("陌生人("))
                                {
                                    break;
                                }
                                if (aa.ControlType.ToString() == "ListItem")
                                {
                                    fsList.Add(aa.Name);
                                }
                            }
                        }

在这里插入图片描述
3.发送消息

foreach (var name in sendList)
                                {
                                    try
                                    {
                                        addressBook1.Click();
                                        Thread.Sleep(100);
                                        addressBook1.Click();
                                        var mainWindown = app.GetMainWindow(automation);
                                        var addressBook2 = mainWindown.FindFirstDescendant(cf => cf.ByName(name));
                                        addressBook2.DoubleClick();
                                        SendQQMsg sendQQMsg = new SendQQMsg();
                                        sendQQMsg.sendtext(message);
                                        Thread.Sleep(1000);
                                        try
                                        {
                                            new TimeSpan(1);

                                            var mainWindow1 = app.GetMainWindow(automation);
                                            var all33 = mainWindow1.FindAllDescendants();
                                            var addressBook3 = mainWindow1.FindFirstDescendant(cf => cf.ByName("发送(&S)"));
                                            var addressBook4 = mainWindow1.FindFirstDescendant(cf => cf.ByName("关闭(&C)"));
                                            addressBook3.Click();
                                            addressBook4.Click();

                                        }
                                        catch (Exception ex)
                                        {
                                            continue;
                                        }
                                    }
                                    catch (Exception ex)
                                    {
                                    }


                                }
                                sendList.Clear();

简单录制了个屏幕:QQ群发
微信:微信群发

作者: 三角猫 出处: http://www.zu14.cn/ 版权归 三角猫 和 真有意思网 所有,转载请注明出处 using System; using System.Collections.Generic; using System.Text; namespace QQAutoMsg { /// /// 消息发送 /// internal static class QQMsgSender { /// /// 发送消息 /// /// 所以已打开的QQ窗体的列表 /// 消息内容 internal static void Go(List qqChatWindows, string msg) { foreach (EnumQQChatWindows.QQChatWindow win in qqChatWindows) { SendMsg(win.WindowHwnd, msg); } } /// /// 根据窗体句柄,找到输入框和发送按钮,发送消息出去 /// /// 聊天窗口句柄 /// 消息内容 private static void SendMsg(IntPtr hWnd, string msg) { if (NativeMethods.IsWindow(hWnd)) //确认该聊天窗口仍然有效 { ////找到 发送 按钮 IntPtr hwndButton = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "Button", "发送(S)"); if (IntPtr.Zero != hwndButton) { ////找到窗体顺序上的第一个RichEdit20A控件,其实就是消息显示框 IntPtr hwndRichEdit = NativeMethods.FindWindowEx(hWnd, IntPtr.Zero, "RichEdit20A", null); ////利用spy++,可以看到消息输入框的父窗体是类名为 AfxWnd42 的控件 ////在顺序上是显示框的下一个窗体 if (IntPtr.Zero != hwndRichEdit) { ////找到 AfxWnd42 这个窗体 hwndRichEdit = NativeMethods.GetWindow(hwndRichEdit, NativeMethods.GW_HWNDNEXT); if (IntPtr.Zero != hwndRichEdit) { ////这才是真正的消息输入框 hwndRichEdit = NativeMethods.FindWindowEx(hwndRichEdit, IntPtr.Zero, "RichEdit20A", null); if (hwndRichEdit != IntPtr.Zero) { ////发送消息,因为QQ屏蔽了 WM_SETTEXT, WM_PASTE 命令,所有采用 EM_REPLACESEL 来实现 NativeMethods.SendMessage(hwndRichEdit, NativeMethods.EM_REPLACESEL, IntPtr.Zero, msg); ////给发送按钮发 鼠标单击消息 NativeMethods.SendMessage(hwndButton, NativeMethods.BM_CLICK, IntPtr.Zero, IntPtr.Zero); } } } } } } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值