C# 控制微信客户端自动发送消息

C# 控制微信客户端自动发送消息

添加引用

.net core以上版本的项目需要设置目标os为windows
Main方法需要[STAThread]标记

using FlaUI.Core;
using FlaUI.Core.AutomationElements;
using FlaUI.Core.Conditions;
using FlaUI.Core.Input;
using FlaUI.Core.WindowsAPI;
using FlaUI.UIA3;

代码

    internal class Program
    {


        [DllImport("user32.dll", EntryPoint = "ShowWindow", SetLastError = true)]
        static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);


        [DllImport("user32.dll", EntryPoint = "FindWindow", SetLastError = true)]
        static extern IntPtr FindWindow(string lpClassName, string lpWindowName);

        [DllImport("kernel32.dll", SetLastError = true)]
        static extern IntPtr GetConsoleWindow();

        [DllImport("user32.dll", SetLastError = true)]
        internal static extern bool MoveWindow(IntPtr hWnd, int X, int Y, int nWidth, int nHeight, bool bRepaint);



        [STAThread]
        static void Main(string[] args)
        {
        	// 设置控制台程序窗口位置和大小
            IntPtr ptr = GetConsoleWindow();
            MoveWindow(ptr, 0, 0, 200, 100, true);


            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)
                        {
                        	// cf.Name.Contains(target) 模糊查询,也可以 cf.Name==target 精确查询
                            var searchItem = searchList.FindAllDescendants().FirstOrDefault(cf => cf.Name.Contains(target)  && cf.ControlType == FlaUI.Core.Definitions.ControlType.ListItem);
                            searchItem?.DrawHighlight(System.Drawing.Color.Red);
                            searchItem?.AsListBoxItem().Click();
                        }
                        else
                        {
                            Console.WriteLine("没有搜索到内容");
                            return;
                        }
                        Thread.Sleep(500);


                        //6.输入文本
                        string sendMsg = "自动消息测试";
                        var msgInput = mainWindow.FindFirstDescendant(cf => cf.ByName("输入")).AsTextBox();

                        if (msgInput == null) return;

                        msgInput?.Click();
                        System.Windows.Forms.Clipboard.SetText(sendMsg);
                        Keyboard.TypeSimultaneously(new[] { VirtualKeyShort.CONTROL, VirtualKeyShort.KEY_V });

                        Thread.Sleep(500);

                        //按下回车
                        Keyboard.Press(VirtualKeyShort.ENTER);

						//点击发送按钮
                        //var sendBtn = mainWindow.FindFirstDescendant(cf => cf.ByName("发送(S)"));
                        //Console.WriteLine(sendBtn.Name);
                        //sendBtn?.DrawHighlight(System.Drawing.Color.Red);
                        //sendBtn?.Click();

                    }
                }


            }

        }
    }



三级目录

  • 0
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 3
    评论
### 回答1: c是第三个英文字母,在字母表中排名为3。它是一个常见的字母,广泛使用在英语和许多其他语言的拼写中。 在计算机科学中,c是一种编程语言,也是一种广泛使用的编程语言之一。由于其简单的语法和高效的执行速度,C语言被广泛应用于系统程序设计和嵌入式系统开发。同时,C语言也是许多其他编程语言的基础,如C++和Java。 此外,“C”还可以代表一些其他概念。例如,C型血液是指人体血液中的一种特定血型,大约占全球人口的37%。C型血液可以分为C型RH阳性和C型RH阴性两种类型。 “C”还可以代表一些单位和术语。在化学中,C是碳元素的化学符号。在物理学中,C代表光速,其数值约为299,792,458米/秒。在数学中,C代表复数集合中的一个特定复数,即C={a+bi|a,b∈R}。 总而言之,c是一个常见的字母,代表许多不同的概念和领域。无论是在语言、计算机科学、医学还是数学中,c都扮演着重要的角色。 ### 回答2: c是英文字母表中的第3个字母。它的大小写形式分别是C和c。c在英语中可以表示许多不同的含义和用途。例如,在计算机科学中,C是一种编程语言,它被广泛用于开发软件和系统。 C语言具有高效性和灵活性,因此它是许多程序员的首选语言之一。此外,在数学中,c可以表示复数的虚部,用于表示一个复数的实部和虚部。在音乐中,C是音阶的第一音符,通常用作参考音。 在日常生活中,c还可以代表一系列词汇,例如cat(猫)、cap(帽子)、car(汽车)等等。总之,c是一个常见的字母,它在不同的领域和语境中有着不同的含义和用途。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

weixin_42199478

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值