变化的WinForms菜单下拉列表中选择边框颜色 [英] Change the border color of Winforms menu dropdown list

查看:388
本文介绍了变化的WinForms菜单下拉列表中选择边框颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有可能改变一个工具条菜单下拉列表的边框颜色。



在我的样本下面我想在下拉菜单中有1种颜色(蓝色)没有白色边框正在displated,但保持主菜单(我的菜单)项白色。



任何想法?




解决方案
>是否有可能改变边框颜色一个工具条菜单下拉列表中。



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

 类MenuColorTable: ProfessionalColorTable 
{
公共MenuColorTable()
{
//见注
base.UseSystemColors = FALSE;
}
公众覆盖的System.Drawing.Color MenuBorder
{
{返回Color.Fuchsia;}
}
公众覆盖的System.Drawing.Color MenuItemBorder
{
{返回Color.DarkViolet;}
}
公众覆盖颜色MenuItemSelected
{
{返回Color.Cornsilk;}
}
公众覆盖颜色MenuItemSelectedGradientBegin
{
{返回Color.LawnGreen;}
}
公众覆盖颜色MenuItemSelectedGradientEnd
{
{返回Color.MediumSeaGreen; }
}
公众覆盖颜色MenuStripGradientBegin
{
{返回Color.AliceBlue; }
}
公众覆盖颜色MenuStripGradientEnd
{
{返回Color.DodgerBlue; }
}
}

在窗体加载:

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

如果视觉样式未开启时,并非所有的颜色表项将被使用,有些 systemColors中将被用来代替。您启用视觉样式的Main()

  //必须做过任何UI元素都使用
Application.EnableVisualStyles()之前;

您可能还希望禁用系统颜色在构造函数中。默认的应该是假的视觉样式是否已启用或没有,但也许别的东西发生了变化呢?



  base.UseSystemColors = FALSE ; 



两者 EnableVisualStyles() UseSystemColors = FALSE; 必须到位的所有的要实现你的颜色表渲染的元素,否则只能使用/一番。 (虽然, MenuBorder 似乎确实不管工作是什么。)否则,结果不出所料:



<一个HREF =htt​​p://i.stack.imgur.com/MRuwd.jpg>



菜单梯度艾莉斯蓝去宝蓝;一个的项目的鼠标在它使用了顶到草绿的底部梯度暗海藻(未显示鼠标)。





当打开时,菜单边境洋红色(MMMM,舒缓!)





随着鼠标的项目之一(未显示鼠标),该项目使用了 MenuItemSelected 颜色,是Consilk。



如果你有麻烦您的覆盖工作,请检查您使用是正确的(或者说他们的意思顾名思义,有些是误导首先)。



您可能还需要检查你使用的是的MenuStrip 的菜单,网络确实有另一个(旧的)菜单类,但是你必须去寻找找到它。您也可以更改或禁用任何主题,看看是否可能会导致不良影响。


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

In my sample below I would like the dropdown menu to have 1 color (blue) without the white border currently being displated, but keeping the main menu ('My Menu') item white.

Any ideas?

解决方案

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

Yes. A class which inherits from ProfessionalColorTable works as expected:

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; }
    }
}

In form load:

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

If visual styles are not turned on, not all the color table items will be used and some SystemColors will be used instead. You enable visual styles in Main():

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

You may want to also disable system colors as shown in the ctor. The default should be false whether visual styles are enabled or not, but maybe something else has changed it?

base.UseSystemColors = false;

Both EnableVisualStyles() and UseSystemColors = false; have to be in place for all the rendering elements in your color table to be implemented, otherwise only some are used. (Though, MenuBorder does seem to work no matter what.) Otherwise, results are as expected:

The menu gradient goes from AliceBlue to DodgerBlue; an item with the mouse over it uses a top to bottom gradient of LawnGreen to MediumSeaGreen (mouse not shown).

When open, the menu border is Fuschia (mmmm, soothing!)

With the mouse over one of the items (mouse not shown), the item uses the MenuItemSelected color which was Consilk.

If you are having trouble getting your overrides to work, check that you are using the right ones (or that they mean what the name implies, some are misleading at first).

You might also check that you are using a MenuStrip for the menu, Net does have another (older) menu class though you have to go searching to find it. You might also change or disable any Theme to see if that might be causing adverse effects.

这篇关于变化的WinForms菜单下拉列表中选择边框颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆