[英]C# richtextbox font properties
我做了一個簡單的文本編輯器。 其中有一個用於更改字體系列,樣式和顏色的組合框。 我成功地更改了所選文本的字體屬性。 我的密碼
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;
}
}
我的困境:
每當我嘗試將富文本框的當前字體屬性設置為新字體時,它都會成功更改它,但不會保留先前文本的字體屬性。 我應該如何更改字體屬性,而不更改富文本框中文本的先前字體屬性? 請幫助,在此先感謝!
這就是你所需要的。 我也有這個問題。 首先,您必須迭代所選文本的所有字符,然后應用字體。
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
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.