C#RichTextBox带右键菜单封装

代码来源于网上。

首先创建解决方案,类

using System;
using System.Windows.Forms;
using System.Drawing;
using MyRichTextBox.Properties;

namespace MyRichTextBox
{
    public class MyRichTextBox : RichTextBox
    {
        private System.ComponentModel.IContainer components = null;
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        private ContextMenuStrip contextMenuStrip1;
        private System.Windows.Forms.ToolStripMenuItem CopyItem;
        private System.Windows.Forms.ToolStripMenuItem PasterItem;
        private System.Windows.Forms.ToolStripMenuItem UndoItem;
        private System.Windows.Forms.ToolStripMenuItem CutItem;
        private System.Windows.Forms.ToolStripMenuItem ClearItem;
        private System.Windows.Forms.ToolStripMenuItem SelectAllItem;
        private System.Windows.Forms.ToolStripMenuItem RedoItem;
        private ToolStripSeparator toolStripSeparator1;
        private ToolStripSeparator toolStripSeparator2;

        public MyRichTextBox()
        {
            InitializeComponent();
        }
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
          //  System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(XtraForm1));

            this.contextMenuStrip1 = new ContextMenuStrip(this.components);
            this.toolStripSeparator1 = new System.Windows.Forms.ToolStripSeparator();
            this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
            this.CopyItem = new ToolStripMenuItem();
            this.PasterItem = new ToolStripMenuItem();
            this.UndoItem = new ToolStripMenuItem();
            this.CutItem = new ToolStripMenuItem();
            this.ClearItem = new ToolStripMenuItem();
            this.SelectAllItem = new ToolStripMenuItem();
            this.RedoItem = new ToolStripMenuItem();
            this.contextMenuStrip1.SuspendLayout();
            this.SuspendLayout();


            this.contextMenuStrip1.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.UndoItem,
           this.toolStripSeparator1 ,
            this.CutItem,
            this.CopyItem,
            this.PasterItem,
            this.ClearItem,
          this.toolStripSeparator2,
            this.SelectAllItem,            
            this.RedoItem             
            });
            this.contextMenuStrip1.Name = "contextMenuStrip1";
            this.contextMenuStrip1.Size = new System.Drawing.Size(149, 166);
            this.ContextMenuStrip = this.contextMenuStrip1;

            base.MouseUp += new MouseEventHandler(MyRichTextBox_MouseUp);

            this.toolStripSeparator1.Name = "toolStripSeparator1";
            this.toolStripSeparator1.Size = new System.Drawing.Size(149, 6);
            // 
            // toolStripSeparator2
            // 
            this.toolStripSeparator2.Name = "toolStripSeparator2";
            this.toolStripSeparator2.Size = new System.Drawing.Size(149, 6);



            this.UndoItem.Name = "UndoItem";
            this.UndoItem.Size = new System.Drawing.Size(100, 22);
            this.UndoItem.Image = Resources.undo;
            this.UndoItem.Text = "撤销          ";
            this.UndoItem.Click += new System.EventHandler(this.UndoItem_Click);
            // 
            // CutItem
            // 
            this.CutItem.Name = "CutItem";
            this.CutItem.Size = new System.Drawing.Size(100, 22);
            this.CutItem.Text = "剪切           ";
            this.CutItem.Image = Resources.cut;
            this.CutItem.Click += new System.EventHandler(this.CutItem_Click);
            // 
            // CopyItem
            // 
            this.CopyItem.Name = "CopyItem";
            this.CopyItem.Size = new System.Drawing.Size(100, 22);
            this.CopyItem.Text = "复制";
            this.CopyItem.Image = Resources.copy;
            this.CopyItem.Click += new System.EventHandler(this.CopyItem_Click);
            // 

            this.CopyItem.Name = "Redo";
            this.CopyItem.Size = new System.Drawing.Size(100, 22);
            this.CopyItem.Text = "重做";
            this.CopyItem.Image = Resources.cancel;
            this.RedoItem.Click += new EventHandler(RedoItem_Click);
            // PasterItem
            // 
            this.PasterItem.Name = "PasterItem";
            this.PasterItem.Size = new System.Drawing.Size(100, 22);
            this.PasterItem.Text = "粘贴";
            this.PasterItem.Image = Resources.paster;
            this.PasterItem.Click += new System.EventHandler(this.PasterItem_Click);
            // 
            // ClearItem
            // 
            this.ClearItem.Name = "ClearItem";
            this.ClearItem.Size = new System.Drawing.Size(100, 22);
            this.ClearItem.Text = "删除";
            this.ClearItem.Image = Resources.clear;
            this.ClearItem.Click += new System.EventHandler(this.ClearItem_Click);
            // 
            // SelectAllItem
            // 
            this.SelectAllItem.Name = "SelectAllItem";
            this.SelectAllItem.Size = new System.Drawing.Size(100, 22);
            this.SelectAllItem.Text = "全选";
            this.SelectAllItem.Image = Resources.all;
            this.SelectAllItem.Click += new System.EventHandler(this.SelectAllItem_Click);


            this.contextMenuStrip1.ResumeLayout(false);
            this.ResumeLayout(false);

        }

        private void MyRichTextBox_MouseUp(object sender, MouseEventArgs e)//控制右键菜单的显示
        {
            if (e.Button == MouseButtons.Right)
            {
                if (base.CanRedo)//redo重做
                {
                    UndoItem.Enabled = true;//CopyItem
                }
                else
                {
                    UndoItem.Enabled = false;
                }
                if (base.CanUndo)//undo
                {
                    UndoItem.Enabled = true;
                }
                else
                {
                    UndoItem.Enabled = false;
                }

                if (base.SelectionLength > 0)
                {
                    CopyItem.Enabled = true;
                    CutItem.Enabled = true;//剪切
                    ClearItem.Enabled = true;
                }
                else
                {
                    CopyItem.Enabled = false;
                    CutItem.Enabled = false;
                    ClearItem.Enabled = false;
                }

                if (Clipboard.GetDataObject().GetDataPresent(DataFormats.Text))
                {
                    PasterItem.Enabled = true;
                }
                else
                {
                    PasterItem.Enabled = false;
                    contextMenuStrip1.Show(this, new Point(e.X, e.Y));
                }
            }
        }
        private void CopyItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();//先获取焦点,防止点两下才运行
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Copy();
        }

        private void PasterItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Paste();

        }

        private void UndoItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Undo();
        }

        private void CutItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Cut();
        }


        private void ClearItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.SelectedText = string.Empty;
        }

        private void SelectAllItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.SelectAll();
        }

        private void RedoItem_Click(object sender, EventArgs e)
        {
            this.contextMenuStrip1.SourceControl.Select();
            RichTextBox rtb = (RichTextBox)this.contextMenuStrip1.SourceControl;
            rtb.Redo();
        }
    }
}

调用方式:
首先添加生成的dll,然后在窗口上添加一个richTextBox1控件,在构建函数中,改一下

   this.richTextBox1 = new MyRichTextBox.MyRichTextBox();

 

posted @ 2013-08-06 16:36  永远的菜鸟@me  阅读(2459)  评论(0编辑  收藏  举报