Пример #1
0
 //加好友按钮
 private void Send_Click(object sender, RoutedEventArgs e)
 {
     if (CheckID() == true)
     {
         if (FriendList.id2friends.Contains(FriendID.Text))
         {
             Message.Show(MessageFunction.Autoclose, "Error", "对方已经是你的好友了!");
         }
         else
         {
             f.temp[f.count]    = new Friends();
             f.temp[f.count].ID = FriendID.Text;
             f.toserver.Query(FriendID.Text);
             //执行上一步操作后,id2ip中肯定只有在线的,id2friend和memberdata中都会有,然而对方还不一定是好友,所以下一步要处理
             Thread.Sleep(500);
             if (FriendList.id2ip.Contains(FriendID.Text))
             {
                 Message.Show(MessageFunction.Autoclose, "Success", "对方在线,正在发送好友请求");
                 ChatSend cs = new ChatSend(FriendList.id2ip[FriendID.Text].ToString(), FriendID.Text, this);
                 cs.ConnectFriend();
                 cs.SendMessage(ChatMessageType.REQUEST, myself.IP + "\t" + myself.Name + "\t" + myself.Header);
             }
             else
             {
                 Message.Show(MessageFunction.Autoclose, "Error", "对方离线或不存在该用户,无法接收到好友请求");
             }
             //因为人家还不是你的好友,所以删掉咯
             FriendList.id2friends.Remove(FriendID.Text);
             FriendList.id2ip.Remove(FriendID.Text);
             f.memberData.Remove(f.temp[f.count]);
             f.count1--;
         }
     }
 }
Пример #2
0
        public ChatBox(Main main, string ip, string id)
        {
            InitializeComponent();


            //初始化参数
            this.ip = ip;
            m       = main;
            eb.cb   = this;
            cs      = new ChatSend(ip, id, main);

            //绑定图片
            fileimage.Source    = Function.img2source(Properties.Resources.file);
            emotionimage.Source = Function.img2source(Properties.Resources.emotion);
            picimage.Source     = Function.img2source(Properties.Resources.pic);
            shakeimage.Source   = Function.img2source(Properties.Resources.shake);
            cutimage.Source     = Function.img2source(Properties.Resources.cut);
            eb.Visibility       = Visibility.Collapsed;
            eb.IsEnabled        = false;

            ChatRecord.FocusVisualStyle  = null;
            ChatRecord.SelectionBrush    = null;
            ChatRecord.IsDocumentEnabled = true;
            cs.ConnectFriend();

            screenCaputre.ScreenCaputred         += OnScreenCaputred;
            screenCaputre.ScreenCaputreCancelled += OnScreenCaputreCancelled;
            cuttimer.Interval = TimeSpan.FromMilliseconds(10);
            cuttimer.Tick    += new EventHandler(cuttimer_Tick);
        }
Пример #3
0
        public FileRecv(string path)
        {
            IP_Address  = IPAddress.Parse(ChatSend.GetLocalIP());
            socketWatch = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
            //将获取的ip地址和端口号绑定到网络节点endpoint上
            IPEndPoint endpoint = new IPEndPoint(IP_Address, Port);

            if (!FileRecv.ServiceFlag)
            {
                socketWatch.Bind(endpoint);
                socketWatch.Listen(20);
                FileRecv.ServiceFlag = true;
            }

            FilePath = path;
            this.fp  = null;

            Finish = false;
        }
Пример #4
0
        public Main(string ID)
        {
            InitializeComponent();

            //初始化参数
            myID           = ID;
            myself.Header  = "7";
            myself.ID      = myID;
            myself.IP      = ChatSend.GetLocalIP();
            myself.Name    = "User1";
            myself.State   = "Online";
            timer.Interval = TimeSpan.FromSeconds(5);
            timer.Tick    += timer_tick;

            //绑定事件,起始操作
            UserInfo.userclick += OnClick;
            cl.chatmessage     += OnCall;
            cl.main             = this;
            cl.StartListening();

            f = new FriendList(this);
            //刷新好友列表,新线程防止界面卡住
            refreshfriendlist();

            myselfitem.Visibility = Visibility.Collapsed;
            myselfgrid.Visibility = Visibility.Collapsed;

            //初始化UI
            //grid.Background = new ImageBrush(Function.img2source(Properties.Resources.bg));
            bgimage.Source = Function.img2source(Properties.Resources.bg);
            label.Content  = "";
            Chat.Source    = Function.img2source(Properties.Resources.Chat);
            Add.Source     = Function.img2source(Properties.Resources.Add);
            Myself.Source  = Function.img2source(Properties.Resources.myself);
            ImageBrush b = new ImageBrush(Function.img2source(Properties.Resources.welcomeimage));

            welcomegrid.Background = b;
            flipview.IsEnabled     = false;

            timer.Start();

            icon();
        }
Пример #5
0
        //关键部分,处理对方发来的信息
        private void OnCall(object sender, ChatListenEventArgs e)
        {
            //聊天类型
            if (e.type == ChatMessageType.CHAT)
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    //如果那个窗口曾经打开过,或者打开着
                    if (id2window.Contains(e.from))
                    {
                        //显示信息
                        ChatBox temp = id2window[e.from] as ChatBox;
                        temp.Show_Time();
                        temp.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Left, e.message, 0, e.header));
                        temp.ChatRecord.ScrollToEnd();

                        //增加提示
                        if (tabControl.SelectedIndex != Convert.ToInt32(id2number[e.from]))
                        {
                            (id2tabitem[e.from] as TabItem).Background = MyColor.White;
                        }
                        //不管是不是被关了,都让其可见
                        (id2tabitem[e.from] as TabItem).Visibility = Visibility.Visible;
                        ((id2tabitem[e.from] as TabItem).Content as Grid).Visibility = Visibility.Visible;
                    }
                    //有这么个人但是从没开过窗口
                    else if (FriendList.id2ip.Contains(e.from))
                    {
                        try
                        {
                            //先开这个窗口,但是不把焦点移过去
                            TabItem temp = new TabItem();
                            Grid grid    = new Grid();
                            grid.Margin  = new Thickness(0, -1, 0, 1);
                            ChatBox cb   = new ChatBox(this, FriendList.id2ip[e.from] as string, e.from);
                            grid.Children.Add(cb);
                            grid.Visibility = Visibility.Visible;
                            grid.Background = MyColor.Trans;
                            temp.Content    = grid;
                            temp.Header     = (FriendList.id2friends[e.from] as Friends).Name;
                            temp.Background = MyColor.White;

                            cb.Show_Time();
                            cb.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Left, e.message, 0, e.header));

                            number++;

                            tabControl.Items.Add(temp);
                            id2number.Add(e.from, number);
                            id2window.Add(e.from, cb);
                            id2tabitem.Add(e.from, temp);
                        }
                        catch (Exception ex)
                        {
                        }
                    }

                    //添加消息预览
                    UserInfo temp2       = id2userinfo[e.from] as UserInfo;
                    temp2.label1.Content = e.message.Substring(0, min(10, e.message.Length)) + "...";
                }));
            }
            //加好友类型
            else if (e.type == ChatMessageType.REQUEST)
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    //如果点了同意
                    if (Message.Show(MessageFunction.YesNo, "Request", e.from + "申请加您为好友,是否接受?") == MessageResult.Yes)
                    {
                        string[] tokens = e.message.Split(new char[] { '\t' });
                        //发送确认信息
                        ChatSend cs = new ChatSend(tokens[0], e.from, this);
                        cs.ConnectFriend();
                        cs.SendMessage(ChatMessageType.HELLO, myself.IP + "\t" + myself.Name + "\t" + myself.Header);
                        if (!FriendList.id2friends.Contains(e.from))
                        {
                            //把对方加到好友列表并刷新
                            f.WriteFriendsList(e.from, tokens[1], tokens[2]);
                            refreshfriendlist();
                        }
                    }
                }));
            }
            //对方同意加好友类型
            else if (e.type == ChatMessageType.HELLO)
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    Message.Show(MessageFunction.Autoclose, "Success", "对方已经接受你的好友请求");
                    string[] tokens = e.message.Split(new char[] { '\t' });
                    f.WriteFriendsList(e.from, tokens[1], tokens[2]);
                    refreshfriendlist();
                }));
            }
            //对方请求发文件
            else if (e.type == ChatMessageType.FILE)
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    string[] tokens   = e.message.Split(new char[] { '~' });
                    string filePath   = tokens[1];
                    string fileName   = tokens[0];
                    string fileLength = tokens[2];
                    if (Message.Show(MessageFunction.YesNo, "收到文件请求\n",
                                     "收到文件" + tokens[0] + "\n大小为" + tokens[2] + "MB\n是否接收?") == MessageResult.Yes)
                    {
                        System.Windows.Forms.SaveFileDialog sfd = new System.Windows.Forms.SaveFileDialog();
                        //设置默认文件名
                        sfd.FileName = fileName;
                        if (sfd.ShowDialog() == System.Windows.Forms.DialogResult.OK)
                        {
                            string FilePath = sfd.FileName;
                            if (!string.IsNullOrEmpty(FilePath))
                            {
                                fpr = new FileProgress(fileName, 0, e.header);

                                FileRecv fr = new FileRecv(FilePath, fpr);
                                fr.main     = this;
                                fr.Start();
                            }
                            //如果已经打开着窗口
                            if (id2window.Contains(e.from))
                            {
                                ChatBox temp = id2window[e.from] as ChatBox;

                                Thread.Sleep(500);

                                temp.Show_Time();

                                temp.SendMessage(ChatMessageType.FILEREQUEST, tokens[1] + "~" + tokens[0]);

                                temp.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Left, fpr));
                                temp.ChatRecord.ScrollToEnd();


                                (id2tabitem[e.from] as TabItem).Visibility = Visibility.Visible;
                                ((id2tabitem[e.from] as TabItem).Content as Grid).Visibility = Visibility.Visible;

                                if (tabControl.SelectedIndex != Convert.ToInt32(id2number[e.from]))
                                {
                                    (id2tabitem[e.from] as TabItem).Background = MyColor.White;
                                }
                            }
                            //如果在好友列表里但没开过窗口
                            else if (FriendList.id2ip.Contains(e.from))
                            {
                                TabItem temp = new TabItem();
                                Grid grid    = new Grid();
                                grid.Margin  = new Thickness(0, -1, 0, 1);
                                ChatBox cb   = new ChatBox(this, FriendList.id2ip[e.from] as string, e.from);

                                Thread.Sleep(500);
                                cb.Show_Time();
                                cb.SendMessage(ChatMessageType.FILEREQUEST, tokens[1] + "~" + tokens[0]);
                                cb.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Left, fpr));
                                cb.ChatRecord.ScrollToEnd();

                                grid.Children.Add(cb);
                                grid.Visibility = Visibility.Visible;
                                grid.Background = MyColor.Trans;
                                temp.Content    = grid;
                                temp.Header     = (FriendList.id2friends[e.from] as Friends).Name;
                                temp.Background = MyColor.White;

                                number++;

                                tabControl.Items.Add(temp);
                                id2number.Add(e.from, number);
                                id2window.Add(e.from, cb);
                                id2tabitem.Add(e.from, temp);
                            }
                        }
                    }
                }));
            }
            //发来图片
            else if (e.type == ChatMessageType.PICTURE)
            {
                string[] tokens   = e.message.Split(new char[] { '~' });
                string   fileName = tokens[0];

                string FilePath = "./ChatImage/" + fileName;

                FileRecv fr = new FileRecv();
                if (!string.IsNullOrEmpty(FilePath))
                {
                    fr      = new FileRecv(FilePath);
                    fr.main = this;
                    fr.Start();
                }

                //等待图片传送完毕
                while (!fr.Finish)
                {
                    Thread.Sleep(1000);
                }

                this.Dispatcher.Invoke(new Action(delegate
                {
                    //如果已经打开着窗口
                    if (id2window.Contains(e.from))
                    {
                        ChatBox temp = id2window[e.from] as ChatBox;
                        temp.Show_Time();
                        temp.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Left, new BitmapImage(new Uri(FilePath, UriKind.Relative)), 0, e.header));
                        temp.ChatRecord.ScrollToEnd();
                        (id2tabitem[e.from] as TabItem).Visibility = Visibility.Visible;
                        ((id2tabitem[e.from] as TabItem).Content as Grid).Visibility = Visibility.Visible;

                        if (tabControl.SelectedIndex != Convert.ToInt32(id2number[e.from]))
                        {
                            (id2tabitem[e.from] as TabItem).Background = MyColor.White;
                        }
                    }
                    //如果在好友列表里但没开过窗口
                    else if (FriendList.id2ip.Contains(e.from))
                    {
                        TabItem temp = new TabItem();
                        Grid grid    = new Grid();
                        grid.Margin  = new Thickness(0, -1, 0, 1);
                        ChatBox cb   = new ChatBox(this, FriendList.id2ip[e.from] as string, e.from);


                        cb.Show_Time();
                        cb.SendMessage(ChatMessageType.FILEREQUEST, tokens[1] + "~" + tokens[0]);

                        cb.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Left, new BitmapImage(new Uri(FilePath)), 0, e.header));
                        cb.ChatRecord.ScrollToEnd();

                        grid.Children.Add(cb);
                        grid.Visibility = Visibility.Visible;
                        grid.Background = MyColor.Trans;
                        temp.Content    = grid;
                        temp.Header     = (FriendList.id2friends[e.from] as Friends).Name;
                        temp.Background = MyColor.White;

                        number++;

                        tabControl.Items.Add(temp);
                        id2number.Add(e.from, number);
                        id2window.Add(e.from, cb);
                        id2tabitem.Add(e.from, temp);
                    }
                }));
            }
            //如果是对方同意发送文件
            else if (e.type == ChatMessageType.FILEREQUEST)
            {
                string[] tokens   = e.message.Split(new char[] { '~' });
                string   filePath = tokens[0];
                string   fileName = tokens[1];
                this.Dispatcher.Invoke(new Action(delegate
                {
                    fp = new FileProgress(fileName, 1, myheader);


                    if (id2window.Contains(e.from))
                    {
                        ChatBox temp = id2window[e.from] as ChatBox;

                        temp.Show_Time();
                        temp.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Right, fp));
                        temp.ChatRecord.ScrollToEnd();

                        (id2tabitem[e.from] as TabItem).Visibility = Visibility.Visible;
                        ((id2tabitem[e.from] as TabItem).Content as Grid).Visibility = Visibility.Visible;
                        if (tabControl.SelectedIndex != Convert.ToInt32(id2number[e.from]))
                        {
                            (id2tabitem[e.from] as TabItem).Background = MyColor.White;
                        }

                        UserInfo temp2 = id2userinfo[e.from] as UserInfo;

                        temp2.label1.Content = e.message.Substring(0, min(10, e.message.Length)) + "...";
                    }
                    else if (FriendList.id2ip.Contains(e.from))
                    {
                        TabItem temp = new TabItem();
                        Grid grid    = new Grid();
                        grid.Margin  = new Thickness(0, -1, 0, 1);
                        ChatBox cb   = new ChatBox(this, FriendList.id2ip[e.from] as string, e.from);
                        grid.Children.Add(cb);
                        grid.Visibility = Visibility.Visible;
                        grid.Background = MyColor.Trans;
                        temp.Content    = grid;
                        temp.Header     = (FriendList.id2friends[e.from] as Friends).Name;
                        temp.Background = MyColor.White;

                        cb.Show_Time();
                        cb.ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Right, fp));
                        cb.ChatRecord.ScrollToEnd();

                        number++;

                        tabControl.Items.Add(temp);
                        id2number.Add(e.from, number);
                        id2window.Add(e.from, cb);
                        id2tabitem.Add(e.from, temp);

                        UserInfo temp2       = id2userinfo[e.from] as UserInfo;
                        temp2.label1.Content = e.message.Substring(0, min(10, e.message.Length)) + "...";
                    }


                    FileSend fs = new FileSend(FriendList.id2ip[e.from] as string, filePath, fileName, fp);
                    fs.SendFile();
                }));
            }
            else if (e.type == ChatMessageType.SHAKE)
            {
                this.Dispatcher.Invoke(new Action(delegate
                {
                    int i, j, k;             //定义三个变量
                    for (i = 1; i <= 3; i++) //循环次数
                    {
                        for (j = 1; j <= 10; j++)
                        {
                            this.Top  += 1;
                            this.Left += 1;
                            Thread.Sleep(5);
                        }
                        for (k = 1; k <= 10; k++)
                        {
                            this.Top  -= 1;
                            this.Left -= 1;
                            Thread.Sleep(5);
                        }
                    }
                }));
            }
        }