单击时如何闪烁或闪烁ContextMenuStripItem?

时间:2019-04-28 15:05:15

标签: c# click menuitem contextmenustrip flashing

我正在尝试为用户提供视觉提示,以便他们可以看到他们单击了ContextMenuStrip中的某个选项。 我喜欢闪烁或闪烁的悬停(或bg颜色),例如:https://imgur.com/a/S1uK6yE

我尝试在此处使用答案:https://stackoverflow.com/a/11777092/10012792,但那无济于事,因为它应该与toolstripButton一起使用,而不是ContextMenuStrip toolstripMenuItem。

我想知道是否应该采用“响应ContextMenuStrip项目单击”的方式:How to respond to a ContextMenuStrip item click

点击事件:

private void Option1_Click(object sender, EventArgs e)
        { try
            { }
            catch (Exception)
            { }
        }

        private void Option2_Click(object sender, EventArgs e)
        { try
            { }
            catch (Exception)
            { throw; }
        }

        private void Option3_Click(object sender, EventArgs e)
        { try
            { }
            catch (Exception ex)
            { }
        }

设计师:

this.ctm_options.BackColor = System.Drawing.Color.FromArgb(((int)(((byte)(57)))), ((int)(((byte)(57)))), ((int)(((byte)(65)))));
            this.ctm_options.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
            this.toolStripOption1,
            this.toolStripOption2,
            this.toolStripOption3,
            this.toolStripSeparator1});
            this.ctm_options.Name = "ctm_options";
            this.ctm_options.RenderMode = System.Windows.Forms.ToolStripRenderMode.System;
            this.ctm_options.Size = new System.Drawing.Size(181, 104);
            this.ctm_options.Opening += new System.ComponentModel.CancelEventHandler(this.ctm_options_Opening);

请介意,我正在使用自定义渲染器来更改悬停颜色,例如:

public class MyRenderer : ToolStripProfessionalRenderer
        {
            public MyRenderer()
                : base(new MyColorTable())
            {
            }
            protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                var r = new Rectangle(e.ArrowRectangle.Location, e.ArrowRectangle.Size);
                r.Inflate(-2, -6);
                e.Graphics.DrawLines(Pens.Black, new Point[]{
        new Point(r.Left, r.Top),
        new Point(r.Right, r.Top + r.Height /2),
        new Point(r.Left, r.Top+ r.Height)});
            }

            protected override void OnRenderItemCheck(ToolStripItemImageRenderEventArgs e)
            {
                e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
                var r = new Rectangle(e.ImageRectangle.Location, e.ImageRectangle.Size);
                r.Inflate(-4, -6);
                e.Graphics.DrawLines(Pens.Black, new Point[]{
        new Point(r.Left, r.Bottom - r.Height /2),
        new Point(r.Left + r.Width /3,  r.Bottom),
        new Point(r.Right, r.Top)});
            }
        }

        public class MyColorTable : ProfessionalColorTable
        {
            public override Color MenuItemBorder
            {
                get { return Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215))))); ; }
            }
            public override Color MenuItemSelected
            {
                get { return Color.FromArgb(((int)(((byte)(0)))), ((int)(((byte)(120)))), ((int)(((byte)(215))))); ; }
            }
            public override Color ToolStripDropDownBackground
            {
                get { return Color.FromArgb(((int)(((byte)(57)))), ((int)(((byte)(57)))), ((int)(((byte)(65))))); ; }
            }
            public override Color ImageMarginGradientBegin
            {
                get { return Color.FromArgb(((int)(((byte)(57)))), ((int)(((byte)(57)))), ((int)(((byte)(65))))); ; }
            }
            public override Color ImageMarginGradientMiddle
            {
                get { return Color.FromArgb(((int)(((byte)(57)))), ((int)(((byte)(57)))), ((int)(((byte)(65))))); ; }
            }
            public override Color ImageMarginGradientEnd
            {
                get { return Color.FromArgb(((int)(((byte)(57)))), ((int)(((byte)(57)))), ((int)(((byte)(65))))); ; }
            }
        }
public Form()
        {
            InitializeComponent();
            this.ctm_options.Renderer = new MyRenderer();
        }

我将如何使用前面提到的toolstripButton代码,因此它也适用于toolstripMenuItems? 还是应该使用其他方法来解决这个问题?

0 个答案:

没有答案