我制作了一个简单的文本编辑器。其中,提供了一个用于更改字体系列,样式和颜色的组合框。我成功地更改了所选文本的字体属性。 我的代码
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;
}
}
我的困境:
每当我尝试将富文本框的当前字体属性设置为新文本框时,它都会成功更改它,但不会保留前一文本的字体属性。如何在不更改富文本框中文本的先前字体属性的情况下更改字体属性? 请帮忙,提前致谢!
答案 0 :(得分:0)
这就是你所需要的。我也有这个问题。 首先,您必须迭代所选文本的所有字符,然后应用字体。
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