C# 是否更改子菜单中ToolstripSeparator的背景色?
我有一个C# 是否更改子菜单中ToolstripSeparator的背景色?,c#,C#,我有一个ContextMenuStrip,它有4个项目。其中一个项目有一个子菜单和另外4个项目,其中一个是分隔符。我更改了所有项目的背景色,但分隔符的背景色没有更改 为什么以及如何改变它 我今天刚刚面对这个问题,发现解决它非常简单 有同样情况的: 解决方案: 创建一个继承ToolStripSeparator类的类,并向绘制事件处理程序添加一个方法以绘制分隔符: public class ExtendedToolStripSeparator : ToolStripSeparator {
ContextMenuStrip
,它有4个项目。其中一个项目有一个子菜单和另外4个项目,其中一个是分隔符。我更改了所有项目的背景色,但分隔符的背景色没有更改
为什么以及如何改变它
我今天刚刚面对这个问题,发现解决它非常简单 有同样情况的: 解决方案: 创建一个继承
ToolStripSeparator
类的类,并向绘制事件处理程序添加一个方法以绘制分隔符:
public class ExtendedToolStripSeparator : ToolStripSeparator
{
public ExtendedToolStripSeparator()
{
this.Paint += ExtendedToolStripSeparator_Paint;
}
private void ExtendedToolStripSeparator_Paint(object sender, PaintEventArgs e)
{
// Get the separator's width and height.
ToolStripSeparator toolStripSeparator = (ToolStripSeparator)sender;
int width = toolStripSeparator.Width;
int height = toolStripSeparator.Height;
// Choose the colors for drawing.
// I've used Color.White as the foreColor.
Color foreColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardForeColorName);
// Color.Teal as the backColor.
Color backColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardBackColorName);
// Fill the background.
e.Graphics.FillRectangle(new SolidBrush(backColor), 0, 0, width, height);
// Draw the line.
e.Graphics.DrawLine(new Pen(foreColor), 4, height / 2, width - 4, height / 2);
}
}
ToolStripSeparator toolStripSeparator = new ExtendedToolStripSeparator();
this.DropDownItems.Add(newGameToolStripMenuItem);
this.DropDownItems.Add(addPlayerToolStripMenuItem);
this.DropDownItems.Add(viewResultsToolStripMenuItem);
// Add the separator here.
this.DropDownItems.Add(toolStripSeparator);
this.DropDownItems.Add(exitToolStripMenuItem);
然后添加分隔符:
public class ExtendedToolStripSeparator : ToolStripSeparator
{
public ExtendedToolStripSeparator()
{
this.Paint += ExtendedToolStripSeparator_Paint;
}
private void ExtendedToolStripSeparator_Paint(object sender, PaintEventArgs e)
{
// Get the separator's width and height.
ToolStripSeparator toolStripSeparator = (ToolStripSeparator)sender;
int width = toolStripSeparator.Width;
int height = toolStripSeparator.Height;
// Choose the colors for drawing.
// I've used Color.White as the foreColor.
Color foreColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardForeColorName);
// Color.Teal as the backColor.
Color backColor = Color.FromName(Utilities.Constants.ControlsRelatedConstants.standardBackColorName);
// Fill the background.
e.Graphics.FillRectangle(new SolidBrush(backColor), 0, 0, width, height);
// Draw the line.
e.Graphics.DrawLine(new Pen(foreColor), 4, height / 2, width - 4, height / 2);
}
}
ToolStripSeparator toolStripSeparator = new ExtendedToolStripSeparator();
this.DropDownItems.Add(newGameToolStripMenuItem);
this.DropDownItems.Add(addPlayerToolStripMenuItem);
this.DropDownItems.Add(viewResultsToolStripMenuItem);
// Add the separator here.
this.DropDownItems.Add(toolStripSeparator);
this.DropDownItems.Add(exitToolStripMenuItem);
结果: