/// <summary> /// 将信息在聊天窗口显示 /// </summary> /// <param name="username"></param> /// <param name="msg"></param> /// <param name="remoteIPEndPoint"></param> public void showChatForm(string msgFrom, string msg, IPEndPoint remoteIPEndPoint, bool showReceiveFileForm = false) { //获取所有的打开窗体 FormCollection collections = Application.OpenForms; foreach (Form eachForm in collections) { //找出聊天窗口 if (eachForm.GetType().Equals(typeof(ChatForm))) { ChatForm chatForm = (ChatForm)eachForm; //如果对话框已经打开 if (msgFrom == chatForm.username) { AddTipsDelegate addTipsDelegate = new AddTipsDelegate(delegate(ChatForm form, string s) { form.addTips(s); //判断是否要打开传输文件界面 if (showReceiveFileForm) { ReceiveFileForm receiveFileForm = new ReceiveFileForm(); receiveFileForm.Show(); } }); Console.WriteLine("找到聊天窗口"); chatForm.Invoke(addTipsDelegate, chatForm, msg); return; } } } UserList userListForm = UserList.getInstance(); showChatFormDelegate showDelegate = new showChatFormDelegate(delegate(string username, IPEndPoint remoteIP) { //对话窗没有打开 ChatForm newChatForm = new ChatForm(); //设置窗体内的具体信息 newChatForm.Text = username; newChatForm.username = username; newChatForm.remoteIPEndPoint = remoteIP; newChatForm.Show(); newChatForm.addTips(msg); //判断是否要打开传输文件界面 if (showReceiveFileForm) { ReceiveFileForm receiveFileForm = new ReceiveFileForm(); receiveFileForm.Show(); } }); userListForm.Invoke(showDelegate, msgFrom, remoteIPEndPoint); }
/// <summary> /// 显示发送文件的窗口 /// </summary> /// <param name="msgFrom"></param> /// <param name="remoteIPEndPoint"></param> public void showSendFileForm(string msgFrom, IPEndPoint remoteIPEndPoint) { //获取所有的打开窗体 FormCollection collections = Application.OpenForms; foreach (Form eachForm in collections) { //找出聊天窗口 if (eachForm.GetType().Equals(typeof(ChatForm))) { ChatForm chatForm = (ChatForm)eachForm; //如果对话框已经打开 if (msgFrom == chatForm.username) { AddTipsDelegate addTipsDelegate = new AddTipsDelegate(delegate(ChatForm form, string s) { form.addTips(s); //显示发送文件的窗口 SendFileForm sendFileForm = new SendFileForm(); sendFileForm.remoteIPEndPoint = remoteIPEndPoint; sendFileForm.remoteUsername = msgFrom; sendFileForm.Show();; }); chatForm.Invoke(addTipsDelegate, chatForm, "你请求发送文件\n"); return; } } } UserList userListForm = UserList.getInstance(); showChatFormDelegate showDelegate = new showChatFormDelegate(delegate(string username, IPEndPoint remoteIP) { //对话窗没有打开 ChatForm newChatForm = new ChatForm(); //设置窗体内的具体信息 newChatForm.Text = username; newChatForm.username = username; newChatForm.remoteIPEndPoint = remoteIP; newChatForm.Show(); newChatForm.addTips("你请求发送文件"); //打开文件传输form //显示发送文件的窗口 SendFileForm sendFileForm = new SendFileForm(); sendFileForm.remoteIPEndPoint = remoteIPEndPoint; sendFileForm.remoteUsername = msgFrom; sendFileForm.Show(); }); userListForm.Invoke(showDelegate, msgFrom, remoteIPEndPoint); }