C# 文档编辑器设置字体、大小、字体格式
思路:如果选择的是一个字符,直接调用 curRichTextBox.SelectionFont 方法
如果选择的是两个字符以上则实例化一个临时的的temprichtextbox将选择的字符复制到里面,对字符进行组个修改。然后将temprichtextbox中修改完的字符串替换原来的字符创
添加字体格式(加粗、斜体、下划线、删除线)
private void SetFontStyle(FontStyle fontstyle) {
if (fontstyle != FontStyle.Bold && fontstyle != FontStyle.Italic && fontstyle != FontStyle.Underline&&fontstyle!=FontStyle.Strikeout&&fontstyle!=FontStyle.Regular)
throw new System.InvalidProgramException("字体格式错误");
RichTextBox tempRichTextBox = new RichTextBox();
int tempRtbStart = 0;
Font font = curRichTextBox.SelectionFont;
if (len <= 1 && font != null) {
curRichTextBox.SelectionFont = new Font(font, font.Style | fontstyle);
return;
}
tempRichTextBox.Rtf = curRichTextBox.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
tempRichTextBox.SelectionFont =
new Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style | fontstyle);
}
tempRichTextBox.Select(tempRtbStart, len);
curRichTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
curRichTextBox.Select(curRtbStart, len);
curRichTextBox.Focus();
}
更改字体:
private void ChangeFont( String fontName)
{
if(fontName=="")
throw new System.InvalidProgramException("字体参数错误");
curRtbStart = curRichTextBox.SelectionStart;
len = curRichTextBox.SelectionLength;
RichTextBox tempRichTextBox = new RichTextBox();
int tempRtbStart = 0;
Font font = curRichTextBox.SelectionFont;
if (len <= 1 && font!= null) {
curRichTextBox.SelectionFont = new Font(fontName, font.Size, font.Style);
return;
}
tempRichTextBox.Rtf = curRichTextBox.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
tempRichTextBox.SelectionFont = new Font(fontName, tempRichTextBox.SelectionFont.Size, tempRichTextBox.SelectionFont.Style);
}
tempRichTextBox.Select(tempRtbStart, len);
curRichTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
curRichTextBox.Select(curRtbStart, len);
curRichTextBox.Focus();
}
去掉字体格式(加粗、斜体、下划线、删除线、)
private void RemoveFontStyle(FontStyle fontstyle)
{
if (fontstyle != FontStyle.Bold && fontstyle != FontStyle.Italic && fontstyle != FontStyle.Underline && fontstyle != FontStyle.Strikeout && fontstyle != FontStyle.Regular)
throw new System.InvalidProgramException("字体格式错误");
RichTextBox tempRichTextBox = new RichTextBox();
int tempRtbStart = 0;
Font font = curRichTextBox.SelectionFont;
if (len <= 1 && font != null)
{
curRichTextBox.SelectionFont = new Font(font, font.Style ^ fontstyle);
return;
}
tempRichTextBox.Rtf = curRichTextBox.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont,
tempRichTextBox.SelectionFont.Style ^ fontstyle);
}
tempRichTextBox.Select(tempRtbStart, len);
curRichTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
curRichTextBox.Select(curRtbStart, len);
curRichTextBox.Focus();
}
设置字体大小:
private void ChangFontSize(float fontSize)
{
if (fontSize <= 0.0)
throw new InvalidProgramException("字号参数错误");
curRtbStart = curRichTextBox.SelectionStart;
len = curRichTextBox.SelectionLength;
RichTextBox tempRichTextBox = new RichTextBox();
int tempRtbStart = 0;
Font font = curRichTextBox.SelectionFont;
if (len <= 1 && font != null) {
curRichTextBox.SelectionFont = new Font(font.Name, fontSize, font.Style);
return;
}
tempRichTextBox.Rtf = curRichTextBox.SelectedRtf;
for (int i = 0; i < len; i++)
{
tempRichTextBox.Select(tempRtbStart + i, 1);
tempRichTextBox.SelectionFont = new Font(tempRichTextBox.SelectionFont.Name, fontSize, tempRichTextBox.SelectionFont.Style);
}
tempRichTextBox.Select(tempRtbStart, len);
curRichTextBox.SelectedRtf = tempRichTextBox.SelectedRtf;
curRichTextBox.Select(curRtbStart, len);
curRichTextBox.Focus();
}