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

标签 c# winforms toolstripmenu

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

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

有什么想法吗?

enter image description here

最佳答案

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

是的。从 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

打开时,菜单边框是紫红色(嗯,舒缓!)

enter image description here

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

如果您在使用覆盖时遇到问题,请检查您是否使用了正确的覆盖(或者它们的含义是否如名称所暗示的那样,有些一开始会产生误导)。

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

关于c# - 更改Winforms菜单下拉列表的边框颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32307778/

相关文章:

c# - 存在异常的继承异常

C# 窗体 : Control locked but locked propety is set to false

c# - 如何禁用面板中的水平滚动条

c# 从另一个表单更改已经打开的表单?

c# - 将热键/快捷方式文本放在winforms中的工具条菜单项旁边

c# - Expression.Compile 在 Monotouch 上做了什么?

c# - 通过 Entity Framework 分析查询

c# - 您可以在自定义渲染器中更改 ToolStripMenuItem 的字体吗

c# - Topshelf 服务未启动 访问被拒绝

c# - "Beta: Use Unicode UTF-8 for worldwide language support"究竟做了什么?