C# 如何更改 .NET 中菜单条和菜单的默认背景颜色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/617224/
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
How do you change the default background color for menustrips and menus in .NET?
提问by Jason Roberts
These days with Visual C# (Visual C# 2008 specifically) it seems that the default color for menustrips and menus is blue, which to me looks really strange and is something that I'd really like to override. I'm guessing that Visual Studio is picking up this blue color from my selected system theme or something, however no other Windows app running on my system has this blue color so I don't know why my .NET apps would have to have it. ;)
这些天使用 Visual C#(特别是 Visual C# 2008)似乎菜单条和菜单的默认颜色是蓝色,对我来说这看起来很奇怪,而且我真的很想覆盖它。我猜 Visual Studio 正在从我选择的系统主题或其他东西中选择这种蓝色,但是在我的系统上运行的其他 Windows 应用程序没有这种蓝色,所以我不知道为什么我的 .NET 应用程序必须拥有它. ;)
Anyway, I noticed that if I create an application using an older version of Visual Studio (Visual Studio.NET), the default background color for menustrips and menus is the standard gray that you'd expect to see. This is one solution to the problem I suppose, but it seems like kind of a stupid one and I'd really like to find a way to override it in the current version of Visual C#.
无论如何,我注意到如果我使用旧版本的 Visual Studio (Visual Studio.NET) 创建应用程序,则菜单条和菜单的默认背景颜色是您希望看到的标准灰色。这是我认为的问题的一种解决方案,但它似乎有点愚蠢,我真的很想找到一种方法来在当前版本的 Visual C# 中覆盖它。
采纳答案by snarf
DotNet 1.x didn't have a MenuStrip, and used a standard Windows menu.
DotNet 1.x 没有 MenuStrip,而是使用标准的 Windows 菜单。
DotNet versions 2.0 and up have the MenuStrip, and VS 7 and up removes the MainMenu from the toolbox and replaces it with a MenuStrip, which uses the Office Xp2003 theme, hence the blue color scheme for the MenuStrip and ToolStrip.
DotNet 2.0 及更高版本具有 MenuStrip,而 VS 7 及更高版本从工具箱中删除 MainMenu 并将其替换为 MenuStrip,它使用 Office 经验值2003 主题,因此 MenuStrip 和 ToolStrip 使用蓝色配色方案。
The MainMenu can still be added to the toolbox for a standard Windows menu.
MainMenu 仍然可以添加到标准 Windows 菜单的工具箱中。
回答by BFree
Have you tried setting the back color of your menuStrip? You should be able to do something like:
您是否尝试过设置 menuStrip 的背景颜色?您应该能够执行以下操作:
this.menuStrip1.BackColor = Color.FromKnownColor(KnownColor.Control);
回答by snarf
Set the "RenderMode" option of the MenuStrip to "System" instead of "managerRenderMode"
将MenuStrip的“ RenderMode”选项设置为“ System”而不是“managerRenderMode”
:)
:)
回答by snarf
You can use the render mode but that only sets the system theme to default. .net allows you to change the backcolor and the for color but when you hover over it It still shows the default colors. Heres a way around that...
您可以使用渲染模式,但这只会将系统主题设置为默认值。.net 允许您更改背景色和 for 颜色,但是当您将鼠标悬停在它上面时,它仍然显示默认颜色。这是一种解决方法......
his is very simple to accomplish by using the "DropDownOpening" , "DropDownClosed", and "MouseEnter" events.
通过使用 "DropDownOpening" 、 "DropDownClosed" 和 "MouseEnter" 事件来完成它是非常简单的。
private void fileToolStripMenuItem_DropDownOpening(object sender, EventArgs e){
// When the user clicks on "File" it will change to red.
fileToolStripMenuItem.ForeColor = Color.Red;
}
private void testToolStripMenuItem_MouseEnter(object sender, EventArgs e){
// When the user hovers over a child of "file" called "test", "file" turns orange.
fileToolStripMenuItem.ForeColor = Color.Orange;
}
private void test2ToolStripMenuItem_MouseEnter(object sender, EventArgs e){
// When the user hovers on a child of "file" called "test2", "file" turns blue.
fileToolStripMenuItem.ForeColor = Color.Blue;
}
private void fileToolStripMenuItem_DropDownClosed(object sender, EventArgs e){
// When the user leaves the "file" menu, it gets restored back to black.
fileToolStripMenuItem.ForeColor = Color.Black;
}
回答by magnusi
You can make own Renderer
您可以制作自己的渲染器
public class BrowserMenuRenderer : ToolStripProfessionalRenderer
{
public BrowserMenuRenderer() : base(new BrowserColors()) {}
}
public class BrowserColors : ProfessionalColorTable
{
public override Color MenuItemSelected
{
get { return Color.FromArgb(30, 30, 30); }
}
public override Color MenuItemBorder
{
get { return Color.FromArgb(30, 30, 30); }
}
public override Color MenuItemSelectedGradientBegin
{
get { return Color.FromArgb(30, 30, 30); }
}
public override Color MenuItemSelectedGradientEnd
{
get { return Color.FromArgb(30, 30, 30); }
}
}
Then you just set your menustrip's renderer as YourRenderer
然后你只需将菜单条的渲染器设置为 YourRenderer