/// <summary> /// 将消息分发给对应的聊天窗口 /// </summary> public void DistributeToChatForm() { try { //如果存在,将消息传到这个窗口,如果不存在,创建一个新窗口 //找到当前已经打开的聊天窗口的句柄 string windowsName = "与" + Remoteuser + "聊天中"; IntPtr handle = Find_Windows(null, windowsName); if (handle != IntPtr.Zero) { //进程间通信时,必须对要发送的数据进行编码封装,直接发string类型,收到会出错 byte[] msg_bytes = Encoding.Default.GetBytes(Msgdetail); int msg_len = msg_bytes.Length; PreDefine.COPYDATASTRUCT msg_struct; msg_struct.dwData = (IntPtr)0;//该程序中此变量不被使用 msg_struct.lpData = Msgdetail; msg_struct.cbData = Encoding.Default.GetBytes(Msgdetail).Length + 1; Send_Messages(handle, PreDefine.WM_COPYDATA, 0, ref msg_struct); Flash_Windows(handle, true); } else { frmChat chatForm = new frmChat(Remoteip, Remoteuser, Remotehostname, Msgdetail); chatForm.Text = windowsName; chatForm.WindowState = System.Windows.Forms.FormWindowState.Minimized; //指定窗口状态为最小化状态 chatForm.ShowDialog(); //显示窗口,由于窗口状态为最小化,窗口只在任务栏上显示 } } catch (Exception ex) { MessageBox.Show(ex.Message, "消息分发出错"); } }
private void lvwUsers_ItemActivate(object sender, EventArgs e) { if (lvwUsers.SelectedItems[0].Index != -1) { string windowname = "与" + lvwUsers.SelectedItems[0].SubItems[0].Text + "聊天中"; IntPtr handle = FindChatWindow(null, windowname); if (handle != IntPtr.Zero) { Form frm = (Form)FromHandle(handle); frm.WindowState = FormWindowState.Normal; frm.Activate(); } else { string user = lvwUsers.SelectedItems[0].SubItems[1].Text; string ip = lvwUsers.SelectedItems[0].SubItems[2].Text; string hostname = lvwUsers.SelectedItems[0].SubItems[3].Text; frmChat chatform = new frmChat(ip, user, hostname, string.Empty); chatform.Text = "与" + user + "聊天中"; chatform.Show(); } } }