如何更改Windows应用程序菜单的悬停(鼠标悬停)颜色?
C#中的任何方法?
OR
使用Windows API(DllImport)的任何方式?
见图:
答案 0 :(得分:56)
您正在使用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; }
}
}
}
ProfessionalColorTable的其他属性控制其他颜色元素。
答案 1 :(得分:8)
要更改鼠标悬停边框颜色(在项目上),请使用:
public override Color MenuItemBorder
{
get { return Color.Green; }
}
答案 2 :(得分:4)
我有类似的问题,我浏览了许多文章,许多论坛,但没有找到我问题的完美答案。我不仅在下拉菜单元素的悬停上遇到问题,而且在背景和总体布局上还有问题,以及如何以编程方式添加子元素。然后,我发现如何在Stackoverflow论坛中轻松轻松地自定义MenuStrip,但是下拉菜单仍然存在问题。然后,我自己弄清楚ContextMenuStip具有实现目标的属性。将ContextMenuStrip的任何MenuStrip添加为下拉菜单很容易。哦,是的:这样做的好处是您不需要使用任何特殊的组件。
因此,步骤如下:
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.-在MenuStrip上使用它:
menuStrip.Renderer = new ToolStripProfessionalRenderer(new LeftMenuColorTable());
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.-处理子元素
您可以在此处操纵(例如:添加)下拉菜单的元素。颜色,大小和其他属性只是通过这种方式进行测试。您可以使用常数或不同的值。 (“ 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);
在我看来,结果如下:
这可能对其他人有用。谢谢阅读!编码愉快! :)
答案 3 :(得分:2)
你也可以让它透明(不可见):
get { return Color.Transparent; }