using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Automation;
using System.Windows.Forms;
using static QQManager.QQmain;
namespace QQManager
{
public class QQmsg
{
[DllImport("user32.dll")]
//EnumWindows函数,EnumWindowsProc 为处理函数
private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
[DllImport("user32.dll")]
private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
[DllImport("user32.dll")]
private static extern bool IsWindowVisible(int hWnd);
[DllImport("user32.dll")]
private static extern int GetWindowTextLength(int hWnd);
[DllImport("USER32.DLL")]
private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
[DllImport("USER32.DLL")]
private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
[DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
//把窗体置于最前
[DllImport("user32.dll")]
public static extern bool SetForegroundWindow(IntPtr hWnd);
//拖动窗体
[DllImport("user32.dll")]
public static extern bool ReleaseCapture();
[DllImport("user32.dll")]
public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
public const int WM_SYSCOMMAND = 0x0112;
public const int SC_MOVE = 0xF010;
public const int HTCAPTION = 0x0002;
public static List<QQactivelist> Qlist = new List<QQactivelist>();
/// <summary>
/// 刷新QQ页面的句柄和值
/// </summary>
public static void ReloadQQform()
{
Qlist.Clear();
EnumWindowsProc ewp = new EnumWindowsProc(ADA_EnumWindowsProc);
EnumWindows(ewp, 0);
}
/// <summary>
/// 刷新QQ消息
/// </summary>
/// <param name="tittle"></param>
public static List<QQMessage> ReloadQQMessage(string tittle="auto")
{
List<QQMessage> allmessage = new List<QQMessage>();
foreach (var get in Qlist)
{
try {
if (tittle == "auto") { } else if (tittle == get.title) { } else { goto quickexit; }
// QQmanger.sendqq(get.ActiveHwnd, "1111");
AutomationElement window = AutomationElement.FromHandle(get.ActiveHwnd);
PropertyCondition xEllist2 = new PropertyCondition(AutomationElement.NameProperty, get.title);
AutomationElementCollection targetElement = window.FindAll(TreeScope.Element, xEllist2);
AutomationElement AllEdit = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "消息"));
if (AllEdit != null && AllEdit.Current.IsEnabled)
{
List<QQmain> Mlist = new List<QQmain>();
if ((bool)AllEdit.GetCurrentPropertyValue(AutomationElementIdentifiers.IsLegacyIAccessiblePatternAvailableProperty))
{
var pattern = ((LegacyIAccessiblePattern)AllEdit.GetCurrentPattern(LegacyIAccessiblePattern.Pattern));
string isvalue= pattern.Current.Value;
QQmain nmain = new QQmain(isvalue);
Mlist.Add(nmain);
GC.Collect();
}
if (Mlist.Count > 0) {
allmessage.Add(new QQMessage(Mlist, get.title));
}
}
quickexit:
GC.Collect();
}
catch { }
}
return allmessage;
}
private static bool MsgService = false;
public static void QQMsgService(bool check,int timeout=1000)
{
MsgService = check;
Thread T1 = new Thread(qqmsgthread);
T1.SetApartmentState(ApartmentState.STA);
T1.Start(timeout);
}
public static void qqmsgthread(object a)
{
while (MsgService)
{
Thread.Sleep(Convert.ToInt32(a));
ReloadQQform();
List<QQMessage> NewDate = ReloadQQMessage();
foreach (var dbs in NewDate)
{
OutPutQQValue(dbs.allmessage, dbs.MessageinMainTittle);
}
}
}
[DllImport("user32.dll")]
static extern void keybd_event(byte vk, byte vsacn, int flag, int wram);
[DllImport("user32.dll")]
static extern void PostMessage(IntPtr hwnd, uint msg, int w, string l);
[DllImport("user32.dll")]
static extern void PostMessage(IntPtr hwnd, uint msg, int w, int l);
public static void SendQQMsg(string tittlename, string message)
{
if (message == "NotFindValue") { return; }
foreach (var ev in Qlist)
{
if (ev.title == tittlename)
{
var win = FindWindow(null, ev.title);
string OldMessage = Clipboard.GetText();
Clipboard.SetText(message);
keybd_event(0x01, 0, 0, 0);
PostMessage(win, 0x0302, 0, 0);
PostMessage(win, 0x0100, 13, 0);
PostMessage(win, 0x0101, 13, 0);
keybd_event(0x11, 0, 0x0002, 0);
Clipboard.SetText(OldMessage);
}
}
}
public static string LastMessage = "";
public static string BeginMessage = "";
public static string allMessage = "";
/// <summary>
/// 这里是最后的消息输出
/// </summary>
/// <param name="db"></param>
/// <param name="intittle"></param>
public static void OutPutQQValue(List<QQmain> db, string intittle="")
{
LastMessage = BeginMessage = allMessage = "";
string MsgFormat = "->[{0}]{1}:{2}";
bool index = false;
foreach (var Qcls in db)
{
string RichText = "";
Qmbr lastpack = new Qmbr();
foreach (var MsgLine in Qcls.MsgQueue)
{
LastMessage = intittle+string.Format(MsgFormat,MsgLine.UserRemarks,MsgLine.UserName, MsgLine.UserMessage);
lastpack = MsgLine;
new Action<Sendtype>(e => CheckMessage(e))(new Sendtype(MsgLine, intittle,0));
RichText += LastMessage;
if (!index)
{
BeginMessage = LastMessage;
index = true;
}
}
new Action<Sendtype>(e => CheckMessage(e))(new Sendtype(lastpack, intittle, 1));
allMessage = RichText;
}
}
/// <summary>
/// 中间过渡处理
/// </summary>
/// <param name="e"></param>
public static void CheckMessage(Sendtype e)
{
Qmbr Msg = e.Message;
if (Msg.UserName == null == false)
{
if (e.MsgType == 1)
{
//新消息
string gettittlename = e.TittleName;
string getmessage = Msg.UserMessage;
SendQQMsg(gettittlename, TXDictionary(Msg, 1));
SendQQMsg(gettittlename, TXDictionary(Msg, 2));
}
else
{
}
}
GC.Collect();
}
/// <summary>
/// 词库 type=2模糊匹配 1精准匹配
/// </summary>
/// <param name="Message"></param>
/// <param name="type"></param>
public static string TXDictionary(Qmbr e,int type)
{
string message = e.UserMessage.Replace(" ", "").Replace("\r", "");
switch (type)
{
case 1:
//实现精准匹配
if (message=="测A试")
{
return "艾特:" + e.UserName;
}
if (message == "菜单")
{
string richtext = "1.签到功能了\r2.测试功能了";
return richtext;
}
break;
case 2:
//实现模糊匹配
break;
}
return "NotFindValue";
}
public delegate bool EnumWindowsProc(int hWnd, int lParam);
public static void renovateQQcls()
{
EnumWindowsProc ewp = new EnumWindowsProc(ADA_EnumWindowsProc);
EnumWindows(ewp, 0);
}
public static bool ADA_EnumWindowsProc(int hWnd, int lParam)
{
int cTxtLen = 200;
string cTitle;
if (IsWindowVisible(hWnd))
{
cTxtLen = GetWindowTextLength(hWnd) + 1;
StringBuilder text = new StringBuilder(cTxtLen);
GetWindowText(hWnd, text, cTxtLen);
cTitle = text.ToString();
StringBuilder clstext = new StringBuilder(cTxtLen);
GetClassName((IntPtr)hWnd, clstext, cTxtLen);
string GetClsName = clstext.ToString();
//TXGuiFoundation
if (GetClsName.ToLower().Contains("TXGui".ToLower()))
{
QQactivelist Qat = new QQactivelist();
Qat.title = cTitle;
Qat.classname = GetClsName;
Qat.ActiveHwnd = (IntPtr)hWnd;
Qlist.Add(Qat);
}
}
return true;
}
}
public class QQactivelist
{
public string title = "";
public string classname = "";
public IntPtr ActiveHwnd = new IntPtr();
}
public class QQmain
{
public struct Qmbr
{
public string UserName { get; set; }
public string UserMessage { get; set; }
public string UserRemarks { get; set; }
public string QQ { get; set; }//弃用
public string Lockerid { get; set; }//弃用
}
//】
public QQmain(string richmessage)
{
UpdateTime = DateTime.Now;
RichMessage = richmessage;
string[] allline = richmessage.Split('【');
DelRepeatData(ref allline);
foreach (var getstr in allline)
{
if (getstr.Replace(" ", "") == "" == false)
{
Qmbr nQmbr = new Qmbr();
nQmbr.UserRemarks = getstr.Split('】')[0];
if (getstr.Contains("\r"))
{
if (getstr.Contains("】")) {
nQmbr.UserName = getstr.Substring(0, getstr.IndexOf("\r")).Split('】')[1];
nQmbr.UserMessage = getstr.Substring(getstr.IndexOf("\r") + "\r".Length);
nQmbr.QQ = "nothing";
MsgQueue.Add(nQmbr);
}
}
}
}
}
public string RichMessage = "";
public List<Qmbr> MsgQueue = new List<Qmbr>();
public DateTime UpdateTime = new DateTime();
public static void DelRepeatData(ref string[] a)
{
a = a.GroupBy(p => p).Select(p => p.Key).ToArray();
}
}
public class QQMessage
{
public QQMessage(List<QQmain> e,string tittle)
{
allmessage.AddRange(e);
MessageinMainTittle = tittle;
}
public List<QQmain> allmessage = new List<QQmain>();
public string MessageinMainTittle = "";
}
public class Sendtype
{
public Sendtype(Qmbr message, string tittlename,int msgtype)
{
Message = message;
TittleName = tittlename;
MsgType = msgtype;
}
public Qmbr Message=new Qmbr();
public string TittleName = "";
public int MsgType = 0;//1新消息0老消息
}
}
主要实现方法通过inspectX64 获取到TIMQQ控件的消息 LegacyIAccessible.Value
if ((bool)AllEdit.GetCurrentPropertyValue(AutomationElementIdentifiers.IsLegacyIAccessiblePatternAvailableProperty))
{
LegacyIAccessiblePattern pattern = ((LegacyIAccessiblePattern)AllEdit.GetCurrentPattern(LegacyIAccessiblePattern.Pattern));
//pattern.Current.Value;
}
ps:窗口不能合并要把所有要获取的窗口单独拉出来前后台无所谓
ReloadQQMessage(你要获取消息的群名称)
List<QQMessage> getmsg = QQmsg.ReloadQQMessage("上古卷轴5研究群");
foreach (var lv1get in getmsg)
{
MessageBox.Show(lv1get.allmessage[0].RichMessage);
}
得到消息
在启动服务的时候
CheckMessage方法里可以用来实时的处理消息