[英]Dynamically Clone ToolStripMenuItem to ContexMenuStrip c# Winforms
[英]c# winforms toolstripmenuitem change background
好的,有人請告訴我為什么這不起作用。
我在 winforms 應用程序(c#)中有一個簡單的 MenuStrip。 它有 ToolStripMenuItems。
在設計器的屬性窗口中,我選擇 BackColor = White。 在 Desginer.cs 文件中我可以看到它。
運行應用,背景色為Control(灰色)。
怎么回事? 為什么背景色不是白色?
謝謝
編輯
這是來自 Designer.cs 的代碼:
this.menuRefresh.BackColor = System.Drawing.Color.White;
編輯2:
在代碼中,加載表單后(在構造函數和 Form_Load 事件中,我放置了這個:
menuRefresh.BackColor = Color.White;
也幫不上忙。
您需要實現一個簡單的渲染器類以實現此目的。 這是一個例子:
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);
}
}
}
MenuStrip
的BackColor
不能確定任何工具條菜單(下拉菜單)中包含的項目的背景色。 這些項目每個都有自己的BackColor
屬性,必須分別設置。
例如,您的“刷新”項目是它自己的ToolStripMenuItem
,因此您還需要將該項目的BackColor
設置為White。
關於第二次編輯,設置menuRefresh.BackColor = Color.White;
應該在構造函數或Form_Load事件中正常工作。 我已經對它們進行了測試,並且可以正常工作。
我想做同樣的事情來制作上下文菜單標題,我可以在其中設置工具條菜單項的背景、前景和邊框顏色。
它與其他答案相似,但更加獨立。
代碼是如何實現的;
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.