private void SetShowMsg(string strShowMsg) { if (richTextBox2.InvokeRequired) { DsetRichText _call = new DsetRichText(SetShowMsg); this.Invoke(_call, strShowMsg); } else { richTextBox2.AppendText("[" + DateTime.Now.ToString() + "] " + strShowMsg); richTextBox2.ScrollToCaret(); } }
private void SetShowMsg(string strShowMsg) { //string time = DateTime.Now.ToString("HH:mm:ss"); this.Invoke(new Action(() => { TextBox textbox = new TextBox(); textbox.Multiline = true; textbox.TextAlign = HorizontalAlignment.Left; textbox.BorderStyle = BorderStyle.FixedSingle; //textbox.CreateControl(); textbox.ReadOnly = true; panel1.Controls.Add(textbox); //Point locationOnForm = textbox.FindForm().PointToClient(textbox.Parent.PointToScreen(textbox.Location)); if (textbox.InvokeRequired) { DsetRichText _call = new DsetRichText(SetShowMsg); this.Invoke(_call, strShowMsg); } else { if (strShowMsg.Contains(UserInfo.userId)) // 내가 보낸 메세지 { textbox.BackColor = Color.LightBlue; textbox.BorderStyle = BorderStyle.FixedSingle; if (userCheck == true) // 로그인 이후 { string message = strShowMsg.ToString(); string[] result = message.Split(new char[] { ']' }); string a = message.Remove(0, UserInfo.userId.Length + 1); textbox.Text = result[1]; } else // 로그인 전 { textbox.Text = strShowMsg; userCheck = true; } } else // 다른 사람이 보낸 메세지 { string receive_message = strShowMsg.ToString(); string[] receive_result = receive_message.Split(new char[] { ']' }); textbox.Text = strShowMsg; textbox.Left = receiveX; } Graphics textGraphics = Graphics.FromHwnd(textbox.Handle); SizeF stringSize = textGraphics.MeasureString(textbox.Text, textbox.Font); if ((int)stringSize.Width > 295) { int i = 0; String temp = ""; while (i < textbox.TextLength) { if (textbox.Text[i] != '\n' && textGraphics.MeasureString(temp + textbox.Text[i], textbox.Font).Width >= 295) { temp += "\r\n"; } temp += textbox.Text[i]; i++; } textbox.Text = temp; } if (textbox.Text[textbox.Text.Length - 1] == '\n') { textbox.Text = textbox.Text.Remove(textbox.Text.Length - 1); } stringSize = textGraphics.MeasureString(textbox.Text, textbox.Font); textbox.Size = new Size((int)stringSize.Width + 10, (int)stringSize.Height + 5); if (strShowMsg.Contains(UserInfo.userId)) { textbox.Left = panel1.Right - textbox.Width - 40;//pointX ; } if (postBox != null) { textbox.Top = postBox.Location.Y + postBox.Height + 10; } postBox = textbox; panel1.AutoScrollPosition = new Point(0, panel1.DisplayRectangle.Height); } })); }