工具栏使用示例
来源:互联网 发布:淘宝发布虚拟物品 编辑:程序博客网 时间:2024/05/17 20:14
using System;using System.Collections.Generic;using System.ComponentModel;using System.Data;using System.Drawing;using System.Linq;using System.Text;using System.Threading.Tasks;using System.Windows.Forms;namespace 工具栏使用示例{ public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void 粗体BToolStripMenuItem_Click(object sender, EventArgs e) { if (this.richTextBox1.SelectionFont == null) return; ToolStripMenuItem menuItem = (ToolStripMenuItem)sender; Font oldFont = this.richTextBox1.SelectionFont; Font newFont; if (menuItem.Checked) newFont = new Font(oldFont, oldFont.Style | FontStyle.Bold); else newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Bold); this.richTextBox1.SelectionFont = newFont; this.toolStripButtonBold.Checked = menuItem.Checked; } private void 斜体IToolStripMenuItem_Click(object sender, EventArgs e) { if (this.richTextBox1.SelectionFont == null) return; ToolStripMenuItem menuItem = (ToolStripMenuItem)sender; Font oldFont = this.richTextBox1.SelectionFont; Font newFont; if (menuItem.Checked) newFont = new Font(oldFont, oldFont.Style | FontStyle.Italic); else newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Italic); this.richTextBox1.SelectionFont = newFont; this.toolStripButtonItalic.Checked = menuItem.Checked; } private void 下划线UToolStripMenuItem_Click(object sender, EventArgs e) { if (this.richTextBox1.SelectionFont == null) return; Font oldFont = richTextBox1.SelectionFont; ToolStripMenuItem menuItem = (ToolStripMenuItem)sender; Font newFont; if (menuItem.Checked) newFont = new Font(oldFont, oldFont.Style | FontStyle.Underline); else newFont = new Font(oldFont, oldFont.Style & ~FontStyle.Underline); this.richTextBox1.SelectionFont = newFont; this.toolStripButtonUnderLine.Checked = menuItem.Checked; } private void alignToolStipMenuItem_Click(object sender, EventArgs e) { ToolStripMenuItem menuItem = (ToolStripMenuItem)sender; switch (menuItem.Text) { case "左对齐(&L)": this.richTextBox1.SelectionAlignment = HorizontalAlignment.Left; this.左对齐LToolStripMenuItem.Checked = true; this.toolStripButtonLeft.Checked = true; this.右对齐RToolStripMenuItem.Checked = false; this.toolStripButtonRight.Checked = false; this.居中CToolStripMenuItem.Checked = false; this.toolStripButtonCenter.Checked = false; break; case "居中(&C)": this.richTextBox1.SelectionAlignment = HorizontalAlignment.Center; this.左对齐LToolStripMenuItem.Checked = false; this.toolStripButtonLeft.Checked = false; this.右对齐RToolStripMenuItem.Checked = false; this.toolStripButtonRight.Checked = false; this.居中CToolStripMenuItem.Checked = true; this.toolStripButtonCenter.Checked = true; break; case "右对齐(&R)": this.richTextBox1.SelectionAlignment = HorizontalAlignment.Right; this.左对齐LToolStripMenuItem.Checked = false; this.toolStripButtonLeft.Checked = false; this.右对齐RToolStripMenuItem.Checked = true; this.toolStripButtonRight.Checked = true; this.居中CToolStripMenuItem.Checked = false; this.toolStripButtonCenter.Checked = false; break; } } private void richTextBox1_SelectionChanged(object sender, EventArgs e) { if (this.richTextBox1.SelectionFont == null) return; float fontSize = this.richTextBox1.SelectionFont.Size; string fontName = this.richTextBox1.SelectionFont.Name; HorizontalAlignment align = this.richTextBox1.SelectionAlignment; //格式刷 this.粗体BToolStripMenuItem.Checked = this.richTextBox1.SelectionFont.Bold; this.toolStripButtonBold.Checked = this.richTextBox1.SelectionFont.Bold; this.斜体IToolStripMenuItem.Checked = this.richTextBox1.SelectionFont.Italic; this.toolStripButtonItalic.Checked = this.richTextBox1.SelectionFont.Italic; this.toolStripButtonUnderLine.Checked = this.richTextBox1.SelectionFont.Underline; this.下划线UToolStripMenuItem.Checked = this.richTextBox1.SelectionFont.Underline; this.居中CToolStripMenuItem.Checked = (align == HorizontalAlignment.Center); this.toolStripButtonLeft.Checked = (align == HorizontalAlignment.Left); this.左对齐LToolStripMenuItem.Checked = (align == HorizontalAlignment.Left); this.toolStripButtonCenter.Checked = (align == HorizontalAlignment.Center); this.右对齐RToolStripMenuItem.Checked = (align == HorizontalAlignment.Right); this.toolStripButtonRight.Checked = (align == HorizontalAlignment.Right); //禁用粘贴,复制,剪切工具 this.toolStripButtonCut.Enabled = (this.richTextBox1.SelectionLength > 0); this.toolStripButtonCopy.Enabled = (this.richTextBox1.SelectionLength > 0); this.toolStripButtonPaste.Enabled = (Clipboard.GetText().Length > 0); } private void toolstripButton_Click(object sender, EventArgs e) { ToolStripButton tsb = (ToolStripButton)sender; string text = tsb.Text; switch (text) { case "粗体": this.粗体BToolStripMenuItem.PerformClick(); break; case "斜体": this.斜体IToolStripMenuItem.PerformClick(); break; case "下划线": this.下划线UToolStripMenuItem.PerformClick(); break; case "左对齐": this.左对齐LToolStripMenuItem.PerformClick(); break; case "右对齐": this.右对齐RToolStripMenuItem.PerformClick(); break; case "居中": this.居中CToolStripMenuItem.PerformClick(); break; } } }}