更改 ToolStrip 子菜单的透明度

Change transparency for ToolStrip submenu

我现在正在篡改 ToolStrip 修改,并试图使子菜单也像 MenuStrip 一样透明。我无法使子菜单的 属性 像菜单本身一样。

我该怎么做?

这是我的修改代码:

    public class ArrowRenderer : ToolStripProfessionalRenderer
    {
        public ArrowRenderer() : base(new LeftMenuColorTable())
        {

        }
        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            var tsMenuItem = e.Item as ToolStripMenuItem;
            if (tsMenuItem != null)
                e.TextColor = Color.White;
            base.OnRenderItemText(e);
        }

        protected override void OnRenderArrow(ToolStripArrowRenderEventArgs e)
        {
            var tsMenuItem = e.Item as ToolStripMenuItem;
            if (tsMenuItem != null)
                e.ArrowColor = Color.White;
            base.OnRenderArrow(e);
        }
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            var tsMenuItem = e.Item as ToolStripMenuItem;
            if (tsMenuItem != null)
                e.Item.BackColor = Color.Black;
            base.OnRenderMenuItemBackground(e);
        }
    }
    public class LeftMenuColorTable : ProfessionalColorTable
    { 
        public override Color MenuItemSelected
        {
            // when the menu is selected
            get { return ColorTranslator.FromHtml("#494f52");  }
        }

        public override Color ToolStripBorder
        {
            get { return ColorTranslator.FromHtml("#FFFFFF"); }
        }
        public override Color ToolStripDropDownBackground
        {
            get { return Color.White; }
        }
    }

    internal void SetTrayMenu()
    {
        if (m_menu != null)
            if (notifyIcon.ContextMenuStrip != null)
                notifyIcon.ContextMenuStrip.Refresh();

        m_menu = new ContextMenuStrip();
        m_menu.Renderer = new ArrowRenderer();
        m_menu.AllowTransparency = true;
        m_menu.Opacity = 0.8;

        m_menu.BackColor = Color.Black;
}

因为ToolStripDropDownMenu that hosts/lists the sub items or DropDownItems is not the same object that the ContextMenuStrip inherits. Hence you need to apply the same settings for each sub menu or DropDown

SetTrayMenu()应该做的:

internal void SetTrayMenu()
{
    if (m_menu != null && notifyIcon.ContextMenuStrip != null)
        //Why?
        notifyIcon.ContextMenuStrip.Refresh();
    else
    {
        m_menu = new ContextMenuStrip
        {
            Renderer = new ArrowRenderer(),
            AllowTransparency = true,
            Opacity = 0.8,
        };

        foreach (var dd in m_menu.Items.OfType<ToolStripMenuItem>()
            .Where(x => x.HasDropDown))
        {
            var ddm = dd.DropDown as ToolStripDropDownMenu;

            if (ddm != null)
            {
                ddm.AllowTransparency = true;
                ddm.Opacity = 0.8;
            }
        }

        m_menu.BackColor = Color.Black;
    }
}

之前

之后

注:此处不透明度=0.5。