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

如果有哪位賢兄可以改成窗體也是不成問題的

源程序下載


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM