界面

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

资源下载

C# 消息发送的更多相关文章

  1. C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)

    我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...

  2. [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作

    这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...

  3. Kafka、RabbitMQ、RocketMQ消息中间件的对比 —— 消息发送性能-转自阿里中间件

    引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...

  4. iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧

    1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...

  5. activemq安装与简单消息发送接收实例

    安装环境:Activemq5.11.1, jdk1.7(activemq5.11.1版本需要jdk升级到1.7),虚拟机: 192.168.147.131 [root@localhost softwa ...

  6. eBay 消息发送(2)

      1.简介 Call Index Doc: http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/index.html   消息发送主要 ...

  7. eBay 消息发送(1)

      1.简介 Call Index Doc: http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/index.html   消息发送主要 ...

  8. twitter storm源码走读之2 -- tuple消息发送场景分析

    欢迎转载,转载请注明出处源自徽沪一郎.本文尝试分析tuple发送时的具体细节,本博的另一篇文章<bolt消息传递路径之源码解读>主要从消息接收方面来阐述问题,两篇文章互为补充. worke ...

  9. ActiveMQ点对点的消息发送案例

    公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...

  10. 高效的TCP消息发送组件

    目前的.net 架构下缺乏高效的TCP消息发送组件,而这种组件是构建高性能分布式应用所必需的.为此我结合多年的底层开发经验开发了一个.net 下的高效TCP消息发送组件.这个组件在异步发送时可以达到每 ...

随机推荐

  1. 使用webgl(three.js)创建自动化抽象化3D机房,3D机房模块详细介绍(抽象版一)

    目前市面上有两种机房 一种是普通机房 一种是由微模块组成的机房,本文主要介绍普通机房的抽象化体现模式. 抽象机房模式:机房展示过程中,我们需要对机房进行建模,当遇到大量机房需要建模时,这无疑是巨大工作 ...

  2. IoT(Internet of things)物联网入门介绍

    1.什么样的物可以入网? 要有数据传输通路 要有一点的存储功能 要有CPU 要有操作系统 要有专门的应用程序 遵循物联网的通信协议 在网络世界中有可被识别的唯一编号 2.MQTT协议 不是在说物联网吗 ...

  3. MinGW 和 MSVC

    在 Winodws 上编译通常会用到这两种工具链 MinGW(Minimalist GNU for Windows) 通常用于跨平台开发,可以编译出在 Windows 系统上运行的 .exe 程序 M ...

  4. Go微服务框架go-kratos实战学习08:负载均衡基本使用

    微服务框架 go-kratos 中负载均衡使用 一.介绍 在前面这篇文章 负载均衡和它的算法介绍,讲了什么是负载均衡以及作用.算法介绍. go-kratos 的负载均衡主要接口是 Selector,它 ...

  5. 【笔记】css —— BFC 原理

    一.什么是 BFC BFC 即 Block Formatting Contexts (块级格式化上下文),具有 BFC 特性的元素可以看作是隔离了的独立容器,容器里面的元素不会在布局上影响到外面的元素 ...

  6. go控制grpc的metadata

    grpc让我们可以像本地调用一样实现远程调用,对于每一次的RPC调用中,都可能会有一些有用的数据,而这些数据就可以通过 metadata来传递.metadata是以key-value的形式存储数据的, ...

  7. 02-Redis系列之-架构和高级API的使用

    通用部分 通用命令 # 1-keys # 打印出所有key keys * # 打印出所有以n开头的key keys n* # 打印出所有以nam开头,第四个字母是a到z的范围 keys nam[a-z ...

  8. DataGear 制作自适应任意屏幕尺寸的数据可视化看板

    DataGear 即支持以编写HTML.JavaScript.CSS源码的源码模式制作看板,也支持直观可见.友好快捷的可视模式制作看板. 本文将通过看板可视编辑模式提供的网格布局和样式设置功能,介绍如 ...

  9. 【Azure 应用服务】如何定期自动重启 Azure App Service Plan(应用服务计划)

    问题描述 如何定期自动重启 Azure App Service Plan(应用服务计划)? 因一个App Service Plan 下包含多个应用服务,如果能统一通过应用服务计划来重启所有的应用,则有 ...

  10. 【Azure 应用服务】Storage Queue触发Azure Function时报错 The input is not a valid Base-64 string

    问题描述 创建一个PowerShell脚本的Azure Function,触发方式为 Storage Queue.但执行函数结果一直失败 (Failed). 错误消息为: Executed 'Func ...