//public void ScrollToBottom() //{ // SendMessage(rtb.Handle, WM_VSCROLL, SB_BOTTOM, 0); //} void AppendNStext(string text) { ////bool focused = rtb.Focused; //////backup initial selection //int selection = rtb.SelectionStart; ////int length = rtb.SelectionLength; //////allow autoscroll if selection is at end of text //bool autoscroll = (selection == rtb.Text.Length); ////if (!autoscroll) ////{ //// //shift focus from RichTextBox to some other control //// if (focused) rtb.Parent.Focus(); //// //hide selection //// SendMessage(rtb.Handle, EM_HIDESELECTION, 1, 0); ////} ////if (focused) ////{ //// //rtb.HideSelection = true; ////} ////else ////{ //// //rtb.HideSelection = false; ////} //rtb.AppendText(text); //if (autoscroll) ScrollToBottom(); ////if (!autoscroll) ////{ //// //restore initial selection //// rtb.SelectionStart = selection; //// rtb.SelectionLength = length; //// //unhide selection //// SendMessage(rtb.Handle, EM_HIDESELECTION, 0, 0); //// //restore focus to RichTextBox //// if (focused) rtb.Focus(); ////} rtb.AppendText(text); }
/// <summary> /// Writes a line to the textbox. Automaticall adds newline character /// </summary> /// <param name="text"></param> public void WriteLine(Ubiquitous.MainForm.UbiMessage message, ChatIcon icon = ChatIcon.Default, bool highlight = false, Color?foreColor = null, Color?backColor = null) { if (tb == null) { return; } if (tb.InvokeRequired) { SetTextCallback d = new SetTextCallback(WriteLine); try { tb.Parent.Invoke(d, new object[] { message, icon, highlight, foreColor, backColor }); } catch { } } else { Bitmap chatIcon = GetChatBitmap(icon); if (tb.Text.Length > 0) { tb.AppendText(Environment.NewLine); } if (!string.IsNullOrEmpty(message.Text)) { if (tb.TimeStamp) { tb.AppendTextAsRtf(DateTime.Now.GetDateTimeFormats('T')[0] + " ", tb.TimestampFont, tb.TimestampColor); } if (chatIcon != null) { tb.InsertImage(chatIcon); } tb.AppendTextAsRtf(" ", tb.Font, tb.TextColor); if (message.TextOnly) { if (highlight) { tb.AppendTextAsRtf(message.Text, tb.PersonalMessageFont, tb.PersonalMessageColor, tb.PersonalMessageBack); } else if (foreColor.HasValue && backColor.HasValue) { tb.AppendTextAsRtf(message.Text, tb.Font, foreColor.Value, backColor.Value); } else { tb.AppendTextAsRtf(message.Text, tb.Font, tb.TextColor); } } else { String messageFormat = String.Empty; if (!String.IsNullOrEmpty(message.FromGroupName)) { messageFormat = settings.appearanceGrpMessageFormat; } else if (!String.IsNullOrEmpty(message.FromName)) { messageFormat = settings.appearanceMsgFormat; } String[] messageFormatParts = messageFormat.Split('%'); String suffix = String.Empty; foreach (String messageFormatPart in messageFormatParts) { if (messageFormatPart.StartsWith("t")) { suffix = messageFormatPart.Length > 1 ? messageFormatPart.Substring(1) : String.Empty; if (highlight) { tb.AppendTextAsRtf(message.Text, tb.PersonalMessageFont, tb.PersonalMessageColor, tb.PersonalMessageBack); } else if (foreColor.HasValue && backColor.HasValue) { tb.AppendTextAsRtf(message.Text, tb.Font, foreColor.Value, backColor.Value); } else { tb.AppendTextAsRtf(message.Text, tb.Font, tb.TextColor); } if (!String.IsNullOrEmpty(suffix)) { tb.AppendTextAsRtf(suffix, tb.Font, tb.TextColor); } } else if (messageFormatPart.StartsWith("sg")) { suffix = messageFormatPart.Length > 2 ? messageFormatPart.Substring(2) : String.Empty; tb.AppendTextAsRtf(message.FromGroupName == null ? String.Empty : message.FromGroupName, tb.Font, message.NickColor); if (!String.IsNullOrEmpty(suffix)) { tb.AppendTextAsRtf(suffix, tb.Font, tb.TextColor); } } else if (messageFormatPart.StartsWith("s")) { suffix = messageFormatPart.Length > 1 ? messageFormatPart.Substring(1) : String.Empty; tb.AppendTextAsRtf(message.FromName == null ? String.Empty : message.FromName, tb.Font, message.NickColor); if (!String.IsNullOrEmpty(suffix)) { tb.AppendTextAsRtf(suffix, tb.Font, tb.TextColor); } } else if (messageFormatPart.StartsWith("d")) { suffix = messageFormatPart.Length > 1 ? messageFormatPart.Substring(1) : String.Empty; tb.AppendTextAsRtf(message.ToName == null ? String.Empty : "->" + message.ToName, tb.Font, message.NickColor); if (!String.IsNullOrEmpty(suffix)) { tb.AppendTextAsRtf(suffix, tb.Font, tb.TextColor); } } else if (messageFormatPart.StartsWith("c")) { suffix = messageFormatPart.Length > 1 ? messageFormatPart.Substring(1) : String.Empty; tb.AppendTextAsRtf(message.FromEndPoint.ToString(), tb.Font, message.NickColor); if (!String.IsNullOrEmpty(suffix)) { tb.AppendTextAsRtf(suffix, tb.Font, tb.TextColor); } } } } //tb.AppendText(" " + text); } tb.SelectionStart = tb.Text.Length; tb.SelectionLength = 0; tb.ScrollToEnd(); } }