简体   繁体   English

时间:2019-05-11 标签:c#toolstripmenuitem更改背景winforms

[英]c# winforms toolstripmenuitem change background

OK, someone please tell me why this is not working.好的,有人请告诉我为什么这不起作用。

I have a simple MenuStrip in winforms app (c#).我在 winforms 应用程序(c#)中有一个简单的 MenuStrip。 It has ToolStripMenuItems.它有 ToolStripMenuItems。

In the properties window of the designer, I select BackColor = White.在设计器的属性窗口中,我选择 BackColor = White。 In Desginer.cs file I can see it.在 Desginer.cs 文件中我可以看到它。

Running the app, the background color is Control (grey).运行应用,背景色为Control(灰色)。

What is going?怎么回事? Why is the backcolor not white?为什么背景色不是白色?

Thanks谢谢

EDIT编辑

This is the code from the Designer.cs:这是来自 Designer.cs 的代码:

   this.menuRefresh.BackColor = System.Drawing.Color.White;

刷新项应该是白色的

EDIT2:编辑2:

In the code, after loading the form (in the constructor and also in Form_Load event I've placed this:在代码中,加载表单后(在构造函数和 Form_Load 事件中,我放置了这个:

 menuRefresh.BackColor = Color.White;

Also not helping.也帮不上忙。

You need to implement a simple renderer class to achieve this. 您需要实现一个简单的渲染器类以实现此目的。 Here is an example: 这是一个例子:

public partial class Form1 : Form 
{ 
    public Form1() 
    { 
      InitializeComponent(); 
      menuStrip1.Renderer = new MyRenderer(); 
    } 

    private class MyRenderer : ToolStripProfessionalRenderer 
    { 
        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)      
        { 
            Rectangle rc = new Rectangle(Point.Empty, e.Item.Size); 
            Color c = e.Item.Selected ? Color.Azure : Color.Beige; 
            using (SolidBrush brush = new SolidBrush(c)) 
                e.Graphics.FillRectangle(brush, rc); 
        } 
    } 
} 

The BackColor of the MenuStrip does not determine the background colour of the items included in any tool strip menus (drop downs). MenuStripBackColor不能确定任何工具条菜单(下拉菜单)中包含的项目的背景色。 These items each have their own BackColor property, which must be set separately. 这些项目每个都有自己的BackColor属性,必须分别设置。

For example, your "Refresh" item is it's own ToolStripMenuItem , so you need to set the BackColor of that to White also. 例如,您的“刷新”项目是它自己的ToolStripMenuItem ,因此您还需要将该项目的BackColor设置为White。


In regards to your second edit, setting menuRefresh.BackColor = Color.White; 关于第二次编辑,设置menuRefresh.BackColor = Color.White; should work fine in either the constructor, or the Form_Load event. 应该在构造函数或Form_Load事件中正常工作。 I have tested it with both and it works as expected. 我已经对它们进行了测试,并且可以正常工作。

I wanted to do the same thing to make a contextmenustrip header, where i could set the background, forground and border colors of a toolstripmenuitem.我想做同样的事情来制作上下文菜单标题,我可以在其中设置工具条菜单项的背景、前景和边框颜色。

It is similar to the other answers but a bit more self contained.它与其他答案相似,但更加独立。

How it looks看起来如何

How the code is implemented;代码是如何实现的;

ContextMenustrip.Items.Add(new CustomCMSItems.ToolStripHeader("Shifts", new Font("Segoe UI", 10, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 1, false), Color.Black, Color.LightGray, Color.Red));


public class ToolStripHeader : ToolStripMenuItem
{
    Color _BackColor;
    Color _BorderColor;
    Color _FontColor;
    Font _Font;//= new Font("Segoe UI", 10, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 1, false)

    public ToolStripHeader(string text, Font font, Color textcolor, Color BackgroundColor, Color BorderColor) //: base(new Label())
    {
        this.Padding = Padding.Empty;

        _BackColor = BackgroundColor;
        _BorderColor = BorderColor;
        _FontColor = textcolor;
        _Font = font;

        this.Text = text;
    }

    protected override void OnParentChanged(ToolStrip oldParent, ToolStrip newParent)
    {
        base.OnParentChanged(oldParent, newParent);

        if (newParent != null)
        {
            if (newParent.GetType() == typeof(ContextMenuStrip))
            {
                newParent.Renderer = new HeaderRenderer(_Font, _FontColor, _BackColor, _BorderColor);
            }
        }
    }

    private class HeaderRenderer : ToolStripProfessionalRenderer
    {
        Color _BackColor;
        Color _BorderColor;
        Color _FontColor;
        Font _Font;//= new Font("Segoe UI", 10, FontStyle.Bold, System.Drawing.GraphicsUnit.Point, 1, false)

        public HeaderRenderer(Font font, Color textcolor, Color BackgroundColor, Color BorderColor) //: base(new Label())
        {


            _BackColor = BackgroundColor;
            _BorderColor = BorderColor;
            _FontColor = textcolor;
            _Font = font;

        }

        protected override void OnRenderMenuItemBackground(ToolStripItemRenderEventArgs e)
        {
            if (e.Item.GetType() == typeof(ToolStripHeader))
            {
                Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);

                SolidBrush brush = new SolidBrush(_BackColor);
                Pen pen = new Pen(_BorderColor);

                e.Graphics.FillRectangle(brush, rc);
                e.Graphics.DrawRectangle(pen, 1, 0, rc.Width - 2, rc.Height - 1);

                return;
            }

            base.OnRenderMenuItemBackground(e);

            if (!e.Item.Selected) base.OnRenderMenuItemBackground(e);
            else
            {
                //Example
                //Rectangle rc = new Rectangle(Point.Empty, e.Item.Size);
                //e.Graphics.FillRectangle(Brushes.DarkGray, rc);
                //e.Graphics.DrawRectangle(Pens.Black, 1, 0, rc.Width - 2, rc.Height - 1);

            }
        }

        protected override void OnRenderItemText(ToolStripItemTextRenderEventArgs e)
        {
            if (e.Item.GetType() == typeof(ToolStripHeader))
            {
                e.TextColor = _FontColor;
                e.TextFont = _Font;
            }
            base.OnRenderItemText(e);
        }

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM