【问题标题】:C# richtextbox font propertiesC#richtextbox 字体属性
【发布时间】:2012-04-09 03:31:59
【问题描述】:

我做了一个简单的文本编辑器。其中有一个用于更改字体系列、样式和颜色的组合框。我成功地更改了所选文本的字体属性。 我的代码

private void fontFamilySelectedIndexChanged(object sender, EventArgs e)
        {
            if (notesCon[currentNotes].SelectedText != "")
            {
                notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);                
            }
        }

        private void fontStyleSelectedIndexChanged(object sender, EventArgs e) 
        {
            if (notesCon[currentNotes].SelectedText != "")
            {
                switch (fontStyle.SelectedItem.ToString())
                {
                    case "Bold": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Bold);
                        break;
                    case "Italic": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Italic);
                        break;
                    case "Regular": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Regular);
                        break;
                    case "Strikeout": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Strikeout);
                        break;
                    case "Underline": notesCon[currentNotes].SelectionFont = new Font(notesCon[currentNotes].SelectionFont.FontFamily, notesCon[currentNotes].SelectionFont.Size, FontStyle.Underline);
                        break;

                }
            }            
        }

        private void fontColorSelectedValueChanged(object sender, EventArgs e)
        {
            if (notesCon[currentNotes].SelectedText != "")
            {
                notesCon[currentNotes].SelectionFont = new Font(fontFamily.SelectedItem.ToString(), notesCon[currentNotes].SelectionFont.Size, notesCon[currentNotes].SelectionFont.Style);
                notesCon[currentNotes].SelectionColor = fontColor.SelectedItem.Color;
            }
        }

我的困境:

每当我尝试将富文本框的当前字体属性设置为新的时,它都会成功更改它,但不会保留以前文本的字体属性。我应该如何更改字体属性,而不更改富文本框中文本的先前字体属性? 请帮助,在此先感谢!

【问题讨论】:

    标签: c# fonts richtextbox


    【解决方案1】:

    这是你需要的。我也有这个问题。 首先,您必须迭代所选文本的所有字符,然后应用字体。

    private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            _maskChanges = true;
            try
            {
                int txtStartPosition = txtFunctionality.SelectionStart;
                int selectionLength = txtFunctionality.SelectionLength;
                if (selectionLength > 0)
                    using (RichTextBox txtTemp = new RichTextBox())
                    {
                        txtTemp.Rtf = txtFunctionality.SelectedRtf;
                        for (int i = 0; i < selectionLength; ++i)
                        {
                            txtTemp.Select(i, 1);
                            txtTemp.SelectionFont = RenderFont(txtTemp.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
                        }
    
                        txtTemp.Select(0, selectionLength);
                        txtFunctionality.SelectedRtf = txtTemp.SelectedRtf;
                        txtFunctionality.Select(txtStartPosition, selectionLength);
                    }
            }
            finally
            {
                _maskChanges = false;
            }
        }
    

    并渲染字体:

    /// <summary>
        /// Changes a font from originalFont appending other properties
        /// </summary>
        /// <param name="originalFont">Original font of text
        /// <param name="familyName">Target family name
        /// <param name="emSize">Target text Size
        /// <param name="fontStyle">Target font style
        /// <param name="enableFontStyle">true when enable false when disable
        /// <returns>A new font with all provided properties added/removed to original font</returns>
        private Font RenderFont(Font originalFont, string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
        {
            if (fontStyle.HasValue && fontStyle != FontStyle.Regular && fontStyle != FontStyle.Bold && fontStyle != FontStyle.Italic && fontStyle != FontStyle.Underline)
                throw new System.InvalidProgramException("Invalid style parameter to ChangeFontStyleForSelectedText");
    
            Font newFont;
            FontStyle? newStyle = null;
            if (fontStyle.HasValue)
            {
                if (fontStyle.HasValue && fontStyle == FontStyle.Regular)
                    newStyle = fontStyle.Value;
                else if (originalFont != null && enableFontStyle.HasValue && enableFontStyle.Value)
                    newStyle = originalFont.Style | fontStyle.Value;
                else
                    newStyle = originalFont.Style & ~fontStyle.Value;
            }
    
            newFont = new Font(!string.IsNullOrEmpty(familyName) ? familyName : originalFont.FontFamily.Name,
                                emSize.HasValue ? emSize.Value : originalFont.Size,
                                newStyle.HasValue ? newStyle.Value : originalFont.Style);
            return newFont;
        }
    

    更多细节和步骤可以阅读这篇文章:http://how-to-code-net.blogspot.ro/2014/01/how-to-make-custom-richtextbox-control.html

    【讨论】:

      猜你喜欢
      • 2010-11-23
      • 2015-06-26
      • 1970-01-01
      • 2010-10-31
      • 1970-01-01
      • 2014-06-09
      • 2015-10-08
      • 1970-01-01
      • 2010-12-03
      相关资源
      最近更新 更多