统一回复:
长久没来,发现15年时候写的,代码都不存在了。。目前有问题的自行解决,代码可以做参考,但不保证现在能通。
用了二天的时间终于实验成果!从QQ6.6版到现在的QQ概念版。。
因为QQ使用了无句柄窗口,所以无法通过句柄来操作。从网上搜索了半天都是老版本的,从一个帖子中偶然发现了UI Automation,可用于UI自动化测试.
效果如下:
#代码由http://my.csdn.net/my/mycsdn 修改而来
//获取聊天内容
Process[] process = Process.GetProcessesByName("QQConcept");
//获取根节点
AutomationElement aeTop = AutomationElement.RootElement;
foreach (Process p in process)
{
if (p.MainWindowHandle != null)
{
//查找窗体名
AutomationElement aeForm = aeTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, textBox1.Text));
if (aeForm != null)
{
//寻找类型为Document的控件。在ui spy里可以查看到
AutomationElementCollection aeAllEdit = aeForm.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
Thread.Sleep(1000);
for (int i = 0; i < aeAllEdit.Count; i++)
{
try
{
//判断控件ID
if (aeAllEdit[i].Current.AutomationId == "OutputBox")
{
TextPattern textpatternPattern = aeAllEdit[i].GetCurrentPattern(TextPattern.Pattern) as TextPattern;
if (textpatternPattern != null)
{
//获取控件内的文本
string result = textpatternPattern.DocumentRange.GetText(-1);
result = result.Replace("", "");
result = result.Replace("\r", "\r\n");
SetMSG(result);
//将文本框滚动条自动滑到最新一行
Invoke(new MethodInvoker(delegate()
{
Rtb_QQMsg.SelectionStart = Rtb_QQMsg.TextLength;
Rtb_QQMsg.ScrollToCaret();
}));
}
}
}
catch
{
SetState("没有找到正确的消息资源~!");
}
}
}
else
{
// zt("当前没有打开的QQ聊天窗口~!");
}
}
else
{
SetState("没有找到QQ程序,是否已启动?");
}
}
#发送
private void sendqq()
{
SetState("");
Process[] process = Process.GetProcessesByName("QQConcept");
AutomationElement aeTop = AutomationElement.RootElement;
foreach (Process p in process)
{
if (p.MainWindowHandle != null)
{
AutomationElement aeForm = aeTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, textBox1.Text));
if (aeForm != null)
{
AutomationElementCollection aeAllEdit = aeForm.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Document));
Thread.Sleep(100);
for (int i = 0; i < aeAllEdit.Count; i++)
{
try
{
if (aeAllEdit[i].Current.AutomationId == "InputBox")
{
//先发送文本,后寻找发送按钮模拟点击
aeAllEdit[i].SetFocus();
System.Windows.Forms.SendKeys.SendWait(textBox2.Text);
//在UI目录树中找到TXGuiFoundation
AutomationElement aeForm1 = aeTop.FindFirst(TreeScope.Children, new PropertyCondition(AutomationElement.NameProperty, textBox1.Text));
if (aeForm1 != null)
{
AutomationElementCollection aeAllEdit1 = aeForm1.FindAll(TreeScope.Subtree, new PropertyCondition(AutomationElement.ControlTypeProperty, ControlType.Button));
Thread.Sleep(100);
for (int t = 0; t < aeAllEdit1.Count; t++)
{
try
{
if (aeAllEdit1[t].Current.AutomationId == "send")
{
InvokePattern ipClickButton1 = (InvokePattern)aeAllEdit1[t].GetCurrentPattern(InvokePattern.Pattern);
ipClickButton1.Invoke();
break;
}
}
catch
{
SetState("没有找到正确的消息资源~!");
}
}
}
}
}
catch
{
SetState("没有找到正确的消息资源~!");
}
}
}
else
{
// zt("当前没有打开的QQ聊天窗口~!");
}
}
else
{
SetState("没有找到QQ程序,是否已启动?");
}
}
}
通过获取回来的文本就可以做自动回复了,判断前后文本框的差值就可以获取别人发送的信息。。过滤后就可以按照内容自动回复。。