MenuItem 的颜色变化
Color change for MenuItem
我正在编写一个备份工具。在我的工具之上,我有一个包含两个工具条菜单项的菜单条。我根据我的期望稍微改变了颜色。未聚焦菜单看起来不错:
当我现在单击菜单项 "File" 打开上下文菜单时,颜色变为白色,我无法再阅读文本:
谁能告诉我在哪里可以改变这种行为?我使用 Visual Studio 2013 Ultimate,Windows Forms Application,代码在 C# 中。
代码如下:
//
// 初始化 menuStrip1
//
这个.menuStrip1.BackColor = System.Drawing.Color.MediumBlue;
这个。menuStrip1.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Center;
这个。menuStrip1.Font = new System.Drawing.Font("Segoe UI Semilight",
15.75F,System.Drawing.FontStyle.Regular,<br>
System.Drawing.GraphicsUnit.Point, ((字节)(0)));
这个。menuStrip1.Items.AddRange(新System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.helpToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.MinimumSize = new System.Drawing.Size(0, 40);
这个.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1056, 40);
这个.menuStrip1.TabIndex = 77;
这个.menuStrip1.Text = "menuStrip1";
//
// 初始化 fileToolStripMenuItem 并添加到 menuStrip1
//
this.fileToolStripMenuItem.DropDownItems.AddRange(新
System.Windows.Forms.ToolStripItem[] {
this.saveToolStripMenuItem,
this.saveAsToolStripMenuItem,
this.loadToolStripMenuItem});
this.fileToolStripMenuItem.Font = new System.Drawing.Font("Calibri Light",
15.75F,System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((字节)(0)));
this.fileToolStripMenuItem.前景色=
System.Drawing.SystemColors.ControlLightLight;
this.fileToolStripMenuItem.姓名="fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 36);
this.fileToolStripMenuItem.文字="File";
this.fileToolStripMenuItem.点击+=新建System.EventHandler
(this.fileToolStripMenuItem_点击);
//
// 初始化 saveToolStripMenuItem 并添加到 fileToolStripMenuItem
//
this.saveToolStripMenuItem.BackColor = System.Drawing.Color.MediumBlue;
this.saveToolStripMenuItem.前景色=
System.Drawing.SystemColors.ControlLightLight;
this.saveToolStripMenuItem.姓名="saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(166, 30);
this.saveToolStripMenuItem.文字="Save";
this.saveToolStripMenuItem.点击+=新建System.EventHandler
(this.saveToolStripMenuItem_点击);
//
默认情况下,此功能不是开箱即用的。您需要为您的工具条创建自定义 Renderer
才能实现此目的。
创建一个继承自 ToolStripProfessionalRenderer
-
的 class
private class BlueRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
Color c = Color.MediumBlue;
using (SolidBrush brush = new SolidBrush(c))
e.Graphics.FillRectangle(brush, rc);
}
}
并将此呈现器附加到表单构造函数中的菜单条 -
public Form1()
{
InitializeComponent();
menuStrip1.Renderer = new BlueRenderer();
}
您可以创建自己的 ProfessionalColorTable
并覆盖其属性:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
menuStrip1.Renderer = new ToolStripProfessionalRenderer(new MyColorTable());
}
}
public class MyColorTable : ProfessionalColorTable
{
public override Color ToolStripDropDownBackground
{
get
{
return Color.Blue;
}
}
public override Color ImageMarginGradientBegin
{
get
{
return Color.Blue;
}
}
public override Color ImageMarginGradientMiddle
{
get
{
return Color.Blue;
}
}
public override Color ImageMarginGradientEnd
{
get
{
return Color.Blue;
}
}
public override Color MenuBorder
{
get
{
return Color.Black;
}
}
public override Color MenuItemBorder
{
get
{
return Color.Black;
}
}
public override Color MenuItemSelected
{
get
{
return Color.Navy;
}
}
public override Color MenuStripGradientBegin
{
get
{
return Color.Blue;
}
}
public override Color MenuStripGradientEnd
{
get
{
return Color.Blue;
}
}
public override Color MenuItemSelectedGradientBegin
{
get
{
return Color.Navy;
}
}
public override Color MenuItemSelectedGradientEnd
{
get
{
return Color.Navy;
}
}
public override Color MenuItemPressedGradientBegin
{
get
{
return Color.Blue;
}
}
public override Color MenuItemPressedGradientEnd
{
get
{
return Color.Blue;
}
}
}
}
这是上面代码的结果:
我正在编写一个备份工具。在我的工具之上,我有一个包含两个工具条菜单项的菜单条。我根据我的期望稍微改变了颜色。未聚焦菜单看起来不错:
当我现在单击菜单项 "File" 打开上下文菜单时,颜色变为白色,我无法再阅读文本:
谁能告诉我在哪里可以改变这种行为?我使用 Visual Studio 2013 Ultimate,Windows Forms Application,代码在 C# 中。
代码如下:
//
// 初始化 menuStrip1
//
这个.menuStrip1.BackColor = System.Drawing.Color.MediumBlue;
这个。menuStrip1.BackgroundImageLayout =
System.Windows.Forms.ImageLayout.Center;
这个。menuStrip1.Font = new System.Drawing.Font("Segoe UI Semilight",
15.75F,System.Drawing.FontStyle.Regular,<br>
System.Drawing.GraphicsUnit.Point, ((字节)(0)));
这个。menuStrip1.Items.AddRange(新System.Windows.Forms.ToolStripItem[] {
this.fileToolStripMenuItem,
this.helpToolStripMenuItem});
this.menuStrip1.Location = new System.Drawing.Point(0, 0);
this.menuStrip1.MinimumSize = new System.Drawing.Size(0, 40);
这个.menuStrip1.Name = "menuStrip1";
this.menuStrip1.Size = new System.Drawing.Size(1056, 40);
这个.menuStrip1.TabIndex = 77;
这个.menuStrip1.Text = "menuStrip1";
//
// 初始化 fileToolStripMenuItem 并添加到 menuStrip1
//
this.fileToolStripMenuItem.DropDownItems.AddRange(新
System.Windows.Forms.ToolStripItem[] {
this.saveToolStripMenuItem,
this.saveAsToolStripMenuItem,
this.loadToolStripMenuItem});
this.fileToolStripMenuItem.Font = new System.Drawing.Font("Calibri Light",
15.75F,System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((字节)(0)));
this.fileToolStripMenuItem.前景色=
System.Drawing.SystemColors.ControlLightLight;
this.fileToolStripMenuItem.姓名="fileToolStripMenuItem";
this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 36);
this.fileToolStripMenuItem.文字="File";
this.fileToolStripMenuItem.点击+=新建System.EventHandler
(this.fileToolStripMenuItem_点击);
//
// 初始化 saveToolStripMenuItem 并添加到 fileToolStripMenuItem
//
this.saveToolStripMenuItem.BackColor = System.Drawing.Color.MediumBlue;
this.saveToolStripMenuItem.前景色=
System.Drawing.SystemColors.ControlLightLight;
this.saveToolStripMenuItem.姓名="saveToolStripMenuItem";
this.saveToolStripMenuItem.Size = new System.Drawing.Size(166, 30);
this.saveToolStripMenuItem.文字="Save";
this.saveToolStripMenuItem.点击+=新建System.EventHandler
(this.saveToolStripMenuItem_点击);
//
默认情况下,此功能不是开箱即用的。您需要为您的工具条创建自定义 Renderer
才能实现此目的。
创建一个继承自 ToolStripProfessionalRenderer
-
private class BlueRenderer : ToolStripProfessionalRenderer
{
protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
{
Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
Color c = Color.MediumBlue;
using (SolidBrush brush = new SolidBrush(c))
e.Graphics.FillRectangle(brush, rc);
}
}
并将此呈现器附加到表单构造函数中的菜单条 -
public Form1()
{
InitializeComponent();
menuStrip1.Renderer = new BlueRenderer();
}
您可以创建自己的 ProfessionalColorTable
并覆盖其属性:
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
menuStrip1.Renderer = new ToolStripProfessionalRenderer(new MyColorTable());
}
}
public class MyColorTable : ProfessionalColorTable
{
public override Color ToolStripDropDownBackground
{
get
{
return Color.Blue;
}
}
public override Color ImageMarginGradientBegin
{
get
{
return Color.Blue;
}
}
public override Color ImageMarginGradientMiddle
{
get
{
return Color.Blue;
}
}
public override Color ImageMarginGradientEnd
{
get
{
return Color.Blue;
}
}
public override Color MenuBorder
{
get
{
return Color.Black;
}
}
public override Color MenuItemBorder
{
get
{
return Color.Black;
}
}
public override Color MenuItemSelected
{
get
{
return Color.Navy;
}
}
public override Color MenuStripGradientBegin
{
get
{
return Color.Blue;
}
}
public override Color MenuStripGradientEnd
{
get
{
return Color.Blue;
}
}
public override Color MenuItemSelectedGradientBegin
{
get
{
return Color.Navy;
}
}
public override Color MenuItemSelectedGradientEnd
{
get
{
return Color.Navy;
}
}
public override Color MenuItemPressedGradientBegin
{
get
{
return Color.Blue;
}
}
public override Color MenuItemPressedGradientEnd
{
get
{
return Color.Blue;
}
}
}
}
这是上面代码的结果: