C# 如何更改菜单悬停颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9260303/
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 to change menu hover color
提问by Sreekumar P
How to change the Hover (mouse over) color of a Windows application menu?
如何更改 Windows 应用程序菜单的悬停(鼠标悬停)颜色?
Any method in C# ?
C# 中的任何方法?
OR
或者
Any way by using Windows API (DllImport) ?
任何使用 Windows API (DllImport) 的方法?
See image :
见图片:


采纳答案by Hans Passant
You are using the MenuStrip class. You can override its renderer. Here's an example, pick your own colors please.
您正在使用 MenuStrip 类。您可以覆盖其渲染器。这是一个例子,请选择你自己的颜色。
public partial class Form1 : Form {
public Form1() {
InitializeComponent();
menuStrip1.Renderer = new MyRenderer();
}
private class MyRenderer : ToolStripProfessionalRenderer {
public MyRenderer() : base(new MyColors()) {}
}
private class MyColors : ProfessionalColorTable {
public override Color MenuItemSelected {
get { return Color.Yellow; }
}
public override Color MenuItemSelectedGradientBegin {
get { return Color.Orange; }
}
public override Color MenuItemSelectedGradientEnd {
get { return Color.Yellow; }
}
}
}
Other properties of ProfessionalColorTablecontrol other color elements.
ProfessionalColorTable 的其他属性控制其他颜色元素。
回答by Cesar BA
For changing the mouse-over border color (on items) use this:
要更改鼠标悬停边框颜色(在项目上),请使用以下命令:
public override Color MenuItemBorder
{
get { return Color.Green; }
}
回答by Be Maneuver
You can also make it transparent (invisible):
您还可以使其透明(不可见):
get { return Color.Transparent; }
回答by ThomAce
I had the similar question and I went through many articles, many forums, but have not found the perfect answer for my questions. I not only had the problem with the hover of the dropdown menu elements, but the background and overally the layout and how could I add sub-elements programmatically. Then I found how MenuStrip can be customized quiet easily in Stackoverflow forums, however I still got the issue with the dropdowns. Then I turend out by myself that ContextMenuStip has the properties to achieve the goals. It is easy to add any MenuStrip a ContextMenuStrip as a DropDown menu. Ohh, yes: The beauty in this is that you don't need to use any special components.
我有类似的问题,我浏览了很多文章,很多论坛,但没有找到我的问题的完美答案。我不仅有下拉菜单元素悬停的问题,还有背景和整体布局以及如何以编程方式添加子元素的问题。然后我发现如何在 Stackoverflow 论坛中轻松自定义 MenuStrip,但是我仍然遇到下拉列表的问题。然后我自己发现 ContextMenuStip 具有实现目标的属性。很容易将任何 MenuStrip 添加到 ContextMenuStrip 作为下拉菜单。哦,是的:其中的美妙之处在于您不需要使用任何特殊组件。
So, the steps are the following:
因此,步骤如下:
- You need to have a color table.
- You must use it on your MenuStrip.
- ToolStripMenuItems on your MenuStrip must has a ContextMenuStrip as DropDown.
- Through the ToolStripMenuItems.Items[?].DropDownItems function, you can easily manipulate the sub-elements that appears as drop-down elements.
- 你需要有一个颜色表。
- 您必须在 MenuStrip 上使用它。
- MenuStrip 上的 ToolStripMenuItems 必须有一个 ContextMenuStrip 作为 DropDown。
- 通过 ToolStripMenuItems.Items[?].DropDownItems 函数,您可以轻松操作显示为下拉元素的子元素。
1.- The color tables:
1.- 颜色表:
public class submenuColorTable : ProfessionalColorTable
{
public override Color MenuItemSelected
{
get { return ColorTranslator.FromHtml("#302E2D"); }
}
public override Color MenuItemBorder
{
get { return Color.Silver; }
}
public override Color ToolStripDropDownBackground
{
get { return ColorTranslator.FromHtml("#21201F"); }
}
public override Color ToolStripContentPanelGradientBegin
{
get { return ColorTranslator.FromHtml("#21201F"); }
}
}
public class LeftMenuColorTable : ProfessionalColorTable
{
public override Color MenuItemBorder
{
get { return ColorTranslator.FromHtml("#BAB9B9"); }
}
public override Color MenuBorder //added for changing the menu border
{
get { return Color.Silver; }
}
public override Color MenuItemPressedGradientBegin
{
get { return ColorTranslator.FromHtml("#4C4A48"); }
}
public override Color MenuItemPressedGradientEnd
{
get { return ColorTranslator.FromHtml("#5F5D5B"); }
}
public override Color ToolStripBorder
{
get { return ColorTranslator.FromHtml("#4C4A48"); }
}
public override Color MenuItemSelectedGradientBegin
{
get { return ColorTranslator.FromHtml("#4C4A48"); }
}
public override Color MenuItemSelectedGradientEnd
{
get { return ColorTranslator.FromHtml("#5F5D5B"); }
}
public override Color ToolStripDropDownBackground
{
get { return ColorTranslator.FromHtml("#404040"); }
}
public override Color ToolStripGradientBegin
{
get { return ColorTranslator.FromHtml("#404040"); }
}
public override Color ToolStripGradientEnd
{
get { return ColorTranslator.FromHtml("#404040"); }
}
public override Color ToolStripGradientMiddle
{
get { return ColorTranslator.FromHtml("#404040"); }
}
}
2.- Using it on MenuStrip:
2.- 在 MenuStrip 上使用它:
menuStrip.Renderer = new ToolStripProfessionalRenderer(new LeftMenuColorTable());
3.- Adding ContextMenuStrip to the menu element programmatically
3.- 以编程方式将 ContextMenuStrip 添加到菜单元素
ContextMenuStrip CMS = new ContextMenuStrip()
{
Renderer = new ToolStripProfessionalRenderer(new submenuColorTable()),
ShowImageMargin = false
};
ToolStripMenuItem TSMI = new ToolStripMenuItem("Button name")
{
BackColor = sampleMenuItem.BackColor,
ForeColor = sampleMenuItem.ForeColor,
Font = sampleMenuItem.Font,
Margin = sampleMenuItem.Margin,
Padding = sampleMenuItem.Padding,
Size = sampleMenuItem.Size,
TextAlign = sampleMenuItem.TextAlign,
DropDown = CMS
};
menuStrip.Items.Add(TSMI);
4.- Manipulate the sub-elements
4.- 操作子元素
Here you can manipulate (for example: add) the elements of the drop-down menu. The color, size and other properties are just used this way for testing. You can use constant or different values. ("i" is the menu button index you want to add sub-entries)
您可以在此处操作(例如:添加)下拉菜单的元素。颜色、大小和其他属性只是通过这种方式进行测试。您可以使用常量或不同的值。(“i”是您要添加子条目的菜单按钮索引)
ToolStripMenuItem newItem = new ToolStripMenuItem("Button Name", null, ToolStripMenuItem_Click)
{
Text = "Button Name",
BackColor = toolStripMenuItem01.BackColor,
ForeColor = toolStripMenuItem01.ForeColor,
Font = toolStripMenuItem01.Font,
Margin = toolStripMenuItem01.Margin,
Padding = toolStripMenuItem01.Padding,
Size = toolStripMenuItem01.Size
};
((ToolStripMenuItem)menuStrip.Items[i]).DropDownItems.Add(newItem);
The result is in my case the following:
在我的情况下,结果如下:
This might be useful for others. Thanks for reading! Happy coding! :)
这可能对其他人有用。谢谢阅读!快乐编码!:)

