更改Winforms菜单下拉列表的边框颜色

时间:2015-08-31 09:17:15

标签: c# winforms toolstripmenu

是否可以更改工具条菜单下拉列表的边框颜色。

在下面的示例中,我希望下拉菜单中有1种颜色(蓝色),但当前没有移动白色边框,但保持主菜单(“我的菜单”)项目为白色。

有什么想法吗?

enter image description here

4 个答案:

答案 0 :(得分:12)

Is it possible to change the border color of a toolstrip menu dropdown list.

是。继承自ProfessionalColorTable的类按预期工作:

class MenuColorTable : ProfessionalColorTable
{
    public MenuColorTable()
    {
        // see notes
        base.UseSystemColors = false;
    }
    public override System.Drawing.Color MenuBorder
    {
        get{return Color.Fuchsia;}
    }
    public override System.Drawing.Color MenuItemBorder
    {
        get{return Color.DarkViolet;}
    }
    public override Color MenuItemSelected
    {
        get { return Color.Cornsilk;}
    }
    public override Color MenuItemSelectedGradientBegin
    {
        get{return Color.LawnGreen;}
    }
    public override Color MenuItemSelectedGradientEnd
    {
        get { return Color.MediumSeaGreen; }
    }
    public override Color MenuStripGradientBegin
    {
        get { return Color.AliceBlue; }
    }
    public override Color MenuStripGradientEnd
    {
        get { return Color.DodgerBlue; }
    }
}

在表单加载中:

menuStrip1.Renderer = new ToolStripProfessionalRenderer(new MenuColorTable());

如果未启用视觉样式,则不会使用所有颜色表项,而是使用某些SystemColors。您可以在Main()中启用视觉样式:

// must be done before any UI elements are used
Application.EnableVisualStyles();

您可能还想要禁用系统颜色,如ctor中所示。无论是否启用了视觉样式,默认值都应该为false,但是可能还有其它东西改变了它?

base.UseSystemColors = false;

要实现所有颜色表中的渲染元素,EnableVisualStyles()UseSystemColors = false;都必须到位,否则只会使用一些。 (尽管如此,MenuBorder似乎无论如何都有用。)否则,结果如预期的那样:

enter image description here

菜单渐变从AliceBlue转到DodgerBlue;一个,鼠标悬停在它上面,使用LawnGreen到MediumSeaGreen的从上到下的渐变(鼠标未显示)。

enter image description here

打开时,菜单边框为Fuschia(mmmm,舒缓!)

enter image description here

将鼠标悬停在其中一个项目上(鼠标未显示),该项目使用MenuItemSelected颜色,即Consilk。

如果您在覆盖工作时遇到问题,请检查您是否使用了正确的覆盖(或者他们的意思是名称所暗示的,有些人首先会误导)。

您可能还会检查您是否使用MenuStrip作为菜单,Net确实有另一个(较旧的)菜单类,但您必须搜索才能找到它。您也可以更改或禁用任何主题,以查看是否可能导致不利影响。

答案 1 :(得分:6)

要更改边框颜色,只需按照接受的答案中描述的Plutonix解决方案即可。 但要删除项目和菜单边框之间的白色边框,您应该遵循以下解决方案之一:

解决方案1 ​​

您可以通过实现继承ProfessionalColorTable并覆盖正确属性的自定义颜色表来实现。为此,请按照下列步骤操作:

<强>步骤

  1. 在表单上放置一个ToolStrip,并将DropDownButton及其子项添加到其中,并将子项的ForeColor设置为白色。
  2. 创建CustomColorTable类继承自ProfessionalColorTable
  3. 覆盖ImageMarginGradientBegin,ImageMarginGradientMiddle,ImageMarginGradientEnd,ToolStripDropDownBackground并返回您想要的颜色(蓝色).from
  4. 在表单加载事件中设置ToolStripManager的呈现器属性,以使用使用CustomColorTable的ToolStripProfessionalRenderer。
  5. CustomColorTable代码

    public class CustomColorTable:ProfessionalColorTable
    {
        public override Color ImageMarginGradientBegin
        {
            get
            {
                return Color.MidnightBlue;
            }
        }
    
        public override Color ImageMarginGradientMiddle
        {
            get
            {
                return Color.MidnightBlue;
            }
        }
    
        public override Color ImageMarginGradientEnd
        {
            get
            {
                return Color.MidnightBlue; 
            }
        }
    
        public override Color ToolStripDropDownBackground
        {
            get
            {
                return Color.MidnightBlue;
            }
        }
    }
    

    表单加载代码

    private void Form_Load(object sender, EventArgs e)
    {
        ToolStripManager.Renderer = new ToolStripProfessionalRenderer(new CustomColorTable());
    }
    

    <强>截图

    这是正常的截图

    enter image description here

    这是一个2倍放大的屏幕截图:

    enter image description here

    解决方案2

    如果您不想在菜单项中使用图片,还有一种替代解决方案适用。在这种情况下,找到dropdown按钮的DropDown属性并将其强制转换为ToolStripDropDownMenu,然后将其ShowImageMargin属性设置为false,将其BackColor设置为所需颜色(蓝色)。

    private void Form_Load(object sender, EventArgs e)
    {
        //The item with text "My Menu" in your sample
        var dropDownMenu = (ToolStripDropDownMenu)this.myMenuToolStripDropDownButton1.DropDown;
        dropDownMenu.ShowImageMargin = false;
        dropDownMenu.BackColor = Color.Navy;
    }
    

    enter image description here

答案 2 :(得分:0)

我现在无法制作演示应用程序,但在本文中截图 http://www.vbforums.com/showthread.php?596563-100-Customizable-MenuStrip-ToolStrip-StatusStrip-including-common-presets证明我们可以改变边框颜色。

那家伙开发了一些库,但它基于标准的ToolStrip实现。

我希望这个链接会有所帮助。

答案 3 :(得分:0)

您可以为项目提供负边距以掩盖边框。

相关问题