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;
}
}
}
}
C# 消息发送的更多相关文章
- C#开发微信门户及应用(19)-微信企业号的消息发送(文本、图片、文件、语音、视频、图文消息等)
我们知道,企业号主要是面向企业需求而生的,因此内部消息的交流显得非常重要,而且发送.回复消息数量应该很可观,对于大企业尤其如此,因此可以结合企业号实现内部消息的交流.企业号具有关注安全.消息无限制等特 ...
- [UWP]UWP中获取联系人/邮件发送/SMS消息发送操作
这篇博客将介绍如何在UWP程序中获取联系人/邮件发送/SMS发送的基础操作. 1. 获取联系人 UWP中联系人获取需要引入Windows.ApplicationModel.Contacts名称空间. ...
- Kafka、RabbitMQ、RocketMQ消息中间件的对比 —— 消息发送性能-转自阿里中间件
引言 分布式系统中,我们广泛运用消息中间件进行系统间的数据交换,便于异步解耦.现在开源的消息中间件有很多,前段时间我们自家的产品 RocketMQ (MetaQ的内核) 也顺利开源,得到大家的关注. ...
- iOS开发小技巧--即时通讯项目:消息发送框(UITextView)高度的变化; 以及UITextView光标复位的小技巧
1.即时通讯项目中输入框(UITextView)跟随输入文字的增多,高度变化的实现 最主要的方法就是监听UITextView的文字变化的方法- (void)textViewDidChange:(UIT ...
- activemq安装与简单消息发送接收实例
安装环境:Activemq5.11.1, jdk1.7(activemq5.11.1版本需要jdk升级到1.7),虚拟机: 192.168.147.131 [root@localhost softwa ...
- eBay 消息发送(2)
1.简介 Call Index Doc: http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/index.html 消息发送主要 ...
- eBay 消息发送(1)
1.简介 Call Index Doc: http://developer.ebay.com/DevZone/XML/docs/Reference/eBay/index.html 消息发送主要 ...
- twitter storm源码走读之2 -- tuple消息发送场景分析
欢迎转载,转载请注明出处源自徽沪一郎.本文尝试分析tuple发送时的具体细节,本博的另一篇文章<bolt消息传递路径之源码解读>主要从消息接收方面来阐述问题,两篇文章互为补充. worke ...
- ActiveMQ点对点的消息发送案例
公司最近会用MQ对某些业务进行处理,所以,这次我下载了apache-activemq-5.12.0-bin把玩下. 基于练习方便需要,使用Windows的版本. 参考的优秀文章: activemq的几 ...
- 高效的TCP消息发送组件
目前的.net 架构下缺乏高效的TCP消息发送组件,而这种组件是构建高性能分布式应用所必需的.为此我结合多年的底层开发经验开发了一个.net 下的高效TCP消息发送组件.这个组件在异步发送时可以达到每 ...
随机推荐
- Java集合篇之set,面试官:请说一说HashSet、LinkedHashSet、TreeSet的区别?
写在开头 Java的集合世界中主要由List,Set,Queue,Map构成,我们在之前的博文中已经学习了List,接下来我们继续学习Set集合. Set特点:存取无序,不可以存放重复的元素,不可以用 ...
- Javascript中的var变量声明作用域问题
先看一下这两段代码的执行结果 var name2 = 'What!'; function a() { if (typeof name2 === 'undefined') { console.log(' ...
- Spring异步任务async介绍与案例实战
关于spring异步任务 简单地说,用@Async注释bean的方法将使其在单独的线程中执行.换句话说,调用者不会等待被调用方法的完成.利用spring提供的注解即可简单轻松的实现异步任务处理. 默认 ...
- win32- 窗口模板
主要用于日常的win32窗口的测试 #include <Windows.h> #include <stdio.h> #include <iostream> usin ...
- 服务端高性能网络IO编程模型简析
服务端高性能网络IO编程模型简析 一.客户端与服务器端 多数网络应用可以分为客户端(client)和服务器端(server)模型,然后中间通过各种定义的协议来进行两端的通信. 比如常用的 Nginx ...
- Alpine安装gcc g++ make编译环境
apk add gcc g++ make cmake gfortran libffi-dev openssl-dev libtool
- python内置模块argparse的使用
官网文档 https://docs.python.org/3/howto/argparse.html # 简易教程 https://docs.python.org/3/library/argparse ...
- 解决macOS Big Sur系统pyenv不能安装python3.6.x版本的问题及pyenv-virtualenv的安装使用
前置 先安装好pyenv brew install pyenv 配置环境 echo -e 'if command -v pyenv 1>/dev/null 2>&1; then\n ...
- 【Azure 应用服务】App Service - 在修改应用服务计划的页面中,为什么无法查看到同一个资源组下面的其他应用服务计划(App Service Plan)呢?
问题描述 在App Service的门户上,可以通过"Change App Service Plan"来改变当前App Service所属的应用服务计划(App Service P ...
- 16. Class字节码结构
1. 相关概念 1.1字节码文件的跨平台性 Java 语言是跨平台的(write once, run anywhere) 当 Java 源代码成功编译成字节码后,如果想在不同的平台上面运行, 则无须再 ...