C# 向TIM或者QQ自动发送中文消息【微信也是可用的】 附测试GIF

之前用C++简单的写了一个demo

现在用C#写了完整版

  • 定义字符
  • 定义发送数量
  • 定义发送对象

注意事项

  • QQ必须单独一个窗体
  • 微信对象在输入名字的时候必须写微信

源代码

using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TimSendMessage
{
    class Program
    {
        [DllImport("user32.dll")]
        static extern IntPtr FindWindow(String ClassName,String WindwosName);

         [DllImport("user32.dll")]
         static extern void keybd_event(byte vk,byte vsacn,int flag,int wram);

        [DllImport("user32.dll")]
        static extern void PostMessage(IntPtr hwnd,uint msg,int  w,string l);
        [DllImport("user32.dll")]
        static extern void PostMessage(IntPtr hwnd, uint msg, int w, int l);

        [STAThread]
        static void Main(string[] args)
         {
            Console.WriteLine("发送QQ的名字");
            var name = Console.ReadLine();
            Console.WriteLine("要发送的字符");
            var t = Console.ReadLine();
            Console.WriteLine("要发送的次数");
            var Count = int.Parse(Console.ReadLine());

           while(Count>-1)
            {
                Thread.Sleep(TimeSpan.FromMilliseconds(50));
                Clipboard.SetText(t);
                SendKey(name,t);
                Count--;
                Console.WriteLine("测试次数"+Count);
            }
         }
        static void SendKey(string name,string l)
        {
            var win= FindWindow(null, name);
          
            keybd_event(0x01, 0, 0, 0);//激活TIM
            PostMessage(win, 0x0302, 0, 0);
        //    PostMessage(win, 0x0101, new Random().Next(65,128),0);//发送字符                                              //下面是发送回车
            PostMessage(win, 0x0100, 13, 0);
            PostMessage(win, 0x0101, 13, 0);
            keybd_event(0x11, 0, 0x0002, 0);
            
        }
    }
}

如果有哪位贤兄可以改成窗体也是不成问题的

源程序下载

posted @ 2018-08-14 00:16  ARM830  阅读(2502)  评论(8编辑  收藏  举报