如何在 RichTextBox 中改回 FontStyle?

How to change FontStyle back in RichTextBox?

我正在使用单击按钮事件使 selection 文本变为粗体,它有效。但是当我 select 它并再次单击相同的按钮时,我想让它正常,但它不起作用。

private void btnBold_Click(object sender, EventArgs e)
        {
            if (rtbMakale.SelectionLength > 0)
            {
                if (isItBold())
                {
                    rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                        FontStyle.Regular | rtbMakale.SelectionFont.Style);
                    rtbMakale.SelectionLength = 0;
                }
                else
                {
                    rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                        FontStyle.Bold | rtbMakale.SelectionFont.Style);
                    rtbMakale.SelectionLength = 0;
                }

            }

            bool isItBold()
            {
                if (rtbMakale.SelectionFont.Bold)
                {
                    return true;
                }
                else
                {
                    return false;
                }
            }

我已经编辑了代码,它可以正常运行。使用它,您还可以轻松执行许多其他格式化操作。下面,我还添加了 link 从我派生的地方。

private void button1_Click(object sender, EventArgs e)
    {

        if (_richTextBox1.SelectionLength > 0)
        {
            if (_richTextBox1.SelectionFont.Bold == false)
            {
                ChangeOrSetFont(_richTextBox1.SelectionFont.ToString(), _richTextBox1.SelectionFont.Size, FontStyle.Bold, true);
            }
            else
            {
                ChangeOrSetFont(_richTextBox1.SelectionFont.ToString(), _richTextBox1.SelectionFont.Size, FontStyle.Regular, true);
            }
        }
    }

    bool _maskChanges;

    private void ChangeFontStyleForSelectedText(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
    {
        _maskChanges = true;
        try
        {
            int txtStartPosition = _richTextBox1.SelectionStart;
            int selectionLength = _richTextBox1.SelectionLength;
            if (selectionLength > 0)
                using (RichTextBox txtTemp = new RichTextBox())
                {
                    txtTemp.Rtf = _richTextBox1.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);
                    _richTextBox1.SelectedRtf = txtTemp.SelectedRtf;
                    _richTextBox1.Select(txtStartPosition, selectionLength);
                }
        }
        finally
        {
            _maskChanges = false;
        }
    }

    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;
    }

    private void ChangeOrSetFont(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
    {
        if (_richTextBox1.SelectionType == RichTextBoxSelectionTypes.Empty)
        {
            SetSelectionFont(familyName, emSize, fontStyle, enableFontStyle);
        }
        else
        {
            ChangeFontStyleForSelectedText(familyName, emSize, fontStyle, enableFontStyle);
        }
    }

    private void SetSelectionFont(string familyName, float? emSize, FontStyle? fontStyle, bool? enableFontStyle)
    {
        Font renderedFont = RenderFont(_richTextBox1.SelectionFont, familyName, emSize, fontStyle, enableFontStyle);
        _richTextBox1.SelectionFont = renderedFont;
    }

这里是 link:

http://how-to-code-net.blogspot.com/2014/01/how-to-make-custom-richtextbox-control.html

问题出在这里:

rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
                FontStyle.Regular | rtbMakale.SelectionFont.Style);

在这种情况下,SelectionFont.Style 包含标志 FontStyle.Bold。通过对旧样式使用 OR 运算符,将保留所有现有样式,包括粗体。

相反,使用逻辑运算符从样式中删除粗体标志:

rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
    rtbMakale.SelectionFont.Style & ~FontStyle.Bold);

读这部分rtbMakale.SelectionFont.Style & ~FontStyle.Bold按位运算"Current Style AND NOT bold"


一种更简单的方法是对旧样式使用 XOR 运算符和您要切换的新标志。这样就不用先检查样式了

private void btnBold_Click(object sender, EventArgs e)
{
    if (rtbMakale.SelectionLength > 0)
    {
        rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
            rtbMakale.SelectionFont.Style ^ FontStyle.Bold);
    }
}