当前位置:   article > 正文

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

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();
    }
}



  • 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
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135

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


搞定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();
        }
    }

  • 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

源码

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

闽ICP备14008679号