Ejemplo n.º 1
0
 /// <summary>
 /// Append Message to the Chatbox
 /// </summary>
 /// <param name="Message"></param>
 private void LogWrite(String Message, params object[] format)
 {
     //return;
     Message = String.Format(Message, format);
     if (!IsDisposed)
     {
         if (Chatbox.InvokeRequired)
         {
             Chatbox.Invoke(new Action(() =>
             {
                 if (ChatboxAutoscroll)
                 {
                     Chatbox.AppendText(Message);
                 }
                 else
                 {
                     BufferedChatlog += Message;
                 }
             }));
         }
         else
         {
             Chatbox.AppendText(Message);
         }
     }
 }
Ejemplo n.º 2
0
        private void textBox8_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter)
            {
                Chatbox.AppendText("[you] " + this.Sendbox.Text + Environment.NewLine);

                //send packet to the server
                PacketChat chat = new PacketChat(this.Sendbox.Text + Environment.NewLine, hostName, "monitor", id);
                this.serverConnection.WritePacket(chat);
                Console.WriteLine("Sent message");
                Sendbox.Clear();
            }
        }
Ejemplo n.º 3
0
        /******************添加收到的消息******************/
        public void AddMsg(string chatMsg)
        {
            Chatbox.SelectionAlignment = HorizontalAlignment.Left;
            Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular);

            Chatbox.SelectionFont  = font;
            Chatbox.SelectionColor = Color.Blue;
            string msg = System.DateTime.Now.ToLocalTime().ToString();

            msg = msg + "\n" + ">>> " + chatMsg + "\n";
            Chatbox.AppendText(msg);
            Chatbox.ScrollToCaret();
        }
Ejemplo n.º 4
0
        /******************发送消息******************/
        private void Sendbutton_Click(object sender, EventArgs e)
        {
            if (Sendchatbox.Text == "")
            {
                MessageBox.Show("请先填写消息");
            }
            else
            {
                try
                {
                    //向服务器确认好友是否在线,得到IP
                    NetworkStream stream  = toServer.GetStream();
                    string        sendMsg = "q" + friendID;
                    byte[]        sendByt = Encoding.ASCII.GetBytes(sendMsg);
                    stream.Write(sendByt, 0, sendByt.Length);
                    byte[] rcvByt    = new byte[1024];
                    int    rcvLength = stream.Read(rcvByt, 0, rcvByt.Length);
                    string rcvMsg    = Encoding.ASCII.GetString(rcvByt, 0, rcvLength);

                    if (rcvMsg == "n")
                    {
                        MessageBox.Show("好友已离线");
                    }
                    else
                    {
                        //发送该消息
                        sendMsg = "msg" + userID + Sendchatbox.Text;
                        sendByt = Encoding.Unicode.GetBytes(sendMsg);
                        if (sendByt.Length > 1024)
                        {
                            MessageBox.Show("一次输入请不要超过500个字");
                            return;
                        }
                        else
                        {
                            TcpClient tcptoFriend = new TcpClient();
                            tcptoFriend.Connect(rcvMsg, listenPort);
                            NetworkStream streamtoFriend = tcptoFriend.GetStream();
                            streamtoFriend.Write(sendByt, 0, sendByt.Length);
                            streamtoFriend.Close();
                            tcptoFriend.Close();
                            //stream.Close();   会报错

                            //向chatbox添加该消息
                            Chatbox.SelectionAlignment = HorizontalAlignment.Right;
                            Font font = new Font(FontFamily.GenericMonospace, 10, FontStyle.Regular);
                            Chatbox.SelectionFont  = font;
                            Chatbox.SelectionColor = Color.Green;
                            string msg = System.DateTime.Now.ToLocalTime().ToString();
                            msg = msg + "\n" + ">>> " + Sendchatbox.Text + "\n";
                            Chatbox.AppendText(msg);
                            Chatbox.ScrollToCaret();

                            Sendchatbox.Clear();
                            Sendchatbox.Focus();
                        }
                    }
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }