コード例 #1
0
    private static void Init()
    {
        for (int i = 0; i < hWndcollection.Count; i++)//扫描所有打开的窗口无动作时间,第一次运行无。
        {
            TimeSpan timeSpan = DateTime.Now - leasttimecollection[i];
            if (timeSpan.TotalMinutes > 2)
            {
                Win32Native.CloseWindow((IntPtr)hWndcollection[i]); //超过2分钟无消息变化就关闭窗口
                hWndcollection.RemoveAt(i);                         //没有变化就删除对应的那些东西
                elementcollection.RemoveAt(i);
                leastmsgcollection.RemoveAt(i);
                leasttimecollection.RemoveAt(i);
                usercollection.RemoveAt(i);
                i--;
            }
        }
        string[] msg = { "*/@#$%^%$#@~!@#", "", "" };
        //复制list
        List <int> hWndcollection_saved = new List <int>();

        hWndcollection.ForEach(i => hWndcollection_saved.Add(i)); //深拷贝上一次的句柄句柄
        Console.WriteLine("Scanning the target QQ windows...");
        Win32Native.GetWindow();                                  //扫描获取所有的窗口句柄,如果上一次扫描到的窗口还没关闭,就直接用
        for (int i = 0; i < hWndcollection.Count; i++)
        {
            bool isfind = false;
            for (int j = 0; j < hWndcollection_saved.Count; j++)
            {
                if (hWndcollection[i] == hWndcollection_saved[j])
                {
                    isfind = true;
                    break;
                }
            }
            if (isfind == false)//如果之前没有就加进去
            {
                elementcollection.Add(null);
                leastmsgcollection.Add(msg);
                leasttimecollection.Add(DateTime.Now);
            }
            Console.WriteLine(hWndcollection[i].ToString() + usercollection[i] + " " + leasttimecollection[i]);//调试用
        }
        for (int i = 0; i < hWndcollection.Count; i++)
        {
            if (elementcollection[i] != null)
            {
                continue; //如果上一轮有这个窗口,就不找对应的文本输入元素
            }
            try           //两种方式,第一种通过树形结构找,先利用inspect分析,比较快
            {
                AutomationElement temp = AutomationElement.FromHandle((IntPtr)hWndcollection[i]);
                AutomationElement _1   = TreeWalker.ControlViewWalker.GetFirstChild(temp);
                AutomationElement _3   = TreeWalker.ControlViewWalker.GetNextSibling(
                    TreeWalker.ControlViewWalker.GetNextSibling(_1));
                AutomationElement _3_1     = TreeWalker.ControlViewWalker.GetFirstChild(_3);
                AutomationElement _3_1_1_1 = TreeWalker.ControlViewWalker.GetFirstChild(
                    TreeWalker.ControlViewWalker.GetFirstChild(_3_1));
                AutomationElement _3_1_1_1_1   = TreeWalker.ControlViewWalker.GetFirstChild(_3_1_1_1);
                AutomationElement _3_1_1_1_2_1 = TreeWalker.ControlViewWalker.GetFirstChild(
                    TreeWalker.ControlViewWalker.GetNextSibling(_3_1_1_1_1));
                AutomationElement _3_1_1_1_2_1_2 = TreeWalker.ControlViewWalker.GetNextSibling(
                    TreeWalker.ControlViewWalker.GetFirstChild(_3_1_1_1_2_1));
                AutomationElement _3_1_1_1_2_1_2_1   = TreeWalker.ControlViewWalker.GetFirstChild(_3_1_1_1_2_1_2);
                AutomationElement _3_1_1_1_2_1_2_1_2 = TreeWalker.ControlViewWalker.GetNextSibling(
                    TreeWalker.ControlViewWalker.GetFirstChild(_3_1_1_1_2_1_2_1));
                AutomationElement _3_1_1_1_2_1_2_1_2_1_1 = TreeWalker.ControlViewWalker.GetFirstChild(
                    TreeWalker.ControlViewWalker.GetFirstChild(_3_1_1_1_2_1_2_1_2));
                AutomationElement _3_1_1_1_2_1_2_1_2_1_3 = TreeWalker.ControlViewWalker.GetNextSibling(
                    TreeWalker.ControlViewWalker.GetNextSibling(_3_1_1_1_2_1_2_1_2_1_1));
                AutomationElement _3_1_1_1_2_1_2_1_2_1_3_1 = TreeWalker.ControlViewWalker.GetFirstChild(_3_1_1_1_2_1_2_1_2_1_3);
                elementcollection[i] = _3_1_1_1_2_1_2_1_2_1_3_1;
            }
            catch//第二种,直接通过名字遍历窗口的所用元素,很慢
            {
                PropertyCondition nameconditon = new PropertyCondition(AutomationElement.NameProperty, "消息");
                elementcollection[i] = elementcollection[i].FindFirst(TreeScope.Descendants, nameconditon);//找到对应消息空间
            }
        }
    }