C# 消息发送
界面
frmMain
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Diagnostics;
using System.Threading;
namespace lxw_QQAutoSendMsg
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private List<QQChatWindows> ltQQChatWindows = new List<QQChatWindows>();
private void btnFindQQWindow_Click(object sender, EventArgs e)
{
FindQQChatWindows();
}
private void btnSendMsg_Click(object sender, EventArgs e)
{
if (this.txtSendText.Text.Equals(String.Empty))
{
MessageBox.Show("请输入要发送的内容");
return;
}
if (ltQQChatWindows.Count <= 0)
{
MessageBox.Show("没有可用发送的聊天窗体,请先查找对话框!");
return;
}
if (this.listQQWindows.SelectedItem == null)
{
MessageBox.Show("请选择需要发送消息的窗体");
return;
}
string qqCaption = this.listQQWindows.SelectedItem.ToString();
QQChatWindows qqChatWindows = ltQQChatWindows.Find(o => o.Caption.Equals(qqCaption));
if (qqChatWindows == null)
{
MessageBox.Show("没有找到可发送信息的QQ聊天窗体,请先查找对话框!");
}
else
{
int RepeatTime = 10;
int SleepTime = 1000;
if (!int.TryParse(txtRepeatTime.Text, out RepeatTime))
{
RepeatTime = 10;
}
if (!int.TryParse(txtSleepTime.Text, out SleepTime))
{
SleepTime = 1000;
}
string error = "";
string time = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss");
string info = "";
for (int i = 0; i < RepeatTime; i++)
{
if (this.SendQQMsg(qqChatWindows.WindowHwnd, this.txtSendText.Text, out error))
{
info = time + " [" + qqCaption + "]-->" + "发送成功";
}
else
{
info = time + " [" + qqCaption + "]-->" + "发送失败:" + error;
}
if (info.Contains("发送失败"))
{
//高亮显示
rtxtInfo.SelectionStart = rtxtInfo.Text.Length;
rtxtInfo.SelectionLength = info.Length;
rtxtInfo.SelectionColor = Color.FromName("Red");
}
rtxtInfo.AppendText(info + "\r\n");
Thread.Sleep(SleepTime);
}
}
}
/// <summary>
/// 查找QQ对话框
/// </summary>
private void FindQQChatWindows()
{
this.listQQWindows.Items.Clear();
ltQQChatWindows.Clear();
WinAPI.EnumDesktopWindows(IntPtr.Zero, new WinAPI.EnumDesktopWindowsDelegate(EnumWindowsProc), IntPtr.Zero);
}
private bool EnumWindowsProc(IntPtr hWnd, uint lParam)
{
string qqProName = this.GetProcessName(hWnd);
StringBuilder className = new StringBuilder(255 + 1); //ClassName 最长
WinAPI.GetClassName(hWnd, className, className.Capacity);
if (!qqProName.Equals(String.Empty) && qqProName.Equals("QQ") && className.ToString().Equals("TXGuiFoundation"))
{
StringBuilder caption = new StringBuilder(WinAPI.GetWindowTextLength(hWnd) + 1);
WinAPI.GetWindowText(hWnd, caption, caption.Capacity);
if (!caption.ToString().Equals(String.Empty) && !caption.ToString().Equals("TXMenuWindow"))
{
QQChatWindows qqchat = new QQChatWindows(hWnd, caption.ToString());
ltQQChatWindows.Add(qqchat);
this.listQQWindows.Items.Add(caption);
}
}
return true;
}
public string GetProcessName(IntPtr hWnd)
{
try
{
string processname = String.Empty;
int proid = 0;
uint threadid = WinAPI.GetWindowThreadProcessId(hWnd, out proid);
if (threadid > 0 && proid > 0)
{
Process pro = Process.GetProcessById(proid);
processname = pro.ProcessName;
}
return processname;
}
catch
{
return String.Empty;
}
}
/// <summary>
/// 发送消息
/// </summary>
/// <param name="hWnd"></param>
/// <param name="sendText"></param>
/// <param name="error"></param>
/// <returns></returns>
private bool SendQQMsg(IntPtr hWnd, string sendText, out string error)
{
error = "";
try
{
WinAPI.ShowWindow(hWnd, WinAPI.ShowWindowCommands.Normal);
WinAPI.BringWindowToTop(hWnd);
SendKeys.SendWait(sendText);
//SendKeys.SendWait("^{ENTER}");//Ctrl+Enter
SendKeys.Send("{ENTER}");//Enter
return true;
}
catch (Exception ex)
{
error = ex.Message;
return false;
}
}
}
}
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?