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

看到大家对于这部分微信自动发送消息实际运行时遇到很多问题,目前微信的界面也变了,这部分代码已经不再适用了。因此针对现在微信界面重新调整了代码。目前适配的微信版本是3.9.6.33。使用依赖如下
.NetFramework 4.8
FlaUI.UIA3 4.0.0
编译好的执行文件
最新源码
FlaUI.UIA3再结合inspect.exe来进行编程,可以支持很多桌面程序的自动化,大家可以自行发挥
代码如下:

using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Conditions;
using FlaUI.Core.Definitions;
using FlaUI.Core.Input;
using FlaUI.Core.WindowsAPI;
using FlaUI.UIA3;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;



class Program
{
    [DllImport("user32.dll", CharSet = CharSet.Auto)]
    public static extern bool SwitchToThisWindow(IntPtr hWnd, bool fAltTab);


    [STAThread]
    static void Main(string[] args)
    {
        //微信主界面窗口从左向右分3分窗口,可以使用inspect.exe  程序来看实际窗口,控件排列
        string target = "文件传输助手";
        string sendMsg = "hello  this is a test " + DateTime.Now.ToString();
        if (args.Length != 2)
        {
            Console.WriteLine("=======================================================================");
            Console.WriteLine("WebChatAuto  联系人  发送消息");
            Console.WriteLine("注意:联系人和发送消息需要使用双引号包括");
            Console.WriteLine("示例:WebChatAuto \"文件传输助手\"  \"Hello WebChat\"");
            Console.WriteLine("=======================================================================");
        }
        else
        {
            target = args[0];
            sendMsg = args[1];
        }

        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(500);
                    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(500);
                        //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();
                        System.Windows.Forms.Clipboard.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("发送完成");
                    }
                   

                }
            }


        }

        Console.ReadLine();
    }
}



以下是之前写的内容及代码。


搞定pywinauto微信自动发送消息后,看到是使用的UIA,然后看到FlaUI。好吧,C#也能做,然后就开干了。具体代码如下:

    class Program
    {
        [STAThread]
        static void Main(string[] args)
        {
            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);
                        Console.WriteLine("获取主界面");
                        //3.切换到通讯录
                        var elements = mainWindow.FindAll(FlaUI.Core.Definitions.TreeScope.Subtree, TrueCondition.Default);
                        var addressBook = mainWindow.FindFirstDescendant(cf => cf.ByName("通讯录"));
                        addressBook.DrawHighlight(System.Drawing.Color.Red);
                        Console.WriteLine("点击通讯录");
                        addressBook.Click();

                        4.搜索
                        string target = "文件传输助手";
                        var searchTextBox = mainWindow.FindFirstDescendant(cf => cf.ByName("搜索")).AsTextBox();
                        searchTextBox.Click();
                        Keyboard.Type(target);
                        Keyboard.Type(VirtualKeyShort.RETURN);
                        Console.WriteLine("搜索目标对象");

                        //5.切换到对话框
                        Thread.Sleep(500);
                        
                        var searchList= mainWindow.FindFirstDescendant(cf=>cf.ByName("搜索结果"));
                        if (searchList != null)
                        {
                            var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name == target && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
                            searchItem?.DrawHighlight(System.Drawing.Color.Red);
                            searchItem?.AsListBoxItem().Click();
                        }
                        else
                        {
                            Console.WriteLine("没有搜索到内容");
                        }
                        Thread.Sleep(500);
                        //6.输入文本
                        string sendMsg = "hello  this is a test "+DateTime.Now.ToString();
                        var msgInput = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();
                        msgInput?.Click();
                        System.Windows.Forms.Clipboard.SetText(sendMsg);
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });
                        var sendBtn= mainWindow.FindFirstDescendant(cf=>cf.ByName("发送(S)"));
                        sendBtn?.DrawHighlight(System.Drawing.Color.Red);
                        sendBtn?.Click();

                    }
                }
                 

            }

            Console.ReadLine();
        }
    }

源码

  • 7
    点赞
  • 66
    收藏
    觉得还不错? 一键收藏
  • 26
    评论
C是一种计算机编程语言,由美国贝尔实验室的丹尼斯·里奇于1972年开发。C语言是一种通用的高级程序设计语言,被广泛应用于系统软件、应用软件和嵌入式系统的开发中。 C语言的设计目标是提供高效、可移植和可扩展的编程环境。它具有较低的级别和较高的抽象程度,允许程序员直接访问和操作计算机硬件,同时具备更高级别语言的表达能力。 C语言具有简洁而又灵活的语法结构,使得程序员可以轻松地编写和理解代码。它支持丰富的数据类型、运算符和控制流结构,为程序员提供了强大的编程能力。同时,C语言还提供了丰富的库函数,使得编写复杂的程序变得更加简单。 C语言的特点之一是具有良好的可移植性。由于C语言在不同的计算机系统上的实现较为统一,因此编写的程序可以很容易地在不同的平台上运行。这使得C语言成为了跨平台开发的首选语言。 C语言的应用非常广泛。它被用于开发各种类型的软件,包括操作系统、编译器、数据库、网络协议等。此外,C语言还被广泛应用于嵌入式系统的开发中,因为它能够直接访问硬件资源,具备较好的性能和效率。 总之,C语言是一种功能强大、灵活且可移植的编程语言。它在计算机科学领域发挥着重要的作用,无论是在系统软件还是应用软件的开发中,都有着广泛的应用。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 26
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值