界面

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. 基于Android的“哲学家就餐问题”防死锁的设计与实现

    1问题描述 如图1.1,有5个哲学家围着一个大圆桌就餐.哲学家和筷子都按逆时针顺序编号,每个哲学家的编号与他左边的筷子的编号一致. 规定如下: (1)哲学家之间不相互交流: (2)哲学家只做两件事:吃 ...

  2. SpringBoot+Shiro+LayUI权限管理系统项目-1.项目介绍

    1.项目介绍 本项目旨在打造一个基于RBAC架构模式的通用的.并不复杂但易用的权限管理系统.通过本项目可以较好的理解权限系统的常见业务同时学习掌握Springboot和Shiro等诸多技术点.需要的朋 ...

  3. Django实战之文件上传下载

    项目介绍 最近学习django,通过文件上传下载这个小项目,总结下常用的知识点. 做这个案例我有以下需求: 1.要支持一次上传多个文件 2.支持上传后记录上传的数据以及列表展示 3.支持下载和删除文件 ...

  4. centos上使用makefile编译sliver时 提示gcc 错误,cannot find -ldl cannot find -lpthread cannot find -lc

    github.com/bishopfox/sliver/server /usr/local/go/pkg/tool/linux_amd64/link: running gcc failed: exit ...

  5. 擅长使用iter

    def populate_ranks(votes, ranks): names = list(votes.keys()) names.sort(key=votes.get, reverse=True) ...

  6. django学习第四天----mark_safe的用法,静态文件配置,用指令创建django项目应用注意点,ORM介绍,创建表执行命令,模板渲染补充(组件),inclusion_tag 自定义标签

    补充第三天跟safe差不多的一个方法 templatetags文件夹 自定义的py文件 需要先导入模块 from django.utils.safestring import mark_safe @r ...

  7. jupyter notebook更改默认工作目录

    jupyter notebook默认配置路径:C:\Users\Administrator\.jupyter\jupyter_notebook_config.py 如果找不到配置文件,可以生成一个 j ...

  8. 【Azure API 管理】Azure API Management在设置 Policy时,如何对URL进行解码呢? 使用 HttpUtility.UrlDecode 出错

    问题描述 Azure API Management在设置 Policy时,如何对URL进行解码呢? 使用 HttpUtility.UrlDecode 出错,是否有其他可以对URL解码的方法呢? 使用H ...

  9. Isito 入门(二):Istio 的部署

    本教程已加入 Istio 系列:https://istio.whuanle.cn 目录 2,部署 Istio 安装 Helm 部署 istio-base 部署 istiod 部署 istio-ingr ...

  10. 李宏毅2022机器学习HW4 Speaker Identification下

    Task Sample Baseline模型介绍 class Classifier(nn.Module): def __init__(self, d_model=80, n_spks=600, dro ...