示例#1
0
        private void btnSend_Click(object sender, EventArgs e)
        {
            //We have a name selected
            if (contactIndex >= 0)
            {
                string message = decode(msgBox.Text);

                Contact contact = contactList[contactIndex];
                contact.saveCurrentMessage("");
                msgBox.Focus();//FOCUS NEEDS TO HAPPEN FIRST
                msgBox.Clear();

                if (packetSupport())
                {
                    //Send a Message Packet To Server, need to update friends convoBox and conversation string
                    ClientPacket  packet        = new ClientPacket(profile.getUsername(), profile.getIP(), message, contact.getPacket().getUsername(), contact.getPacket().getClientIP());
                    IFormatter    formatter     = new BinaryFormatter(); //the formatter that will serialize my object on my stream
                    NetworkStream networkStream = new NetworkStream(clientSocket);
                    formatter.Serialize(networkStream, packet);          //the serialization process
                    networkStream.Close();
                }
                else
                {
                    clientSocket.Send(Encoding.ASCII.GetBytes(message));
                }

                //Append new message to our convoBox
                //appendToConvoBox("You: " + message);

                //Save our msg to our conversation string
                //contact.setConversation(convoBox.Text);

                int            buffer = 4;
                List <Control> currentConversation = contact.getConversationUI();
                if (convoPanel.Controls.Count > 0)
                {
                    //Check if last message was yours
                    if (convoPanel.Controls[0].Controls[0].BackColor == Color.FromArgb(199, 237, 252))
                    {
                        //Check if your within time frame to merge messages
                        if (convoPanel.Controls[0].Controls[1].Text == DateTime.Now.ToString("h:mm tt"))
                        {
                            MyFixedRichTextBox.AppendText((MyFixedRichTextBox)convoPanel.Controls[0].Controls[0].Controls[0], "\r\n" + message, emoji);
                            convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum);
                            //Update new message to the conversationUI
                            contact.updateConversation(currentConversation.Count - 1, convoPanel.Controls[0]);
                            return;
                        }
                        else
                        {
                            //convoPanel.Visible = false;
                            //not within time frame so go ahead and buffer new space

                            /* Panel buffer = ChatUI.getPanelBuffer(4);
                             * convoPanel.Controls.Add(buffer);
                             * convoPanel.Controls.SetChildIndex(buffer, 0);
                             * contact.updateConversation(buffer);*/
                        }
                    }
                    else
                    {
                        buffer = 14;
                        //convoPanel.Visible = false;
                        //Message was not yours, so append space

                        /*Panel buffer = ChatUI.getPanelBuffer(14);
                         * convoPanel.Controls.Add(buffer);
                         * convoPanel.Controls.SetChildIndex(buffer, 0);
                         * contact.updateConversation(buffer);*/
                    }
                }

                //Append new message to our convoBox
                ChatUI             msg        = new ChatUI(message, buffer, emoji);
                Panel              messageTab = msg.getMessageTab();
                MyFixedRichTextBox messageBox = msg.getMessageBox();
                emoji.insertEmoji(messageBox);
                contact.updateConversation(messageTab);
                convoPanel.Controls.Add(messageTab);
                convoPanel.Controls.SetChildIndex(messageTab, 0);
                convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum);
                //convoPanel.Visible = true;
            }
        }
示例#2
0
        private void receiveMessage(int index, string message)
        {
            contacts.Invoke(new MethodInvoker(delegate
            {
                Contact contact = contactList[index];
                //Notify you that you got a message
                //contactList[index].updateConversation(message);
                if (contactIndex != index)
                {
                    //Reset Friend Name
                    contact.getContactName().Text = contact.getPacket().getUsername();
                    contact.newUnreadMessage(message, DateTime.Now.ToString("h:mm tt"));
                    contact.getContactName().Text += " (" + contact.getUnreadMessageCount().ToString() + ")";
                }
                else
                {
                    convoPanel.Invoke(new MethodInvoker(delegate
                    {
                        int buffer = 4;
                        List <Control> currentConversation = contact.getConversationUI();
                        if (convoPanel.Controls.Count > 0)
                        {
                            //Check if last message was not yours
                            if (convoPanel.Controls[0].Controls[0].BackColor == Color.FromArgb(240, 244, 248))
                            {
                                //Check if your within time frame to merge received messages
                                if (convoPanel.Controls[0].Controls[1].Text == DateTime.Now.ToString("h:mm tt"))
                                {
                                    MyFixedRichTextBox.AppendText((MyFixedRichTextBox)convoPanel.Controls[0].Controls[0].Controls[0], "\r\n" + message, emoji);
                                    convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum);
                                    //Update new message to the conversationUI
                                    contact.updateConversation(currentConversation.Count - 1, convoPanel.Controls[0]);
                                    return;
                                }
                                else
                                {
                                    //not within time frame so go ahead and buffer new space

                                    /*Panel buffer = ChatUI.getPanelBuffer(4);
                                     * contact.updateConversation(buffer);
                                     * convoPanel.Controls.Add(buffer);
                                     * convoPanel.Controls.SetChildIndex(buffer, 0);*/
                                }
                            }
                            else
                            {
                                //Message was yours, so append space
                                buffer = 14;

                                /*Panel buffer = ChatUI.getPanelBuffer(14);
                                 * contact.updateConversation(buffer);
                                 * convoPanel.Controls.Add(buffer);
                                 * convoPanel.Controls.SetChildIndex(buffer, 0);*/
                            }
                        }

                        //Append new message to your convoBox
                        ChatUI msg       = new ChatUI(message, DateTime.Now.ToString("h:mm tt"), contact.getContactPicture().Image, buffer, emoji);
                        Panel messageTab = msg.getMessageTab();
                        MyFixedRichTextBox messageBox = msg.getMessageBox();
                        emoji.insertEmoji(messageBox);
                        contact.updateConversation(messageTab);
                        convoPanel.Controls.Add(messageTab);
                        convoPanel.Controls.SetChildIndex(messageTab, 0);
                        convoPanel.AutoScrollPosition = new Point(0, convoPanel.VerticalScroll.Maximum);

                        //convoBox.Text = contactList[index].getConversation();
                        //convoBox.SelectionStart = convoBox.TextLength;
                        //convoBox.ScrollToCaret();
                    }));
                }
            }));
        }