///<summary>
///设置字体格式:粗体、斜体、下划线
///</summary>
/// <param name="style">事件触发后传参:字体格式类型</param>
private
void
ChangeFontStyle(FontStyle style)
{
if
(style != FontStyle.Bold && style != FontStyle.Italic &&
style != FontStyle.Underline)
throw
new
System.InvalidProgramException(
"字体格式错误"
);
RichTextBox tempRichTextBox =
new
RichTextBox();
int
curRtbStart = curRichTextBox.SelectionStart;
int
len = curRichTextBox.SelectionLength;
int
tempRtbStart = 0;
Font font = curRichTextBox.SelectionFont;
if
(len <= 1 && font !=
null
)
{
if
(style == FontStyle.Bold && font.Bold ||
style == FontStyle.Italic && font.Italic ||
style == FontStyle.Underline && font.Underline)
{
curRichTextBox.SelectionFont =
new
Font(font, font.Style ^ style);
}
else
if
(style == FontStyle.Bold && !font.Bold ||
style == FontStyle.Italic && !font.Italic ||
style == FontStyle.Underline && !font.Underline)
{
curRichTextBox.SelectionFont =
new
Font(font, font.Style | style);
}
return
;
}
tempRichTextBox.Rtf = curRichTextBox.SelectedRtf;
tempRichTextBox.Select(len - 1, 1);
Font tempFont = (Font)tempRichTextBox.SelectionFont.Clone();
for
(
int
i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
if
(style == FontStyle.Bold && tempFont.Bold ||
style == FontStyle.Italic && tempFont.Italic ||
style == FontStyle.Underline && tempFont.Underline)
{
tempRichTextBox.SelectionFont =
new
Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style ^ style);
}
else
if
(style == FontStyle.Bold && !tempFont.Bold ||
style == FontStyle.Italic && !tempFont.Italic ||
style == FontStyle.Underline && !tempFont.Underline)
{
tempRichTextBox.SelectionFont =
new
Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style | style);
}
}
tempRichTextBox.Select(tempRtbStart, len);
curRichTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
curRichTextBox.Select(curRtbStart, len);
}