Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
368 views
in Technique[技术] by (71.8m points)

c# - Change the border color of Winforms menu dropdown list

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?

enter image description here

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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:

enter image description here

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).

enter image description here

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

enter image description here

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...