C# 如何更改 MenuStrip 的外观

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/13235627/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 07:51:29  来源:igfitidea点击:

How to change the appearance of a MenuStrip

c#borderitemsappearancemenustrip

提问by AnDr3yy

I add a MenuStripin my app and is add on ManagerRenderModeat Render Mode. The problem is with the appearance, look offal. Look at those two photos, I want to change that white border of submenus in transparent, that blue rectangule that look offal on gray for the menu and for the submenu in dark gray (and his border that is a dark blue) and the border white of menu when is selected. How I can do this ?

MenuStrip在我的应用程序中添加了一个,并ManagerRenderModeRender Mode. 问题在于外观,看起来内脏。看那两张照片,我想将子菜单的白色边框更改为透明,蓝色矩形看起来像灰色的菜单和深灰色的子菜单(以及他的深蓝色边框)和白色边框选择时的菜单。我怎么能做到这一点?

BackColoris: 36; 36; 36and ForeColoris LightGray.

BackColor是:36; 36; 36并且ForeColorLightGray

enter image description here

在此处输入图片说明

enter image description here

在此处输入图片说明

I managed to change the blue rectangle, the white rectangle when the option is selected, the blue rectangle when I select an option of submenus, but I don't know how to change the white border, please help..

我设法更改了蓝色矩形,选择选项时的白色矩形,选择子菜单选项时的蓝色矩形,但我不知道如何更改white border,请帮助..

Here is the code so far...

这是到目前为止的代码......

        Color culoare = Color.FromArgb(20, 20, 20);
        Color culoare1 = Color.FromArgb(36, 36, 36);

        public override Color MenuItemSelected
        {
            get { return culoare; }
        }

        public override Color MenuItemBorder
        {
            get { return culoare; }
        }

        public override Color MenuItemSelectedGradientBegin
        { 
            get { return culoare; } 
        }

        public override Color MenuItemSelectedGradientEnd
        { 
            get { return culoare; } 
        }

        public override Color MenuItemPressedGradientBegin
        { 
            get { return culoare; }
        }

        public override Color MenuItemPressedGradientEnd
        {
            get { return culoare; }
        }

        public override Color MenuBorder
        {
            get { return culoare; }
        }

采纳答案by Pondidum

You can do this by creating your own ColorTable, and overriding the properties you wish to change the colour of:

您可以通过创建自己的 ColorTable 并覆盖要更改其颜色的属性来实现此目的:

public  class TestColorTable : ProfessionalColorTable
{
    public override Color MenuItemSelected
    {
        get { return Color.Red; }
    }

    public override Color MenuBorder  //added for changing the menu border
    {
        get { return Color.Green; }
    }

}

You would use it like this:

你会像这样使用它:

private void Form1_Load(object sender, EventArgs e)
{
    menuStrip1.Renderer = new ToolStripProfessionalRenderer(new TestColorTable());
}

回答by Matthew Layton

Your approach is incorrect. You do not style menus and toolstrips using forecolor/backcolor.

你的做法是错误的。您不使用前景色/背景色来设置菜单和工具条的样式。

Take a look at ToolStripProfessionalRenderer

看看ToolStripProfessionalRenderer

Example on how to use this

关于如何使用它的示例

public class MyToolStripRenderer : ToolStripProfessionalRenderer
{
    /* override styling/drawing here */
}

MenuStrip strip = new MenuStrip();

strip.Renderer = new MyToolStripRenderer();

//this will set RenderMode to "Custom"

consider using this example on CodeProjectas some research.

考虑在 CodeProject 上使用这个例子作为一些研究。

Better still, VBForums have loads of them, already implemented (in the usual Luna, Office, Windows, Visual Studio styles!)

更好的是,VBForums 有很多它们已经实现了(在通常的 Luna、Office、Windows、Visual Studio 样式中!)

http://www.vbforums.com/showthread.php?596563-100-Customizable-MenuStrip-ToolStrip-StatusStrip-including-common-presets

http://www.vbforums.com/showthread.php?596563-100-Customizable-MenuStrip-ToolStrip-StatusStrip-including-common-presets

If you simply want to chaneg the colors...use Pondidum's answer! It involves less work!

如果你只是想改变颜色......使用 Pondidum 的答案!它涉及更少的工作!