winform RichTextBox 控件
RichTextBox:
常用的格式化文本方法:
1. 加粗
Font newFont; var oldFont = this.richTextBox1.SelectionFont; if (oldFont.Bold) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); } this.richTextBox1.SelectionFont = newFont; this.richTextBox1.Focus();
2. 斜体
Font newFont; var oldFont = this.richTextBox1.SelectionFont; if (oldFont.Italic) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic); } this.richTextBox1.SelectionFont = newFont; this.richTextBox1.Focus();
3. 下划线
Font newFont; var oldFont = this.richTextBox1.SelectionFont; if (oldFont.Underline) { newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline); } else { newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline); } this.richTextBox1.SelectionFont = newFont; this.richTextBox1.Focus();
4. 大小
var fontFamily = this.richTextBox1.SelectionFont.FontFamily; var newFont = new Font(fontFamily, 20); this.richTextBox1.SelectionFont = newFont; this.richTextBox1.Focus();
5. 对齐方式
this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center;
6. 保存及打开
this.richTextBox1.SaveFile("aa.rtf"); this.richTextBox1.LoadFile("aa.rtf");