public void checkQRcode(object source, System.Timers.ElapsedEventArgs e)
 {
     try
     {
         string filePath;
         //Trace.TraceInformation("进入CheckQRcode方法");
         if (browser.InvokeRequired)
         {
             browser.BeginInvoke(new Action(delegate
             {
                 if (browser.Document == null || browser.Document.DomDocument == null || ((HTMLDocument)browser.Document.DomDocument).documentElement == null)
                 {
                     return;
                 }
                 text1 = ((HTMLDocument)browser.Document.DomDocument).documentElement.outerHTML;
             }));
         }
         else
         {
             //宿主异常结束时,在这里结束
             Application.Exit();
         }
         if (text1.Contains("https://login.weixin.qq.com/qrcode"))
         {
             t1.Stop();
             int    souceIndex = text1.IndexOf("https://login.weixin.qq.com/qrcode");
             char[] dd         = new char[47];
             text1.CopyTo(souceIndex, dd, 0, 47);
             string         url1    = new string(dd);
             HttpWebRequest request = HttpWebRequest.Create(url1) as HttpWebRequest;
             request.Method = "GET";
             // HttpWebResponse response = request.GetResponse() as HttpWebResponse;
             HttpWebResponse response = null;
             using (WebResponse wr = request.GetResponse())
             {
                 response = wr as HttpWebResponse;
                 using (Stream stream = response.GetResponseStream())
                 {
                     //当前时间作为文件名
                     filePath = OperationRecord.QRFile() + @"/" + DateTime.Now.ToString("yyyyMMddhhmmss") + ".jpg";
                     using (Stream fsStream = new FileStream(filePath, FileMode.Create))
                     {
                         stream.CopyTo(fsStream);
                     }
                 }
             }
             if (devNum != null)
             {
                 //像服务器传输二维码
                 Cloud.Login(devNum.TrimStart(), filePath);
             }
         }
     }
     catch (Exception ex)
     {
         //Trace.TraceError("获取二维码并上传图片失败:"+ex.Message);
     }
 }
        /// <summary>
        /// 最后一条好友消息和上次不一样,保存
        /// </summary>
        /// <param name="source"></param>
        /// <param name="e"></param>
        public void ScanRecord(object source, ElapsedEventArgs e)
        {
            try
            {
                if (browser.InvokeRequired)
                {
                    //扫描左侧新消息
                    browser.BeginInvoke(new Action(delegate
                    {
                        if (browser.Document == null || browser.Document.DomDocument == null || ((HTMLDocument)browser.Document.DomDocument).documentElement == null)
                        {
                            return;
                        }
                        string text = ((HTMLDocument)browser.Document.DomDocument).documentElement.outerHTML;
                        int j       = 0;
                        int type    = 0;
                        MessageInfo CurrentLastMessage = new MessageInfo();
                        String currentFriendNick       = "";
                        string uniqueFriend            = FriendNickName;//防止在保存最后一条消息的时候,切换导致的好友名称混乱

                        //判断新消息到来的最后一条消息,1.是对方发的。2.跟上条不一样
                        foreach (HtmlElement i in browser.Document.GetElementsByTagName("div"))
                        {
                            if (i.GetAttribute("ng-repeat") == "message in chatContent")  //发现消息并处理
                            {
                                j++;
                                //获取最后一条是谁发的
                                foreach (HtmlElement mes in i.GetElementsByTagName("div"))
                                {
                                    if (mes.GetAttribute("classname") == "message ng-scope me")
                                    {
                                        type = 0; //我发出的消息
                                    }
                                    else if (mes.GetAttribute("classname") == "message ng-scope you")
                                    {
                                        type = 1;  //我接收的消息
                                        foreach (HtmlElement ee in mes.GetElementsByTagName("img"))
                                        {
                                            if (ee.GetAttribute("classname") == "avatar")
                                            {
                                                currentFriendNick = ee.GetAttribute("title").ToString();
                                            }
                                        }
                                    }
                                }
                                if (i.GetElementsByTagName("span").Count != 0)//时间元素为空,则取上次消息的时间
                                {
                                    if (i.GetElementsByTagName("span")[0].InnerText.Length == 5 && !isExists(i.GetElementsByTagName("span")[0].InnerText))
                                    {
                                        //去掉秒
                                        time1 = i.GetElementsByTagName("span")[0].InnerText;
                                    }
                                }
                            }
                        }

                        if (browser.Document.GetElementsByTagName("pre").Count == 1)  //可能是好友添加信息
                        {
                            return;
                        }
                        if (j == 0)  //没有消息
                        {
                            return;
                        }
                        if (browser.Document.GetElementsByTagName("pre").Count - 1 != j)
                        {
                            if (RobotStyle == true && type == 1)
                            {
                                //   MessageBox.Show("特殊情况测试");
                                //  SendJSMessage("");
                                return;
                            }
                        }
                        //if (browser.Document.GetElementsByTagName("pre")[j - 1].InnerText == null)
                        //    return;
                        int count          = browser.Document.GetElementsByTagName("pre").Count;
                        MessageInfo nowmes = new MessageInfo
                        {
                            Content     = browser.Document.GetElementsByTagName("pre")[count - 2].InnerText,
                            MessageType = type,
                            time        = time1,
                            FriendNick  = currentFriendNick
                        };
                        Trace.TraceInformation("最后一条信息:" + nowmes.Content + "," + nowmes.time);

                        if (type == 0)  //我发送的消息,则保存
                        {
                            if (!uniqueFriend.Equals(FriendNickName))
                            {
                                return;
                            }
                            if (CompareMessage(nowmes, LastMyMessage))
                            {
                                Record d      = new Record();
                                d.MessageList = new List <MessageInfo>();
                                d.MyNick      = MyNickName;

                                d.ChatNick = uniqueFriend;
                                d.MessageList.Add(nowmes);
                                LastMyMessage = nowmes;
                                OperationRecord.WriteRecord(d);
                            }
                            return;
                        }
                        if (CompareMessage(nowmes, LastFriendMessage))
                        {
                            Record d      = new Record();
                            d.MessageList = new List <MessageInfo>();
                            d.MyNick      = MyNickName;
                            if (!uniqueFriend.Equals(FriendNickName))
                            {
                                return;
                            }
                            d.ChatNick = uniqueFriend;
                            d.MessageList.Add(nowmes);
                            LastFriendMessage = nowmes;
                            OperationRecord.WriteRecord(d);
                        }
                        if (RobotStyle == true)
                        {
                            SendJSMessage(nowmes.Content);
                        }
                    }));
                }
                else
                {
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                //Trace.TraceError("ScanRecord操作异常:"+ex.Message);
            }
        }
        //获取聊天面板上所有消息,,并保存
        public void GetAllMessage(object source, ElapsedEventArgs e)
        {
            try
            {
                if (browser.InvokeRequired)
                {
                    browser.BeginInvoke(new Action(delegate
                    {
                        if (browser.Document == null || browser.Document.DomDocument == null || ((mshtml.HTMLDocument)browser.Document.DomDocument).documentElement == null)
                        {
                            return;
                        }
                        Record AllMessage      = new Record();
                        AllMessage.MessageList = new List <MessageInfo>();

                        //聊天界面是否有消息?
                        foreach (HtmlElement i in browser.Document.GetElementsByTagName("div"))
                        {
                            if (i.GetAttribute("ng-repeat") == "message in chatContent")  //发现消息并处理
                            {
                                if (i.GetElementsByTagName("pre").Count == 0)
                                {
                                    continue;
                                }
                                AllMessage.MyNick    = MyNickName;
                                AllMessage.ChatNick  = FriendNickName;
                                MessageInfo amessage = new MessageInfo();
                                amessage.Content     = i.GetElementsByTagName("pre")[0].InnerText; //消息内容
                                foreach (HtmlElement mes in i.GetElementsByTagName("div"))
                                {
                                    if (mes.GetAttribute("classname") == "message ng-scope me")
                                    {
                                        amessage.MessageType = 0; //我发出的消息
                                    }
                                    else if (mes.GetAttribute("classname") == "message ng-scope you")
                                    {
                                        amessage.MessageType = 1;  //我接收的消息
                                        foreach (HtmlElement ee in mes.GetElementsByTagName("img"))
                                        {
                                            if (ee.GetAttribute("classname") == "avatar")
                                            {
                                                amessage.FriendNick = ee.GetAttribute("title").ToString();
                                            }
                                        }
                                    }
                                }
                                if (i.GetElementsByTagName("span").Count != 0)                                                                             //时间元素为空,则取上次消息的时间
                                {
                                    if (i.GetElementsByTagName("span")[0].InnerText.Length == 5 && !isExists(i.GetElementsByTagName("span")[0].InnerText)) //防止附件大小被误识别为日期。
                                    {
                                        amessage.time = i.GetElementsByTagName("span")[0].InnerText;
                                        LastTime      = amessage.time;
                                    }
                                }
                                else
                                {
                                    amessage.time = LastTime;
                                }
                                //把消息加入队列。如果此时切换了friend,应立即保存?
                                AllMessage.MessageList.Add(amessage);
                            }
                        }
                        //这里保存
                        SaveTimer.Stop();
                        OperationRecord.WriteMutiRecord(AllMessage);
                    }));
                }
                else
                {
                    Application.Exit();
                }
            }
            catch (Exception ex)
            {
                //Trace.TraceError("GetAllMessage处理异常:" + ex.Message);
            }
        }