//关键部分,处理点击用户列表的信息 private void OnClick(object sender, UserClickEventArgs e) { if ((sender as UserInfo).friend == e.friend) { welcome.Visibility = Visibility.Collapsed; welcomegrid.Visibility = Visibility.Collapsed; Friends mySelectedElement = e.friend; string selectedid = mySelectedElement.ID; string selectedip = mySelectedElement.IP; if (mySelectedElement.State == "Online") { if (selectedid != null) { if (id2number.Contains(selectedid)) { tabControl.SelectedIndex = Convert.ToInt32(id2number[selectedid]); (id2tabitem[selectedid] as TabItem).Visibility = Visibility.Visible; ((id2tabitem[selectedid] as TabItem).Content as Grid).Visibility = Visibility.Visible; } else { TabItem temp = new TabItem(); Grid grid = new Grid(); grid.Margin = new Thickness(0, -1, 0, 1); ChatBox cb = new ChatBox(this, selectedip, selectedid); grid.Children.Add(cb); grid.Visibility = Visibility.Visible; temp.Content = grid; temp.Header = mySelectedElement.Name; number++; tabControl.Items.Add(temp); tabControl.SelectedIndex = number; id2number.Add(selectedid, number); id2window.Add(selectedid, cb); id2tabitem.Add(selectedid, temp); //, mid2userinfo.Add(selectedid, e.thisuser); } } } else { Message.Show(MessageFunction.Autoclose, "Error", "对方不在线\n尚不提供离线消息服务。"); } } }
//关键部分,处理对方发来的信息 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); } } })); } }
public Emoji(string emo, ChatBox cb) { InitializeComponent(); emojistr = "[emoji]emoji-E" + emo + "[emoji]"; this.cb = cb; //初始化图像 switch (emo) { case "00E": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E00E); break; case "11B": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E11B); break; case "40A": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E40A); break; case "40B": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E40B); break; case "40C": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E40C); break; case "40D": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E40D); break; case "40E": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E40E); break; case "40F": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E40F); break; case "056": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E056); break; case "057": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E057); break; case "058": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E058); break; case "059": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E059); break; case "105": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E105); break; case "106": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E106); break; case "107": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E107); break; case "108": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E108); break; case "401": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E401); break; case "402": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E402); break; case "403": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E403); break; case "404": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E404); break; case "405": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E405); break; case "406": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E406); break; case "407": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E407); break; case "408": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E408); break; case "409": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E409); break; case "410": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E410); break; case "411": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E411); break; case "412": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E412); break; case "413": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E413); break; case "414": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E414); break; case "415": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E415); break; case "416": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E416); break; case "417": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E417); break; case "418": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E418); break; case "419": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E419); break; case "420": EMOJI.Source = Function.img2source(Properties.Resources.emoji_E420); break; } }