【问题标题】:Color change for MenuItemMenuItem 的颜色变化
【发布时间】:2016-08-14 12:42:26
【问题描述】:

我正在编写一个备份工具。在我的工具之上,我有一个包含两个工具条菜单项的菜单条。我根据我的期望稍微改变了颜色。不集中的菜单看起来很棒:

当我现在单击菜单项“文件”以打开上下文菜单时,颜色变为白色,我无法再阅读文本:

谁能告诉我在哪里可以改变这种行为?我使用 Visual Studio 2013 Ultimate,Windows 窗体应用程序,代码使用 C#。

代码如下:

// // initializing menuStrip1 // this.menuStrip1.BackColor = System.Drawing.Color.MediumBlue; this.menuStrip1.BackgroundImageLayout = System.Windows.Forms.ImageLayout.Center; this.menuStrip1.Font = new System.Drawing.Font("Segoe UI Semilight", 15.75F, System.Drawing.FontStyle.Regular,
System.Drawing.GraphicsUnit.Point, ((byte)(0))); this.menuStrip1.Items.AddRange(new 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); this.menuStrip1.Name = "menuStrip1"; this.menuStrip1.Size = new System.Drawing.Size(1056, 40); this.menuStrip1.TabIndex = 77; this.menuStrip1.Text = "menuStrip1"; // // initializing fileToolStripMenuItem and adding to menuStrip1 // this.fileToolStripMenuItem.DropDownItems.AddRange(new 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, ((byte)(0))); this.fileToolStripMenuItem.ForeColor = System.Drawing.SystemColors.ControlLightLight; this.fileToolStripMenuItem.Name = "fileToolStripMenuItem"; this.fileToolStripMenuItem.Size = new System.Drawing.Size(54, 36); this.fileToolStripMenuItem.Text = "File"; this.fileToolStripMenuItem.Click += new System.EventHandler (this.fileToolStripMenuItem_Click); // // initializing saveToolStripMenuItem and adding to fileToolStripMenuItem // this.saveToolStripMenuItem.BackColor = System.Drawing.Color.MediumBlue; this.saveToolStripMenuItem.ForeColor = System.Drawing.SystemColors.ControlLightLight; this.saveToolStripMenuItem.Name = "saveToolStripMenuItem"; this.saveToolStripMenuItem.Size = new System.Drawing.Size(166, 30); this.saveToolStripMenuItem.Text = "Save"; this.saveToolStripMenuItem.Click += new System.EventHandler (this.saveToolStripMenuItem_Click);
//

【问题讨论】:

  • 您可以发布您的颜色自定义代码吗?
  • 添加了初始化代码:)

标签: c# winforms visual-studio menustrip toolstripmenu


【解决方案1】:

您可以创建自己的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;
             }
         }
     }
}

这是上面代码的结果:

【讨论】:

  • 这实际上更简单易读 - 将颜色放在单独的类中很方便。
  • 这是一个漂亮的结果 :) 效果很好 +1
【解决方案2】:

默认情况下,此功能不可用。你需要为你的工具条创建一个自定义的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();
    }

【讨论】:

  • 哈哈,很好用,非常感谢!有趣的是,您可以默认更改所有颜色,但不能更改特定颜色...微软逻辑?!
  • 还有一个问题。现在所有州的颜色都是 MediumBlue。当我单击 menuStrip1 中的项目时,我可以有 LightBlue 吗?
  • @schmelzer-daniel - 像这样定义你的 clocr:Color c = e.Item.Selected ? Color.LightBlue : Color.MediumBlue;
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2013-08-15
  • 1970-01-01
  • 1970-01-01
  • 2016-04-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多