MenuItem的颜色更改 [英] Color change for MenuItem

查看:492
本文介绍了MenuItem的颜色更改的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在编写备份工具.在我的工具之上,我有一个菜单条,其中包含两个工具条菜单.我改变了颜色,使颜色达到了我的期望.不集中菜单看起来很棒:

I am programming a Backup Tool. On top of my tool I have a menustrip containing two toolstripmenuitems. I changed the colors a little bit to my expectations. Not focused the menu looks great:

当我现在单击菜单项文件"以打开上下文菜单时,颜色变为白色,并且我不再能够阅读文本:

When I now click on the menu item "File" to open the context menu, the color changes to white and I am not able to read the text anymore:

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

Can anyone please tell me where I can change that behavior? I use Visual Studio 2013 Ultimate, Windows Forms Application, Code is in 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);
//

// // 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);
//

推荐答案

默认情况下,此功能开箱即用.您需要为工具栏创建一个自定义Renderer来实现此目的.

By default this feature is not available out of the box. You need to create a custom Renderer for you tool strip to achieve this.

创建一个从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);
        }
    }

并将此渲染器附加到表单构造器中的菜单栏上-

And attach this renderer to your menu strip in your form constructor -

    public Form1()
    {
        InitializeComponent();
        menuStrip1.Renderer = new BlueRenderer();
    }

这篇关于MenuItem的颜色更改的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆