public void writeFile(string chatname, string filename, byte[] bytes) { try { FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog(); folderBrowserDialog.Description = "选择文件保存的位置"; if (folderBrowserDialog.ShowDialog() == DialogResult.OK) { string filepath = folderBrowserDialog.SelectedPath + @"\" + filename; File.WriteAllBytes(filepath, bytes); writeMsg(chatname, "文件已保存到" + filepath); string text = string.Format( "{0} 在 {1:MM-dd H:mm:ss} 成功接收了文件{2}", getMyFullname(), DateTime.Now, filename ); ChatLink cl = ls.getChatLink(chatname); cl.sendMsg(common.type_str_text, text); } } catch (System.Exception ex) { writeError(ex); } }
public void addFriend(string nickname, List <string> users) { string chatname = common.generateIdentifier(common.name_header_length); try { ChatLink cl = ls.register(chatname); cl.addUser(sl.getUserName()); foreach (string user in users) { cl.addUser(user); } cl.start(); if (users.Count == 1) { if (nickname != "") { cl.Nickname = nickname; } } else { cl.sendMsg(common.type_str_set_groupname, nickname); } writeInstantMsg("已添加聊天:" + cl.Nickname); } catch (System.Exception ex) { writeError(ex); } }
private void buttonDelFriend_Click(object sender, EventArgs e) { if (listBoxFriend.SelectedIndex > -1) { ChatLink cl = (ChatLink)listBoxFriend.Items[listBoxFriend.SelectedIndex]; ls.unregister(cl.getChatname()); writeInstantMsg("已删除聊天:" + cl.Nickname); } }
private void buttonSendFile_Click(object sender, EventArgs e) { if (listBoxFriend.SelectedIndex > -1) { string localFilePath = String.Empty; OpenFileDialog openFileDialog = new OpenFileDialog(); openFileDialog.Filter = "所有文件(*.*)|*.*"; openFileDialog.FilterIndex = 1; openFileDialog.Title = "请选择要传的文件"; openFileDialog.RestoreDirectory = true; if (openFileDialog.ShowDialog() == DialogResult.OK) { localFilePath = openFileDialog.FileName.ToString(); string filename = Path.GetFileName(localFilePath); try { byte[] bytes = File.ReadAllBytes(localFilePath); ChatLink cl = (ChatLink)listBoxFriend.Items[listBoxFriend.SelectedIndex]; string text = string.Format( "{0} 在 {1:MM-dd H:mm:ss} 分享了文件{2}", getMyFullname(), DateTime.Now, filename ); using (BackgroundWorker bw = new BackgroundWorker()) { bw.DoWork += (object o, DoWorkEventArgs ea) => { cl.sendMsg(common.type_str_text, text); cl.sendMsg(common.type_str_fileowner, sl.getUserName()); cl.sendMsg(common.type_str_filename, filename); cl.sendMsg(common.type_str_file, bytes); }; bw.RunWorkerCompleted += (object o, RunWorkerCompletedEventArgs ea) => { ; }; bw.RunWorkerAsync(); } } catch (System.Exception ex) { writeError(ex); } } } }
private void buttonChangeName_Click(object sender, EventArgs e) { if (listBoxFriend.SelectedIndex > -1) { ChatLink cl = (ChatLink)listBoxFriend.Items[listBoxFriend.SelectedIndex]; string newname = Interaction.InputBox("请输入新的备注", "计网大作业", "滑稽"); if (cl.groupNumber == 2) { cl.Nickname = newname; listBoxFriend.Refresh(); } else { cl.sendMsg(common.type_str_set_groupname, newname); } } }
private void listBoxFriend_DrawItem(object sender, DrawItemEventArgs e) { e.DrawBackground(); if (e.Index >= 0) { ChatLink cl = (ChatLink)listBoxFriend.Items[e.Index]; string chatname = cl.getChatname(); var m = getMsgHistory(chatname); if (m.unread > 0) { string showText = string.Format("【{0}未读】{1}", m.unread, cl.Nickname); e.Graphics.DrawString(showText, new Font("Arial", 8, FontStyle.Bold), Brushes.Black, e.Bounds); } else { e.Graphics.DrawString(cl.Nickname, new Font("Arial", 8, FontStyle.Regular), Brushes.Black, e.Bounds); } } e.DrawFocusRectangle(); }
private void buttonSend_Click(object sender, EventArgs e) { if (listBoxFriend.SelectedIndex > -1) { string text = string.Format( "{0} {1:MM-dd H:mm:ss}{2}", getMyFullname(), DateTime.Now, Environment.NewLine ); text += textBoxMsgSend.Text; try { ChatLink cl = (ChatLink)listBoxFriend.Items[listBoxFriend.SelectedIndex]; cl.sendMsg(common.type_str_text, text); textBoxMsgSend.Text = string.Empty; } catch (System.Exception ex) { writeError(ex); } } }