赞
踩
看到大家对于这部分微信自动发送消息实际运行时遇到很多问题,目前微信的界面也变了,这部分代码已经不再适用了。因此针对现在微信界面重新调整了代码。目前适配的微信版本是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(); } }
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。