(4) 分别双击ContextMenuStrip控件中的各菜单项,添加其Click事件,代码如下:
private void toolStripMenuItem大字体_Click(object sender, EventArgs e)
{
FontFamily oldFontFamily = richTextBox1.SelectionFont.FontFamily;
richTextBox1.SelectionFont = new Font(oldFontFamily, 24, oldFontStyle);
}
private void toolStripMenuItem小字体_Click(object sender, EventArgs e)
{
FontFamily oldFontFamily = richTextBox1.SelectionFont.FontFamily;
richTextBox1.SelectionFont = new Font(oldFontFamily, smallSize, oldFontStyle);
}
private void toolStripMenuItem红色字体_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Red;
}
private void toolStripMenuItem蓝色字体_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Blue;
}
private void toolStripMenuItem绿色字体_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = Color.Green;
}
private void toolStripMenuItem恢复字体大小_Click(object sender, EventArgs e)
{
richTextBox1.SelectionFont = new Font(oldFontFamily, 8, oldFontStyle);
}
private void toolStripMenuItem恢复字体颜色_Click(object sender, EventArgs e)
{
richTextBox1.SelectionColor = oldColor;
}
private void toolStripMenuItem保存文件_Click(object sender, EventArgs e)
{
richTextBox1.SaveFile("D:\\source.rtf", RichTextBoxStreamType.RichText);
}
(5) 按〈F5〉键编译并执行,尝试利用快捷菜单对richTextBox1中的文本进行各种操作。