3

OK, someone please tell me why this is not working.

I have a simple MenuStrip in winforms app (c#). It has ToolStripMenuItems.

In the properties window of the designer, I select BackColor = White. In Desginer.cs file I can see it.

Running the app, the background color is Control (grey).

What is going? Why is the backcolor not white?

Thanks

EDIT

This is the code from the Designer.cs:

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

Refresh item should be white

EDIT2:

In the code, after loading the form (in the constructor and also in Form_Load event I've placed this:

 menuRefresh.BackColor = Color.White;

Also not helping.

4

3 回答 3

6

您需要实现一个简单的渲染器类来实现这一点。这是一个例子:

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); 
        } 
    } 
} 
于 2014-08-21T12:24:05.733 回答
1

的不决定任何工具条菜单(下拉菜单)中包含的项目的背景颜色BackColorMenuStrip这些项目都有自己的BackColor属性,必须单独设置。

例如,您的“刷新”项目是它自己的ToolStripMenuItem,因此您还需要将其设置BackColor为白色。


关于您的第二次编辑,设置menuRefresh.BackColor = Color.White;应该在构造函数或 Form_Load 事件中正常工作。我已经对两者进行了测试,并且可以按预期工作。

于 2014-08-21T12:15:16.607 回答
0

我想做同样的事情来制作一个上下文菜单标题,我可以在其中设置工具条菜单项的背景、前景和边框颜色。

它与其他答案相似,但更加独立。

它看起来如何

代码是如何实现的;

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

    }
}
于 2021-10-18T17:57:21.663 回答