当前位置:   article > 正文

C#实现微信消息自动发送_c#发送微信消息

c#发送微信消息

群发消息

C#实现微信消息自动发送

微信UI自动化测试的意义主要体现在以下几个方面:
1.降低人为错误:由于自动化测试是由代码驱动的,因此可以避免人为错误,如遗漏、误操作等。
2.节省人力资源:通过自动化消息发送,特别是在夜间或非工作时间进行测试时,自动化发送消息可以替代部分人力。
总的来说,微信UI自动化测试能够大大提升效率和准确性,作为 Microsoft 支持的库,FlaUIAutomation 与 Windows 和其他 Microsoft 技术紧密集成,提供了更好的稳定性和支持。我通过近几周的研究,最终选择了FlaUI.UIA3来开发。

使用FlaUI

FlaUI.UIA3 是一个用于自动化测试 Windows 应用程序的库,基于微软的本地 UI 自动化库。以下是 FlaUI.UIA3 的主要特性:

  1. 广泛的适用性 :FlaUI.UIA3 支持多种 Windows 应用程序,包括 Win32、WinForms、WPF 和 Windows 商店应用。;
  2. 基于微软的本地 UI 自动化库: FlaUI.UIA3 是基于微软的本地 UI 自动化库构建的,因此它提供了一种围绕这些库的包装;
  3. 最新的版本: UIA3 是 UI 自动化库的最新版本,专门适用于 WPF 和 Windows 商店应用程序;
    4.可扩展性:虽然 FlaUI.UIA3 提供了大量的自动化功能,但在遇到特殊需求时,它还提供了本机对象来满足更复杂的需求;
  4. 提供干净的代码库:FlaUI.UIA3 从头开始重写,旨在提供一个干净的代码库,以便于协作和进一步的开发;
  5. 易于使用:开发者可以在 FlaUI 和 UIA3 之间进行选择,以适应他们的测试需求;
  6. 增加了 焦点写作模式、预览模式、简洁写作模式、左右区域同步滚轮设置 等功能,功能按钮位于编辑区域与预览区域中间。

先看截图

在这里插入图片描述

系统逻辑

首先,其中包含了对Windows API函数的声明和导入。Windows API是一组用于与Windows操作系统交互的函数和数据结构。这些API函数在"user32.dll"等动态链接库中定义。

下面是每个导入函数的简单解释:

SwitchToThisWindow : 这个函数用于将一个窗口带到前台。第一个参数hWnd是要带到前台的窗口的句柄,第二个参数fAltTab决定是否通过按Alt+Tab键来切换到这个窗口。
OpenClipboard : 这个函数用于打开剪贴板并获取所有权。
CloseClipboard : 这个函数用于关闭剪贴板并释放其所有权。
EmptyClipboard : 这个函数用于清空剪贴板的内容。
IsClipboardFormatAvailabl : 这个函数用于检查指定的剪贴板格式是否可用。
GetClipboardData : 这个函数用于检索指定格式的剪贴板数据。
SetClipboardData : 这个函数用于在剪贴板上设置指定格式的数据。
这些函数通常用于与剪贴板进行交互,例如读取和写入数据,以及处理窗口的焦点等。

[DllImport("user32.dll", CharSet = CharSet.Auto)]
        public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);
        [DllImport("User32")]
        public static extern bool OpenClipboard(IntPtr hWndNewOwner);

        [DllImport("User32")]
        public static extern bool CloseClipboard();

        [DllImport("User32")]
        public static extern bool EmptyClipboard();

        [DllImport("User32")]
        public static extern bool IsClipboardFormatAvailable(int format);

        [DllImport("User32")]
        public static extern IntPtr GetClipboardData(int uFormat);

        [DllImport("User32", CharSet = CharSet.Unicode)]
        public static extern IntPtr SetClipboardData(int uFormat, IntPtr hMem);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19

使用 Application.Attach 方法将程序附加到微信进程。这里假设 processes 是一个包含微信进程信息的列表,First() 方法获取该列表中的第一个元素(即微信进程),Id 属性获取该进程的ID。

private static Process[] processes = Process.GetProcessesByName("WeChat");
        public static Window WeChatWindow;

        static FlaUIAutomation()
        {
            using (var app = Application.Attach(processes.First().Id))
            {
                using (var automation = new UIA3Automation())
                {
                    WeChatWindow = app.GetMainWindow(automation);
                }
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
        public void method(string name,string xiaoxi)
        {
            
            //微信主界面窗口从左向右分3分窗口,可以使用inspect.exe  程序来看实际窗口,控件排列
            string target = name;
            string sendMsg = xiaoxi;


            Process[] processes = Process.GetProcessesByName("WeChat");
            if (processes.Count() != 1)
            {
                Console.WriteLine("微信未启动或启动多个微信");
            }
            else
            {
                //1.附加到微信进程
                using (var app = Application.Attach(processes.First().Id))
                {

                    using (var automation = new UIA3Automation())
                    {

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

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

                        //3.切换到通讯录

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

                        //导航窗口
                        var navWind = childWind.FindChildAt(0); //窗口第一部分
                        navWind.DrawHighlight(System.Drawing.Color.Red);
                        var addressBook = navWind.FindFirstDescendant(cf => cf.ByName("通讯录"));
                        addressBook.DrawHighlight(System.Drawing.Color.Red);
                        Console.WriteLine("点击通讯录");
                        addressBook.Click();

                        //4.搜索

                        var secondChild = childWind.FindChildAt(1); //窗口第二部分
                        secondChild.DrawHighlight(System.Drawing.Color.Red);
                        var searchTextBox = secondChild.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox();
                        searchTextBox.DrawHighlight(System.Drawing.Color.Red);
                        searchTextBox.Click();
                        Keyboard.Type(target);
                        Keyboard.Type(VirtualKeyShort.RETURN);
                        Console.WriteLine($"搜索目标对象:{target}");

                        //5.找到搜索到的联系人,切换到对话框
                        Thread.Sleep(10);
                        var tempList = secondChild.FindChildAt(1).FindAllDescendants(cf => cf.ByControlType(ControlType.List));
                        tempList[1].DrawHighlight(System.Drawing.Color.Red);
                        var searchItem = tempList[1].FindAllDescendants(cf => cf.ByControlType(ControlType.ListItem)).FirstOrDefault(cf => cf.Name == target);
                        if (searchItem == null)
                        {

                            Console.WriteLine($"未找到联系人:{target}");
                            Console.WriteLine("发送消息失败");
                        }
                        else
                        {
                            searchItem.DrawHighlight(System.Drawing.Color.Red);
                            searchItem.Click();

                            Thread.Sleep(10);
                            //6.输入文本
                            var lastChild = childWind.FindChildAt(2); //窗口第三部分
                            lastChild.DrawHighlight(System.Drawing.Color.Red);

                            var msgInput = lastChild.FindAllDescendants(cf => cf.ByControlType(ControlType.Edit)).First();
                            msgInput.DrawHighlight(System.Drawing.Color.Red);
                            msgInput?.Click();
                            SetText(sendMsg);
                            Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
                            var sendBtn = lastChild.FindFirstDescendant(cf => cf.ByName("发送(S)"));
                            sendBtn?.DrawHighlight(System.Drawing.Color.Red);
                            sendBtn?.Click();
                            Console.WriteLine("发送完成");
                        }


                    }
                }
            }
        }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91

注意:群聊需要保存到通讯录,才能被获取。
在这里插入图片描述
简单录制了个屏幕:QQ群发
微信:微信群发

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/376506
推荐阅读
相关标签
  

闽ICP备14008679号