示例#1
0
 private void treeView1_DoubleClick(object sender, EventArgs e)
 {
     if (treeView1.SelectedNode != null)
     {
         if (FindForm(treeView1.SelectedNode) == null)
         {
             F_Chat msgform = new F_Chat();
             msgform.Tag       = treeView1.SelectedNode;
             msgform.udpsocket = udpSocket1;
             FormList.add(msgform);
             msgform.FormList = FormList;
             msgform.Text     = "与" + (treeView1.SelectedNode.Tag as ClassUserInfo).UserName + "对话";
             msgform.Show();
         }
     }
 }
示例#2
0
        private void GetMsg(byte[] Data, System.Net.IPAddress Ip, int Port)
        {
            ClassMsg msg   = (ClassMsg) new ClassSerializers().DeSerializerBinary(new MemoryStream(Data));
            string   sid   = msg.SID;   //发送方用户ID
            string   msgid = msg.msgID; //消息标识,GUID

            switch (msg.sendKind)
            {
            case SendKind.SendMsg:                                                   //发送的消息
            {
                switch (msg.sendState)                                               //消息发送的状态
                {
                case SendState.Single:                                               //容量小的单消息
                {
                    String rtf     = Encoding.Unicode.GetString(msg.Data);           //获取发送信息
                    Form   msgform = null;                                           //定义一个窗体
                    for (int i = 0; i < treeView1.Nodes.Count; i++)                  //在treeView1控件中遍历节点
                    {
                        if ((treeView1.Nodes[i].Tag as ClassUserInfo).UserID == sid) //如果当前节点的Tag值为发送信息的用户ID
                        {
                            msgform = FindForm(treeView1.Nodes[i]);                  //查找相关窗体信息
                            if (msgform != null)
                            {
                                (msgform as F_Chat).Rich_Out.SelectionStart = 0;                                             //将光标定位到RichTextBox控件的起始位
                                (msgform as F_Chat).Rich_Out.AppendText((treeView1.Nodes[i].Tag as ClassUserInfo).UserName); //将发送方的名称添加到RichTextBox控件中
                                (msgform as F_Chat).Rich_Out.AppendText("  " + DateTime.Now.ToString());                     //将发送时间添加到RichTextBox控件中
                                (msgform as F_Chat).Rich_Out.AppendText("\r\n");                                             //回车
                                (msgform as F_Chat).Rich_Out.SelectedRtf = rtf;                                              //添加发送的信息
                                msgform.Text = "与" + (treeView1.Nodes[i].Tag as ClassUserInfo).UserName + "对话";
                                msgform.Activate();                                                                          //激活窗体
                            }
                            else
                            {
                                msgform     = new F_Chat();                 //创建并引用消信发送窗体
                                msgform.Tag = treeView1.Nodes[i];           //设置远程客户端的ID号
                                (msgform as F_Chat).udpsocket = udpSocket1; //获取udpSocket1控件的所有信息
                                FormList.add(msgform);                      //将当前窗体信息添加到窗体列表中
                                (msgform as F_Chat).FormList = FormList;
                                (msgform as F_Chat).Rich_Out.SelectionStart = 0;
                                (msgform as F_Chat).Rich_Out.AppendText(
                                    (treeView1.Nodes[i].Tag as ClassUserInfo).UserName);
                                (msgform as F_Chat).Rich_Out.AppendText("  " + DateTime.Now.ToString());
                                (msgform as F_Chat).Rich_Out.AppendText("\r\n");
                                (msgform as F_Chat).Rich_Out.SelectedRtf = rtf;
                                msgform.Text = "与" + (treeView1.Nodes[i].Tag as ClassUserInfo).UserName + "对话";
                                msgform.Show();                //显示发送消息窗体
                            }
                        }
                    }
                    break;
                }

                case SendState.Start:            //大容量开始消息
                {
                    FileStream fs = null;
                    try
                    {
                        string FileName = "c:\\" + msgid + ".msg"; //设置文件接收路径
                        fs = File.Create(FileName);                //创建文件
                        fs.Write(msg.Data, 0, msg.Data.Length);    //向文件中写入接收物信息
                    }
                    finally
                    {
                        fs.Close();                //关闭文件
                    }
                    break;
                }

                case SendState.Sending:            //大容量消息发送中
                {
                    FileStream fs = null;
                    try
                    {
                        string FileName = "c:\\" + msgid + ".msg"; //设置文件接收路径
                        fs = File.OpenWrite(FileName);             //打开当前要写入的文件
                        fs.Seek(0, SeekOrigin.End);                //将该流的当前位值设为0
                        fs.Write(msg.Data, 0, msg.Data.Length);    //向文件中写入当前接收的信息
                    }
                    finally
                    {
                        fs.Close();
                    }
                    break;
                }

                case SendState.End:            //大容量消息结束
                {
                    FileStream fs = null;
                    try
                    {
                        string FileName = "c:\\" + msgid + ".msg";   //文件所在路径
                        fs = File.OpenRead(FileName);                //打开现有文件,以便进行读取
                        //fs.Seek(0, SeekOrigin.End);
                        //fs.Write(msg.Data, 0, msg.Data.Length);

                        byte[] fsdata = new byte[Convert.ToInt32(fs.Length)];          //将读取的信息存入到二进制流中
                        //fs.Seek(0, SeekOrigin.Begin);
                        fs.Read(fsdata, 0, Convert.ToInt32(fs.Length));                //将读取的流写入到缓冲区中
                        String rtf     = Encoding.Unicode.GetString(fsdata);           //将流转换成字符串
                        Form   msgform = null;
                        for (int i = 0; i < treeView1.Nodes.Count; i++)
                        {
                            if ((treeView1.Nodes[i].Tag as ClassUserInfo).UserID == sid)
                            {
                                msgform = FindForm(treeView1.Nodes[i]);
                                if (msgform != null)
                                {
                                    (msgform as F_Chat).Rich_Out.SelectionStart = 0;
                                    (msgform as F_Chat).Rich_Out.AppendText(
                                        (treeView1.Nodes[i].Tag as ClassUserInfo).UserName);
                                    (msgform as F_Chat).Rich_Out.AppendText("  " + DateTime.Now.ToString());
                                    (msgform as F_Chat).Rich_Out.AppendText("\r\n");
                                    (msgform as F_Chat).Rich_Out.SelectedRtf = rtf;
                                    msgform.Text = "与" + (treeView1.Nodes[i].Tag as ClassUserInfo).UserName + "对话";
                                    msgform.Activate();
                                }
                                else
                                {
                                    msgform     = new F_Chat();
                                    msgform.Tag = treeView1.Nodes[i];
                                    (msgform as F_Chat).udpsocket = udpSocket1;
                                    FormList.add(msgform);
                                    (msgform as F_Chat).FormList = FormList;
                                    (msgform as F_Chat).Rich_Out.SelectionStart = 0;
                                    (msgform as F_Chat).Rich_Out.AppendText(
                                        (treeView1.Nodes[i].Tag as ClassUserInfo).UserName);
                                    (msgform as F_Chat).Rich_Out.AppendText("  " + DateTime.Now.ToString());
                                    (msgform as F_Chat).Rich_Out.AppendText("\r\n");
                                    (msgform as F_Chat).Rich_Out.SelectedRtf = rtf;
                                    msgform.Text = "与" + (treeView1.Nodes[i].Tag as ClassUserInfo).UserName + "对话";
                                    msgform.Show();
                                }
                            }
                        }
                    }
                    finally
                    {
                        fs.Close();
                    }
                    break;
                }
                }
                break;
            }

            case SendKind.SendFile:
            {
                break;
            }
            }
        }