当前位置:   article > 正文

C#UI Automation获取TIMQQ聊天信息收发_c# iuiautomation

c# iuiautomation
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Runtime.InteropServices;
  5. using System.Text;
  6. using System.Text.RegularExpressions;
  7. using System.Threading;
  8. using System.Threading.Tasks;
  9. using System.Windows.Automation;
  10. using System.Windows.Forms;
  11. using static QQManager.QQmain;
  12. namespace QQManager
  13. {
  14. public class QQmsg
  15. {
  16. [DllImport("user32.dll")]
  17. //EnumWindows函数,EnumWindowsProc 为处理函数
  18. private static extern int EnumWindows(EnumWindowsProc ewp, int lParam);
  19. [DllImport("user32.dll")]
  20. private static extern int GetWindowText(int hWnd, StringBuilder title, int size);
  21. [DllImport("user32.dll")]
  22. private static extern bool IsWindowVisible(int hWnd);
  23. [DllImport("user32.dll")]
  24. private static extern int GetWindowTextLength(int hWnd);
  25. [DllImport("USER32.DLL")]
  26. private static extern IntPtr FindWindow(string lpClassName, string lpWindowName);
  27. [DllImport("USER32.DLL")]
  28. private static extern bool ShowWindow(IntPtr hWnd, uint nCmdShow);
  29. [DllImport("user32.dll", SetLastError = true, CharSet = CharSet.Auto)]
  30. static extern int GetClassName(IntPtr hWnd, StringBuilder lpClassName, int nMaxCount);
  31. //把窗体置于最前
  32. [DllImport("user32.dll")]
  33. public static extern bool SetForegroundWindow(IntPtr hWnd);
  34. //拖动窗体
  35. [DllImport("user32.dll")]
  36. public static extern bool ReleaseCapture();
  37. [DllImport("user32.dll")]
  38. public static extern bool SendMessage(IntPtr hwnd, int wMsg, int wParam, int lParam);
  39. public const int WM_SYSCOMMAND = 0x0112;
  40. public const int SC_MOVE = 0xF010;
  41. public const int HTCAPTION = 0x0002;
  42. public static List<QQactivelist> Qlist = new List<QQactivelist>();
  43. /// <summary>
  44. /// 刷新QQ页面的句柄和值
  45. /// </summary>
  46. public static void ReloadQQform()
  47. {
  48. Qlist.Clear();
  49. EnumWindowsProc ewp = new EnumWindowsProc(ADA_EnumWindowsProc);
  50. EnumWindows(ewp, 0);
  51. }
  52. /// <summary>
  53. /// 刷新QQ消息
  54. /// </summary>
  55. /// <param name="tittle"></param>
  56. public static List<QQMessage> ReloadQQMessage(string tittle="auto")
  57. {
  58. List<QQMessage> allmessage = new List<QQMessage>();
  59. foreach (var get in Qlist)
  60. {
  61. try {
  62. if (tittle == "auto") { } else if (tittle == get.title) { } else { goto quickexit; }
  63. // QQmanger.sendqq(get.ActiveHwnd, "1111");
  64. AutomationElement window = AutomationElement.FromHandle(get.ActiveHwnd);
  65. PropertyCondition xEllist2 = new PropertyCondition(AutomationElement.NameProperty, get.title);
  66. AutomationElementCollection targetElement = window.FindAll(TreeScope.Element, xEllist2);
  67. AutomationElement AllEdit = window.FindFirst(TreeScope.Descendants, new PropertyCondition(AutomationElement.NameProperty, "消息"));
  68. if (AllEdit != null && AllEdit.Current.IsEnabled)
  69. {
  70. List<QQmain> Mlist = new List<QQmain>();
  71. if ((bool)AllEdit.GetCurrentPropertyValue(AutomationElementIdentifiers.IsLegacyIAccessiblePatternAvailableProperty))
  72. {
  73. var pattern = ((LegacyIAccessiblePattern)AllEdit.GetCurrentPattern(LegacyIAccessiblePattern.Pattern));
  74. string isvalue= pattern.Current.Value;
  75. QQmain nmain = new QQmain(isvalue);
  76. Mlist.Add(nmain);
  77. GC.Collect();
  78. }
  79. if (Mlist.Count > 0) {
  80. allmessage.Add(new QQMessage(Mlist, get.title));
  81. }
  82. }
  83. quickexit:
  84. GC.Collect();
  85. }
  86. catch { }
  87. }
  88. return allmessage;
  89. }
  90. private static bool MsgService = false;
  91. public static void QQMsgService(bool check,int timeout=1000)
  92. {
  93. MsgService = check;
  94. Thread T1 = new Thread(qqmsgthread);
  95. T1.SetApartmentState(ApartmentState.STA);
  96. T1.Start(timeout);
  97. }
  98. public static void qqmsgthread(object a)
  99. {
  100. while (MsgService)
  101. {
  102. Thread.Sleep(Convert.ToInt32(a));
  103. ReloadQQform();
  104. List<QQMessage> NewDate = ReloadQQMessage();
  105. foreach (var dbs in NewDate)
  106. {
  107. OutPutQQValue(dbs.allmessage, dbs.MessageinMainTittle);
  108. }
  109. }
  110. }
  111. [DllImport("user32.dll")]
  112. static extern void keybd_event(byte vk, byte vsacn, int flag, int wram);
  113. [DllImport("user32.dll")]
  114. static extern void PostMessage(IntPtr hwnd, uint msg, int w, string l);
  115. [DllImport("user32.dll")]
  116. static extern void PostMessage(IntPtr hwnd, uint msg, int w, int l);
  117. public static void SendQQMsg(string tittlename, string message)
  118. {
  119. if (message == "NotFindValue") { return; }
  120. foreach (var ev in Qlist)
  121. {
  122. if (ev.title == tittlename)
  123. {
  124. var win = FindWindow(null, ev.title);
  125. string OldMessage = Clipboard.GetText();
  126. Clipboard.SetText(message);
  127. keybd_event(0x01, 0, 0, 0);
  128. PostMessage(win, 0x0302, 0, 0);
  129. PostMessage(win, 0x0100, 13, 0);
  130. PostMessage(win, 0x0101, 13, 0);
  131. keybd_event(0x11, 0, 0x0002, 0);
  132. Clipboard.SetText(OldMessage);
  133. }
  134. }
  135. }
  136. public static string LastMessage = "";
  137. public static string BeginMessage = "";
  138. public static string allMessage = "";
  139. /// <summary>
  140. /// 这里是最后的消息输出
  141. /// </summary>
  142. /// <param name="db"></param>
  143. /// <param name="intittle"></param>
  144. public static void OutPutQQValue(List<QQmain> db, string intittle="")
  145. {
  146. LastMessage = BeginMessage = allMessage = "";
  147. string MsgFormat = "->[{0}]{1}:{2}";
  148. bool index = false;
  149. foreach (var Qcls in db)
  150. {
  151. string RichText = "";
  152. Qmbr lastpack = new Qmbr();
  153. foreach (var MsgLine in Qcls.MsgQueue)
  154. {
  155. LastMessage = intittle+string.Format(MsgFormat,MsgLine.UserRemarks,MsgLine.UserName, MsgLine.UserMessage);
  156. lastpack = MsgLine;
  157. new Action<Sendtype>(e => CheckMessage(e))(new Sendtype(MsgLine, intittle,0));
  158. RichText += LastMessage;
  159. if (!index)
  160. {
  161. BeginMessage = LastMessage;
  162. index = true;
  163. }
  164. }
  165. new Action<Sendtype>(e => CheckMessage(e))(new Sendtype(lastpack, intittle, 1));
  166. allMessage = RichText;
  167. }
  168. }
  169. /// <summary>
  170. /// 中间过渡处理
  171. /// </summary>
  172. /// <param name="e"></param>
  173. public static void CheckMessage(Sendtype e)
  174. {
  175. Qmbr Msg = e.Message;
  176. if (Msg.UserName == null == false)
  177. {
  178. if (e.MsgType == 1)
  179. {
  180. //新消息
  181. string gettittlename = e.TittleName;
  182. string getmessage = Msg.UserMessage;
  183. SendQQMsg(gettittlename, TXDictionary(Msg, 1));
  184. SendQQMsg(gettittlename, TXDictionary(Msg, 2));
  185. }
  186. else
  187. {
  188. }
  189. }
  190. GC.Collect();
  191. }
  192. /// <summary>
  193. /// 词库 type=2模糊匹配 1精准匹配
  194. /// </summary>
  195. /// <param name="Message"></param>
  196. /// <param name="type"></param>
  197. public static string TXDictionary(Qmbr e,int type)
  198. {
  199. string message = e.UserMessage.Replace(" ", "").Replace("\r", "");
  200. switch (type)
  201. {
  202. case 1:
  203. //实现精准匹配
  204. if (message=="测A试")
  205. {
  206. return "艾特:" + e.UserName;
  207. }
  208. if (message == "菜单")
  209. {
  210. string richtext = "1.签到功能了\r2.测试功能了";
  211. return richtext;
  212. }
  213. break;
  214. case 2:
  215. //实现模糊匹配
  216. break;
  217. }
  218. return "NotFindValue";
  219. }
  220. public delegate bool EnumWindowsProc(int hWnd, int lParam);
  221. public static void renovateQQcls()
  222. {
  223. EnumWindowsProc ewp = new EnumWindowsProc(ADA_EnumWindowsProc);
  224. EnumWindows(ewp, 0);
  225. }
  226. public static bool ADA_EnumWindowsProc(int hWnd, int lParam)
  227. {
  228. int cTxtLen = 200;
  229. string cTitle;
  230. if (IsWindowVisible(hWnd))
  231. {
  232. cTxtLen = GetWindowTextLength(hWnd) + 1;
  233. StringBuilder text = new StringBuilder(cTxtLen);
  234. GetWindowText(hWnd, text, cTxtLen);
  235. cTitle = text.ToString();
  236. StringBuilder clstext = new StringBuilder(cTxtLen);
  237. GetClassName((IntPtr)hWnd, clstext, cTxtLen);
  238. string GetClsName = clstext.ToString();
  239. //TXGuiFoundation
  240. if (GetClsName.ToLower().Contains("TXGui".ToLower()))
  241. {
  242. QQactivelist Qat = new QQactivelist();
  243. Qat.title = cTitle;
  244. Qat.classname = GetClsName;
  245. Qat.ActiveHwnd = (IntPtr)hWnd;
  246. Qlist.Add(Qat);
  247. }
  248. }
  249. return true;
  250. }
  251. }
  252. public class QQactivelist
  253. {
  254. public string title = "";
  255. public string classname = "";
  256. public IntPtr ActiveHwnd = new IntPtr();
  257. }
  258. public class QQmain
  259. {
  260. public struct Qmbr
  261. {
  262. public string UserName { get; set; }
  263. public string UserMessage { get; set; }
  264. public string UserRemarks { get; set; }
  265. public string QQ { get; set; }//弃用
  266. public string Lockerid { get; set; }//弃用
  267. }
  268. //
  269. public QQmain(string richmessage)
  270. {
  271. UpdateTime = DateTime.Now;
  272. RichMessage = richmessage;
  273. string[] allline = richmessage.Split('【');
  274. DelRepeatData(ref allline);
  275. foreach (var getstr in allline)
  276. {
  277. if (getstr.Replace(" ", "") == "" == false)
  278. {
  279. Qmbr nQmbr = new Qmbr();
  280. nQmbr.UserRemarks = getstr.Split('】')[0];
  281. if (getstr.Contains("\r"))
  282. {
  283. if (getstr.Contains("】")) {
  284. nQmbr.UserName = getstr.Substring(0, getstr.IndexOf("\r")).Split('】')[1];
  285. nQmbr.UserMessage = getstr.Substring(getstr.IndexOf("\r") + "\r".Length);
  286. nQmbr.QQ = "nothing";
  287. MsgQueue.Add(nQmbr);
  288. }
  289. }
  290. }
  291. }
  292. }
  293. public string RichMessage = "";
  294. public List<Qmbr> MsgQueue = new List<Qmbr>();
  295. public DateTime UpdateTime = new DateTime();
  296. public static void DelRepeatData(ref string[] a)
  297. {
  298. a = a.GroupBy(p => p).Select(p => p.Key).ToArray();
  299. }
  300. }
  301. public class QQMessage
  302. {
  303. public QQMessage(List<QQmain> e,string tittle)
  304. {
  305. allmessage.AddRange(e);
  306. MessageinMainTittle = tittle;
  307. }
  308. public List<QQmain> allmessage = new List<QQmain>();
  309. public string MessageinMainTittle = "";
  310. }
  311. public class Sendtype
  312. {
  313. public Sendtype(Qmbr message, string tittlename,int msgtype)
  314. {
  315. Message = message;
  316. TittleName = tittlename;
  317. MsgType = msgtype;
  318. }
  319. public Qmbr Message=new Qmbr();
  320. public string TittleName = "";
  321. public int MsgType = 0;//1新消息0老消息
  322. }
  323. }

 

 

主要实现方法通过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方法里可以用来实时的处理消息

 

 

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/105153
推荐阅读
相关标签
  

闽ICP备14008679号