コード例 #1
0
ファイル: RTFHelper.cs プロジェクト: hescano/IRClean
        public static void ShowMessage(String message, ExRichTextBox richTextControl, RtfColor foreColor, RtfColor backGround, FontStyle textStyle, HorizontalAlignment textAlignment = HorizontalAlignment.Left, Single textSize = 10, String textFont = "Tahoma", int crlf = 1, Control setFocusTo = null, bool isMessage = true)
        {
            if (richTextControl != null && !richTextControl.IsDisposed)
            {

                //cleaning left-over RTF characters (avoid breaking RTB)
                if (message.IndexOf(@"\") > -1)
                {
                    message = message.Replace(@"\", @"\\");
                }

                if (message.IndexOf("{") > -1)
                {
                    message = message.Replace("{", @"\{");
                }

                if (message.IndexOf("}") > -1)
                {
                    message = message.Replace("}", @"\}");
                }

                if (message.IndexOf(Environment.NewLine) > -1)
                {
                    message = message.Replace(Environment.NewLine, Environment.NewLine + @"\par  ");
                }

                //we deal with the emoticons only if it is a chat message (ignore emoticons on system message)
                if (isMessage)
                {
                    foreach (var icon in EmoticonsHelper.GetEmoticons())
                    {
                        if (icon.Key.IndexOf(",") > -1)
                        {
                            var optionalToken = icon.Key.Split(',');

                            foreach (var opt in optionalToken)
                            {
                                if (message.IndexOf(opt) > -1)
                                {
                                    //replace the token with its RTF equivalent ;)
                                    message = message.Replace(opt, icon.Value);
                                }
                            }
                        }
                        else if (message.IndexOf(icon.Key) > -1)
                        {
                            message = message.Replace(icon.Key, icon.Value);
                        }
                    }
                }

                FontStyle extraStyles = new FontStyle();
                if (message.IndexOf("") > -1)
                {
                    extraStyles |= FontStyle.Bold;
                }

                Font extraFonts = new Font(textFont, textSize, textStyle | extraStyles, GraphicsUnit.Point);

                //add extra carriage returns
                if (crlf > 0)
                {
                    for (int i = 0; i < crlf; i++)
                    {
                        message += Environment.NewLine;
                    }
                }

                RTFFractionMessage messageFractions = new RTFFractionMessage(message, foreColor, backGround);
                try
                {
                    if (message.IndexOf("\u0003") > -1)
                    {
                        string[] formattedParts = message.Split(new string[] { "\u0003" }, StringSplitOptions.None);

                        for (int i = 0; i <= formattedParts.Count() - 1; i++)
                        {
                            if (formattedParts[i].Length > 1)
                            {
                                if (IntegerHelper.IsNumeric(formattedParts[i].Substring(0, 1)))
                                {
                                    messageFractions = (ReturnRTFColor(formattedParts[i]));
                                    if (i < formattedParts.Length - 1)
                                        richTextControl.AppendTextAsRtf(messageFractions.Message, extraFonts, messageFractions.Forecolor, messageFractions.BackgroundColor);
                                }
                                else
                                {
                                    messageFractions = new RTFFractionMessage(formattedParts[i], RtfColor.Black, RtfColor.White);
                                    richTextControl.AppendTextAsRtf(messageFractions.Message, extraFonts, messageFractions.Forecolor, messageFractions.BackgroundColor);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //TODO: Handle
                }

                //write any extra text left
                richTextControl.AppendTextAsRtf(messageFractions.Message, extraFonts, messageFractions.Forecolor, messageFractions.BackgroundColor);

                //scroll to bottom of the RTB
                richTextControl.ScrollToCaret();
                //if something else needs to be focused, focus that
                if (setFocusTo != null)
                {
                    if (setFocusTo.CanFocus)
                    {
                        setFocusTo.Focus();
                    }
                }
            }
        }
コード例 #2
0
        /// <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();
            }
        }
コード例 #3
0
        public static void ShowMessage(String message, ExRichTextBox richTextControl, RtfColor foreColor, RtfColor backGround, FontStyle textStyle, HorizontalAlignment textAlignment = HorizontalAlignment.Left, Single textSize = 10, String textFont = "Tahoma", int crlf = 1, Control setFocusTo = null, bool isMessage = true)
        {
            if (richTextControl != null && !richTextControl.IsDisposed)
            {
                //cleaning left-over RTF characters (avoid breaking RTB)
                if (message.IndexOf(@"\") > -1)
                {
                    message = message.Replace(@"\", @"\\");
                }

                if (message.IndexOf("{") > -1)
                {
                    message = message.Replace("{", @"\{");
                }

                if (message.IndexOf("}") > -1)
                {
                    message = message.Replace("}", @"\}");
                }

                if (message.IndexOf(Environment.NewLine) > -1)
                {
                    message = message.Replace(Environment.NewLine, Environment.NewLine + @"\par  ");
                }

                //we deal with the emoticons only if it is a chat message (ignore emoticons on system message)
                if (isMessage)
                {
                    foreach (var icon in EmoticonsHelper.GetEmoticons())
                    {
                        if (icon.Key.IndexOf(",") > -1)
                        {
                            var optionalToken = icon.Key.Split(',');

                            foreach (var opt in optionalToken)
                            {
                                if (message.IndexOf(opt) > -1)
                                {
                                    //replace the token with its RTF equivalent ;)
                                    message = message.Replace(opt, icon.Value);
                                }
                            }
                        }
                        else if (message.IndexOf(icon.Key) > -1)
                        {
                            message = message.Replace(icon.Key, icon.Value);
                        }
                    }
                }

                FontStyle extraStyles = new FontStyle();
                if (message.IndexOf("") > -1)
                {
                    extraStyles |= FontStyle.Bold;
                }

                Font extraFonts = new Font(textFont, textSize, textStyle | extraStyles, GraphicsUnit.Point);

                //add extra carriage returns
                if (crlf > 0)
                {
                    for (int i = 0; i < crlf; i++)
                    {
                        message += Environment.NewLine;
                    }
                }

                RTFFractionMessage messageFractions = new RTFFractionMessage(message, foreColor, backGround);
                try
                {
                    if (message.IndexOf("\u0003") > -1)
                    {
                        string[] formattedParts = message.Split(new string[] { "\u0003" }, StringSplitOptions.None);

                        for (int i = 0; i <= formattedParts.Count() - 1; i++)
                        {
                            if (formattedParts[i].Length > 1)
                            {
                                if (IntegerHelper.IsNumeric(formattedParts[i].Substring(0, 1)))
                                {
                                    messageFractions = (ReturnRTFColor(formattedParts[i]));
                                    if (i < formattedParts.Length - 1)
                                    {
                                        richTextControl.AppendTextAsRtf(messageFractions.Message, extraFonts, messageFractions.Forecolor, messageFractions.BackgroundColor);
                                    }
                                }
                                else
                                {
                                    messageFractions = new RTFFractionMessage(formattedParts[i], RtfColor.Black, RtfColor.White);
                                    richTextControl.AppendTextAsRtf(messageFractions.Message, extraFonts, messageFractions.Forecolor, messageFractions.BackgroundColor);
                                }
                            }
                        }
                    }
                }
                catch
                {
                    //TODO: Handle
                }

                //write any extra text left
                richTextControl.AppendTextAsRtf(messageFractions.Message, extraFonts, messageFractions.Forecolor, messageFractions.BackgroundColor);

                //scroll to bottom of the RTB
                richTextControl.ScrollToCaret();
                //if something else needs to be focused, focus that
                if (setFocusTo != null)
                {
                    if (setFocusTo.CanFocus)
                    {
                        setFocusTo.Focus();
                    }
                }
            }
        }