private void friend_MouseDoubleClick(object sender, MouseButtonEventArgs e) { TreeViewItem item = (TreeViewItem)sender; object[] obj = (object[])(item.DataContext); String state = (String)obj[5]; if (obj[5].Equals("False")) { obj[5] = (object)"True"; getFriendItemById[(String)obj[0]].DataContext = (object)obj; ChatDialog chatDialog = new ChatDialog((String)obj[2], this, (String)obj[0]); getChatItemById.Add((String)obj[0], chatDialog); chatDialog.Show(); chatDialog.Activate(); } else { getChatItemById[(String)obj[0]].Activate(); } }
public void TranslateMessage(String key, String[] data) { this.Dispatcher.BeginInvoke((Action) delegate() { try { if ("con".Equals(key)) { if (data[0].Equals("Y")) { this.Show(); parent.loggedIn = true; parent.Close(); } else if (data[0].Equals("D")) { MessageBoxResult mbResult = MessageBox.Show("您开没有对该应用进行授权,是否跳转到主站进行授权操作?", "授权验证请求", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (mbResult == MessageBoxResult.OK) { string ret = string.Format("{0}?client_id={1}&response_type=code&redirect_uri={2}", "http://icpc.njust.edu.cn/OAuth/Authorize", "b681a5074007b0a8c2e8fda84ec7be80017c2f04", "http://202.119.81.70:90/OAuthRedirect.aspx"); System.Diagnostics.Process.Start(ret); } } else { MessageBox.Show("对不起,用户账号或者密码不正确,登录不成功!"); } this.Dispatcher.BeginInvoke((Action) delegate() { parent.msg.Content = ""; }); } //个人信息 #region else if (key.Equals("nick"))//昵称 { NickName.Text = data[0]; } else if (key.Equals("gender"))//性别 { try { if (data[0] == "True") { imale = new Image(); //Gender = this.maleRTB; System.Windows.Documents.FlowDocument doc = Gender.Document; doc.Blocks.Clear(); byte[] image_full_original_data = Convert.FromBase64String(male); BitmapImage bImg = new BitmapImage(); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(image_full_original_data); bImg.EndInit(); imale.Source = bImg; new InlineUIContainer(this.imale, Gender.Selection.End); Gender.DataContext = "♂"; } else { ifemale = new Image(); //Gender = this.femaleRTB; System.Windows.Documents.FlowDocument doc = Gender.Document; doc.Blocks.Clear(); byte[] image_full_original_data = Convert.FromBase64String(female); BitmapImage bImg = new BitmapImage(); bImg.BeginInit(); bImg.StreamSource = new MemoryStream(image_full_original_data); bImg.EndInit(); ifemale.Source = bImg; new InlineUIContainer(this.ifemale, Gender.Selection.End); Gender.DataContext = "♀"; } } catch (Exception ee) { MessageBox.Show(ee.ToString()); } } else if (key.Equals("error"))//报错 { MessageBox.Show(data[0]); } #endregion //分组管理 #region else if (key.Equals("crtgroup"))//创建分组 { TreeViewItem tvItem = new TreeViewItem(); tvItem.Header = data[1]; tvItem.DataContext = data[0]; tvItem.MouseRightButtonDown += tb_MouseRightButtonDown; tvItem.MouseRightButtonUp += tb_MouseRightButtonUp; friendsList.Items.Add(tvItem); getGroupItemById.Add(data[0], tvItem); } else if (key.Equals("chggroup"))//修改分组名称 { getGroupItemById[data[0]].Header = data[1]; } else if (key.Equals("delroup"))//删除分组 { TreeViewItem tvItem = getGroupItemById[data[0]]; friendsList.Items.Remove(tvItem); getGroupItemById.Remove(data[0]); } #endregion //好友管理 #region else if (key.Equals("crtfrd"))//添加好友 { TreeViewItem friendItem = new TreeViewItem(); TreeViewItem grpItem = getGroupItemById[data[1]]; String friendInfo = ""; friendInfo += data[2]; friendInfo += "("; friendInfo += data[0]; friendInfo += ")"; String gender; if (data[3] == "True") { friendInfo += " ♂"; gender = " ♂"; } else { friendInfo += " ♀"; gender = " ♀"; } friendItem.Header = friendInfo; object obj = new object[] { (object)data[0], (object)data[1], (object)data[2], (object)gender, (object)" 离线", (object)"False" };//ID,组ID,备注,性别,是否在线,是否有窗口 friendItem.DataContext = obj; friendItem.MouseRightButtonDown += friend_MouseRightButtonDown; friendItem.MouseRightButtonUp += friend_MouseRightButtonUp; friendItem.MouseDoubleClick += friend_MouseDoubleClick; grpItem.Items.Add(friendItem); getFriendItemById.Add(data[0], friendItem); } else if (key.Equals("chgfrd"))//好友改名 { object[] obj = (object[])getFriendItemById[data[0]].DataContext; String[] datas = new String[6]; for (int i = 0; i < obj.Length; i++) { datas[i] = (String)obj[i]; } getFriendItemById[data[0]].Header = data[1] + "(" + datas[0] + ")" + datas[3] + datas[4]; obj[2] = (object)data[1]; getFriendItemById[data[0]].DataContext = (object)obj; if (getChatItemById.ContainsKey(data[0])) { getChatItemById[data[0]].Title = "与 " + data[1] + " 聊天中"; } } else if (key.Equals("delfrd"))//删除好友 { TreeViewItem tvItem = getFriendItemById[data[0]]; object[] obj = (object[])getFriendItemById[data[0]].DataContext; String[] info = new String[2]; info[0] = (String)obj[0]; info[1] = (String)obj[1]; getGroupItemById[info[1]].Items.Remove(tvItem); if (getChatItemById.ContainsKey(data[0])) { MessageBox.Show("由于删除好友,聊天窗口即将关闭!"); getChatItemById[data[0]].Close(); getChatItemById.Remove(data[0]); } getFriendItemById.Remove(data[0]); } else if (key.Equals("frdstt"))//提示好友是否在线 { object[] obj = (object[])getFriendItemById[data[0]].DataContext; if (data[1] == "True") { obj[4] = " 在线"; } else { obj[4] = " 离线"; } getFriendItemById[data[0]].Header = (String)obj[2] + "(" + (String)obj[0] + ")" + (String)obj[3] + (String)obj[4]; getFriendItemById[data[0]].DataContext = (object)obj; } else if (key.Equals("cnffrd"))//添加好友确认 { String msg = ""; msg += "用户 "; msg += data[1]; msg += " 请求添加您为好友,是否同意?"; MessageBoxResult mbResult = MessageBox.Show(msg, "", MessageBoxButton.OKCancel, MessageBoxImage.Question); if (mbResult == MessageBoxResult.OK) { new AcptCnf(this, data[0], data[2], data[3]).ShowDialog(); } } #endregion //接受消息和图片 #region else if (key.Equals("recvmsg"))//接受消息 { SoundPlayer player = new SoundPlayer((String)AppDomain.CurrentDomain.BaseDirectory + "msg.wav"); player.Play(); RichTextBox stackrtb = new RichTextBox(); stackrtb.SelectAll(); stackrtb.Paste(); object[] obj; try { obj = (object[])getFriendItemById[data[0]].DataContext; } catch { MessageBox.Show("陌生人 " + data[0] + " 于 " + data[2] + " 发来消息:" + data[1]); return; } if (((String)obj[5]).Equals("False")) { obj[5] = (object)"True"; getFriendItemById[(String)obj[0]].DataContext = (object)obj; ChatDialog chatDialog = new ChatDialog((String)obj[2], this, (String)obj[0]); getChatItemById.Add((String)obj[0], chatDialog); chatDialog.Show(); chatDialog.Activate(); } ChatDialog cDialog = getChatItemById[data[0]]; String head = (String)obj[2] + " " + data[2]; RichTextBox rtb = new RichTextBox(); RichTextBox rtb2 = cDialog.msgRecRichTextBox; rtb2.SelectAll(); rtb2.Copy(); cDialog.msgRecRichTextBox.SelectAll(); cDialog.msgRecRichTextBox.Paste(); rtb.AppendText(head); rtb.SelectAll(); rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); rtb.Copy(); cDialog.msgRecRichTextBox.Paste(); cDialog.msgRecRichTextBox.AppendText(data[1] + "\r\n"); stackrtb.SelectAll(); stackrtb.Copy(); } else if (key.Equals("recvimg"))//接收图片 { SoundPlayer player = new SoundPlayer((String)AppDomain.CurrentDomain.BaseDirectory + "msg.wav"); player.Play(); RichTextBox stackrtb = new RichTextBox(); stackrtb.SelectAll(); stackrtb.Paste(); object[] obj; try { obj = (object[])getFriendItemById[data[0]].DataContext; } catch { MessageBox.Show("陌生人 " + data[0] + " 发来图片,由于您未加其为好友,内容不予显示"); return; } if (((String)obj[5]).Equals("False")) { obj[5] = (object)"True"; getFriendItemById[(String)obj[0]].DataContext = (object)obj; ChatDialog chatDialog = new ChatDialog((String)obj[2], this, (String)obj[0]); getChatItemById.Add((String)obj[0], chatDialog); chatDialog.Show(); chatDialog.Activate(); } ChatDialog cDialog = getChatItemById[data[0]]; String head = (String)obj[2] + " " + data[2]; RichTextBox rtb = new RichTextBox(); RichTextBox rtb2 = cDialog.msgRecRichTextBox; rtb2.SelectAll(); rtb2.Copy(); cDialog.msgRecRichTextBox.SelectAll(); cDialog.msgRecRichTextBox.Paste(); rtb.AppendText(head); rtb.SelectAll(); rtb.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); rtb.Copy(); cDialog.msgRecRichTextBox.Paste(); Image img2 = new Image(); BitmapImage bImg2 = new BitmapImage(); img2.IsEnabled = true; byte[] image_full_original_data = Convert.FromBase64String(data[1]); bImg2.BeginInit(); bImg2.StreamSource = new MemoryStream(image_full_original_data); bImg2.EndInit(); img2.Source = bImg2; new InlineUIContainer(img2, cDialog.msgRecRichTextBox.Selection.End); //插入图片到选定位置 stackrtb.SelectAll(); stackrtb.Copy(); } else if (key.Equals("imgack")) { getChatItemById[data[0]].SendPicButton.IsEnabled = true; } #endregion //离线接收文件 #region else if (key.Equals("cnffsnd"))//确认是否接收 { object[] obj; try { obj = (object[])getFriendItemById[data[0]].DataContext; } catch { MessageBox.Show("陌生人 " + data[0] + " 请求向您发送文件,由于您未加其为好友,服务器代为拒绝了此文件。"); SendMessage("rjctget", new String[] { data[2], data[0], data[1] }); return; } String nick = (String)obj[2]; if (MessageBox.Show("好友 " + nick + " 请求向您发送文件 " + data[1] + " ,是否接收?", "消息", MessageBoxButton.YesNo, MessageBoxImage.Question).Equals(MessageBoxResult.Yes)) { //接收 SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "All Files (*.*)|*.*"; sfd.DefaultExt = "*" + data[1].Substring(data[1].LastIndexOf('.')); if (sfd.ShowDialog() == true) { getSaveFileUrlOffItemById.Add(data[2], sfd.SafeFileName); getSaveFileOffItemById.Add(data[2], ""); } SendMessage("accptget", new String[] { data[2], data[1] }); } else//拒绝 { SendMessage("rjctget", new String[] { data[2], data[0], data[1] }); } } else if (key.Equals("rjctdvr")) { object[] obj = (object[])getFriendItemById[data[0]].DataContext; String nick = (String)obj[2]; MessageBox.Show("对不起," + nick + " 拒绝接收您发送的文件 \"" + data[1] + "\"。"); } else if (key.Equals("prepget"))//服务器准备接受文件 { getToIdOffItemById.Add(data[2], data[0]); getUrlOffItemById.Add(data[2], data[1]); String datas = ShareFile.Read(data[1]); getProgressDataOffItemById.Add(data[2], datas); int parts = datas.Length / 200000; if (datas.Length % 200000 != 0) { parts++; } getProgressCntOffItemById.Add(data[2], parts); getProgressCntOffNumItemById.Add(data[2], 0); Progress pg = new Progress(); getProgressOffItemById.Add(data[2], pg); getProgressOffItemById[data[2]].label.Content = "正在发送 " + data[1]; //设置进度条节数parts getProgressOffItemById[data[2]].progressBar.Maximum = parts; pg.Show(); if (getProgressCntOffItemById[data[2]] - 1 == getProgressCntOffNumItemById[data[2]]) { SendMessage("sfileseg", new String[] { data[2], datas.Substring(200000 * getProgressCntOffNumItemById[data[2]]) }); } else { SendMessage("sfileseg", new String[] { data[2], datas.Substring(200000 * getProgressCntOffNumItemById[data[2]], 200000) }); } } else if (key.Equals("segcnt")) { int cntnum = getnum(data[1]); Progress pg = new Progress(); getProgressOffItemById.Add(data[0], pg); getProgressOffItemById[data[0]].label.Content = "正在接收文件" + getSaveFileUrlOffItemById[data[0]]; //设置进度条节数cntnum getProgressOffItemById[data[0]].progressBar.Maximum = cntnum; pg.Show(); } else if (key.Equals("segsucc")) { getProgressOffItemById[data[0]].progressBar.Value += 1.0; getProgressCntOffNumItemById[data[0]]++; if (getProgressCntOffNumItemById[data[0]] == getProgressCntOffItemById[data[0]]) { SendMessage("sfilefnsh", new String[] { data[0], getUrlOffItemById[data[0]], getToIdOffItemById[data[0]] }); MessageBox.Show("文件已成功发送至服务器!"); getProgressOffItemById[data[0]].Close(); getProgressOffItemById.Remove(data[0]); getProgressCntOffNumItemById.Remove(data[0]); getProgressCntOffItemById.Remove(data[0]); getProgressDataOffItemById.Remove(data[0]); getToIdOffItemById.Remove(data[0]); getUrlOffItemById.Remove(data[0]); } else { if (getProgressCntOffItemById[data[0]] - 1 == getProgressCntOffNumItemById[data[0]]) { SendMessage("sfileseg", new String[] { data[0], getProgressDataOffItemById[data[0]].Substring(200000 * getProgressCntOffNumItemById[data[0]]) }); } else { SendMessage("sfileseg", new String[] { data[0], getProgressDataOffItemById[data[0]].Substring(200000 * getProgressCntOffNumItemById[data[0]], 200000) }); } } } else if (key.Equals("tfileseg")) { getSaveFileOffItemById[data[0]] += data[1]; getProgressOffItemById[data[0]].progressBar.Value += 1.0; } else if (key.Equals("tfilefnsh")) { ShareFile.Write(getSaveFileUrlOffItemById[data[0]], getSaveFileOffItemById[data[0]]); MessageBox.Show("文件 " + data[1] + " 已成功接收!"); getProgressOffItemById[data[0]].Close(); getProgressOffItemById.Remove(data[0]); getSaveFileUrlOffItemById.Remove(data[0]); getSaveFileOffItemById.Remove(data[0]); } #endregion //在线接收文件 #region else if (key.Equals("prepgeton")) { getToIdOnItemById.Add(data[1], data[0]); getUrlOnItemById.Add(data[1], data[2]); Progress pg = new Progress(); getProgressOnItemById.Add(data[1], pg); String datas = ShareFile.Read(data[2]); getProgressDataOnItemById.Add(data[1], datas); int parts = datas.Length / 200000; if (datas.Length % 200000 != 0) { parts++; } getProgressOnItemById[data[1]].label.Content = "正在发送 " + data[2]; //设置进度条节数parts getProgressOnItemById[data[1]].progressBar.Maximum = parts; pg.Show(); getProgressCntOnItemById.Add(data[1], parts); getProgressCntOnNumItemById.Add(data[1], 0); SendMessage("ssegcnton", new String[] { data[0], data[1], parts.ToString() }); if (getProgressCntOnItemById[data[1]] - 1 == getProgressCntOnNumItemById[data[1]]) { SendMessage("sfilesegon", new String[] { data[1], datas.Substring(200000 * getProgressCntOnNumItemById[data[1]]), data[0] }); } else { SendMessage("sfilesegon", new String[] { data[1], datas.Substring(200000 * getProgressCntOnNumItemById[data[1]], 200000), data[0] }); } } else if (key.Equals("cnffsndon")) { object[] obj; try { obj = (object[])getFriendItemById[data[0]].DataContext; } catch { MessageBox.Show("陌生人 " + data[0] + " 请求向您发送文件,由于您未加其为好友,服务器代为拒绝了此文件。"); SendMessage("rjctgeton", new String[] { data[2], data[0], data[1] }); return; } String nick = (String)obj[2]; if (MessageBox.Show("好友 " + nick + " 请求向您发送文件 " + data[1] + " ,是否接收?", "消息", MessageBoxButton.YesNo, MessageBoxImage.Question).Equals(MessageBoxResult.Yes)) { //接收 SaveFileDialog sfd = new SaveFileDialog(); sfd.Filter = "All Files (*.*)|*.*"; if (sfd.ShowDialog() == true) { getSaveFileUrlOnItemById.Add(data[2], sfd.SafeFileName); getSaveFileOnItemById.Add(data[2], ""); } SendMessage("accptgeton", new String[] { data[0], data[2], data[1] }); } else//拒绝 { SendMessage("rjctgeton", new String[] { data[2], data[0], data[1] }); } } else if (key.Equals("rjctdvron")) { object[] obj = (object[])getFriendItemById[data[0]].DataContext; String nick = (String)obj[2]; MessageBox.Show("对不起," + nick + " 拒绝接收您发送的文件 \"" + data[1] + "\"。"); } else if (key.Equals("tsegcnton")) { int cntnum = getnum(data[1]); Progress pg = new Progress(); getProgressOnItemById.Add(data[0], pg); getProgressOnItemById[data[0]].label.Content = "正在接收文件" + getSaveFileUrlOnItemById[data[0]]; //设置进度条节数cntnum getProgressOnItemById[data[0]].progressBar.Maximum = cntnum; pg.Show(); } else if (key.Equals("segsuccon")) { getProgressOnItemById[data[0]].progressBar.Value += 1.0; getProgressCntOnNumItemById[data[0]]++; if (getProgressCntOnNumItemById[data[0]] == getProgressCntOnItemById[data[0]]) { SendMessage("sfilefnshon", new String[] { data[0], getUrlOnItemById[data[0]], getToIdOnItemById[data[0]] }); MessageBox.Show("向好友 " + getToIdOnItemById[data[0]] + " 发送的文件 " + getUrlOnItemById[data[0]] + "已成功发送!"); getProgressOnItemById[data[0]].Close(); getProgressOnItemById.Remove(data[0]); getProgressCntOnNumItemById.Remove(data[0]); getProgressCntOnItemById.Remove(data[0]); getProgressDataOnItemById.Remove(data[0]); getToIdOnItemById.Remove(data[0]); getUrlOnItemById.Remove(data[0]); } else { if (getProgressCntOnItemById[data[0]] - 1 == getProgressCntOnNumItemById[data[0]]) { SendMessage("sfilesegon", new String[] { data[0], getProgressDataOnItemById[data[0]].Substring(200000 * getProgressCntOnNumItemById[data[0]]), getToIdOnItemById[data[0]] }); } else { SendMessage("sfilesegon", new String[] { data[0], getProgressDataOnItemById[data[0]].Substring(200000 * getProgressCntOnNumItemById[data[0]], 200000), getToIdOnItemById[data[0]] }); } } } else if (key.Equals("tfilesegon")) { getSaveFileOnItemById[data[0]] += data[1]; getProgressOnItemById[data[0]].progressBar.Value += 1.0; } else if (key.Equals("tfilefnshon")) { ShareFile.Write(getSaveFileUrlOnItemById[data[0]], getSaveFileOnItemById[data[0]]); MessageBox.Show("文件 " + data[1] + " 已成功接收!"); getProgressOnItemById[data[0]].Close(); getProgressOnItemById.Remove(data[0]); getSaveFileUrlOnItemById.Remove(data[0]); getSaveFileOnItemById.Remove(data[0]); } else if (key.Equals("off")) { MessageBox.Show("当前账号在其他地区登录,您被迫下线!"); Environment.Exit(0); } #endregion //历史消息 #region else if (key.Equals("history")) { try { HistoryWindow hw; if (this.selfId.Equals(data[0])) { hw = getHistoryItemById[data[1]]; } else { hw = getHistoryItemById[data[0]]; } RichTextBox stackrtb = new RichTextBox(); stackrtb.SelectAll(); stackrtb.Paste(); RichTextBox rtb = hw.historyRecRichTextBox; rtb.SelectAll(); rtb.Copy(); hw.historyRecRichTextBox.SelectAll(); hw.historyRecRichTextBox.Paste(); RichTextBox rtb2 = new RichTextBox(); String head = ""; if (this.selfId.Equals(data[0])) { head += this.NickName.Text; } else { object[] obj = (object[])getFriendItemById[data[0]].DataContext; head += (String)obj[2]; } head += " "; head += data[4]; rtb2.AppendText(head); rtb2.SelectAll(); if (this.selfId.Equals(data[0])) { rtb2.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.DarkGreen); } else { rtb2.Selection.ApplyPropertyValue(TextElement.ForegroundProperty, Brushes.Blue); } rtb2.Copy(); hw.historyRecRichTextBox.Paste(); if (data[2].Equals("msg"))//文字记录 { hw.historyRecRichTextBox.AppendText(data[3] + "\r\n"); } else//图片记录 { Image img2 = new Image(); BitmapImage bImg2 = new BitmapImage(); img2.IsEnabled = true; byte[] image_full_original_data = Convert.FromBase64String(data[3]); bImg2.BeginInit(); bImg2.StreamSource = new MemoryStream(image_full_original_data); bImg2.EndInit(); img2.Source = bImg2; new InlineUIContainer(img2, hw.historyRecRichTextBox.Selection.End); //插入图片到选定位置 //hw.historyRecRichTextBox.AppendText("\r\n"); } stackrtb.SelectAll(); stackrtb.Copy(); } catch (Exception ee) { MessageBox.Show(ee.ToString()); } } #endregion else { MessageBox.Show(key); } } catch { } }); }