[英]How to change FontStyle back in RichTextBox?
我正在使用單擊按鈕事件使選擇文本加粗,它有效。 但是當我選擇它並再次單擊相同的按鈕時,我想使它成為常規但它不起作用。
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;
}
}
問題在這里:
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
作為按位運算“當前樣式而不是粗體”
一種更簡單的方法是在舊樣式上使用 XOR 運算符,並帶有要切換的新標志。 這樣,您無需先檢查樣式。
private void btnBold_Click(object sender, EventArgs e)
{
if (rtbMakale.SelectionLength > 0)
{
rtbMakale.SelectionFont = new Font(rtbMakale.SelectionFont,
rtbMakale.SelectionFont.Style ^ FontStyle.Bold);
}
}
我已經編輯了代碼,它功能齊全。 使用它,您還可以輕松執行許多其他格式化操作。 下面,我還添加了我從中得出的鏈接。
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;
}
鏈接在這里:
http://how-to-code-net.blogspot.com/2014/01/how-to-make-custom-richtextbox-control.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.