コード例 #1
0
        private void pic_Click(object sender, RoutedEventArgs e)
        {
            string filePath   = null;
            string fileName   = null;
            double fileLength = 0;

            System.Windows.Forms.OpenFileDialog dialog = new System.Windows.Forms.OpenFileDialog();
            dialog.Filter = "图像文件|*.jpg;*.jpeg;*.png;*.bmp";
            if (dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                fileName = dialog.SafeFileName;
                filePath = dialog.FileName;
            }
            if (!string.IsNullOrEmpty(filePath))
            {
                fileLength  = new FileInfo(filePath).Length;
                fileLength /= 1024;
                fileLength /= 1024;

                int failtime = 0;
                cs.SendMessage(ChatMessageType.PICTURE, fileName + "~" + filePath + "~" + fileLength.ToString("0.00"));
                //等待对方开启照片接收线程
                Thread.Sleep(1000);
                while (true)
                {
                    try
                    {
                        FileSend fs = new FileSend(ip, filePath, fileName);
                        fs.SendFile();
                        break;
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(200);
                        failtime++;
                        if (failtime == 10)
                        {
                            break;
                        }
                    }
                }
                Show_Time();
                ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Right, new BitmapImage(new Uri(filePath, UriKind.Relative)), 1, Main.myheader));
                ChatRecord.ScrollToEnd();
            }
        }
コード例 #2
0
        private void OnScreenCaputred(object sender, RisCaptureLib.ScreenCaputredEventArgs e)
        {
            //set last size
            lastSize = new Size(e.Bmp.Width, e.Bmp.Height);


            m.Dispatcher.Invoke(new Action(delegate
            {
                m.Show();
                System.Drawing.Bitmap bmpImg = e.BmpImg;
                string fileName = new Random().NextDouble().ToString() + ".jpg";
                string filePath = "./ChatImageSend/" + new Random().NextDouble().ToString() + ".jpg";
                bmpImg.Save(filePath);
                FileInfo f        = new FileInfo(filePath);
                double fileLength = f.Length;
                Show_Time();
                ChatRecord.Document.Blocks.Add(Function.ToShow(TextAlignment.Right, e.Bmp, 1, Main.myheader));
                ChatRecord.ScrollToEnd();
                cs.SendMessage(ChatMessageType.PICTURE, fileName + "~" + filePath + "~" + fileLength.ToString("0.00"));
                //等待对方开启照片接收线程
                Thread.Sleep(2000);
                while (true)
                {
                    try
                    {
                        FileSend fs = new FileSend(ip, filePath, fileName);
                        fs.SendFile();
                        break;
                    }
                    catch (Exception ex)
                    {
                        Thread.Sleep(200);
                    }
                }
            }));
        }
コード例 #3
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);
                        }
                    }
                }));
            }
        }