-1

我在上下文菜单上做的可以在右键单击上找到,就像在实际的代码编辑器中一样: 在此处输入图像描述

我已经使用以下代码完成了剪切、复制和粘贴:

private void rtb_MouseDown(object sender, MouseEventArgs e)
        {
if (e.Button == MouseButtons.Right)
            {

                MenuItem[] menuItems = new MenuItem[] { 
                                        new MenuItem("Cut", new System.EventHandler(this.CutMenuItemClick)), 
                                        new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick)),
                                        new MenuItem("Paste", new System.EventHandler(this.PasteMenuItemClick)), 


                ContextMenu rightcontext = new ContextMenu(menuItems);

                int xOffset = Cursor.Position.X - DtexteditoR.ActiveForm.Location.X;
                int yOffset = Cursor.Position.Y - DtexteditoR.ActiveForm.Location.Y;

                rightcontext.Show(DtexteditoR.ActiveForm, new Point(xOffset, yOffset));

            }
        }
private void CutMenuItemClick(object sender, EventArgs e)
        {
            rtb.Cut();
        }
        private void CopyMenuItemClick(object sender, EventArgs e)
        {
            rtb.Copy();
        }
        private void PasteMenuItemClick(object sender, EventArgs e)
        {
            rtb.Paste();
        }

我使用带有动态控件的winforms(不要使用设计器),我的问题是如何在一个控件(不同的处理程序)中制作多个事件处理程序,如下所示:

new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)),

private void MeasureCopy(object obj,
                           MeasureItemEventArgs miea)
        {
            MenuItem mi = (MenuItem)obj;

            // Get standard menu font so that the text in this
            // menu rectangle doesn't look funny with a
            // different font
            Font menuFont = SystemInformation.MenuFont;

            StringFormat strfmt = new StringFormat();
            SizeF sizef =
                miea.Graphics.MeasureString(mi.Text, menuFont, 1000, strfmt);

            // Get image so size can be computed
            Bitmap bmMenuImage = new Bitmap(typeof(NewForm), "COPY.BMP");

            // Add image height and width  to the text height and width when 
            // drawn with selected font (got that from measurestring method)
            // to compute the total height and width needed for the rectangle
            miea.ItemWidth = (int)Math.Ceiling(sizef.Width) + bmMenuImage.Width;
            miea.ItemHeight = (int)Math.Ceiling(sizef.Height) + bmMenuImage.Height;
        }

让我能够在“复制”旁边添加图像。

如何做这件事:

new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick) || new System.Windows.Forms.MeasureItemEventHandler(this.MeasureCopy)),

正确的方式。谢谢!

4

2 回答 2

1

MeasureItem无法从构造函数设置事件,请尝试:

MenuItem item = new MenuItem("Copy", new System.EventHandler(this.CopyMenuItemClick));
item.MeasureItem += this.MeasureCopy;
于 2013-06-01T08:29:35.170 回答
1

我会用

MenuItem item = new MenuItem("Copy");
item.Click += this.CopyMenuItemClick;
item.Click += this.MeasureCopy;
于 2013-06-01T06:14:15.420 回答