[英]How to change line separators in menustrip in visual studio?
I am having a problem witth visual studio.我在使用 Visual Studio 时遇到问题。 I created a menustrip with different menus and some contain separators.我创建了一个带有不同菜单的菜单条,其中一些包含分隔符。 I want the theme of the program to be dark so I changed the color of all buttons to black background and white text.我希望程序的主题是深色的,所以我将所有按钮的颜色更改为黑色背景和白色文本。 however I cannot change the separators' colors.但是我无法更改分隔符的颜色。 They are white background and black line.它们是白色背景和黑色线条。 I tried from properties - not working.我尝试从属性 - 不工作。 I tried with code - also not working.我尝试使用代码 - 也不起作用。 Can anyone help me?谁能帮我? Also I want to ask how to change the color of the outline of the buttons?另外我想问一下如何更改按钮轮廓的颜色? it's currently white.目前是白色的。
There's no need to create a new separator , just create a color table : 无需创建新的分隔符,只需创建一个颜色表即可:
public class MyCustomColors: ProfessionalColorTable
{
public override Color SeparatorLight
{
get { return Color.FromArgb(100, 100, 100); }
}
public override Color SeparatorDark
{
get { return Color.FromArgb(100, 100, 100); }
}
}
}
then use it like : 然后像这样使用它:
menu_control.Renderer = new ToolStripProfessionalRenderer(new MyCustomColors());
The default toolstrip renderer ignores the BackColor property and uses hard-coded colors. 默认的工具条渲染器将忽略BackColor属性,并使用硬编码的颜色。 so write a custom class as below: 因此,编写如下的自定义类:
public class CustomToolStripSeparator : ToolStripSeparator
{
public CustomToolStripSeparator()
{
Paint += CustomToolStripSeparator_Paint;
}
private void CustomToolStripSeparator_Paint(object sender, PaintEventArgs e)
{
// Get the separator's width and height.
ToolStripSeparator toolStripSeparator = (ToolStripSeparator)sender;
int width = toolStripSeparator.Width;
int height = toolStripSeparator.Height;
//Color foreColor = Color.Blue;
Color backColor = Color.Yellow;
// 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);
}
}
and inside your form_load method apply your custom toolstripseparator: 在您的form_load方法内部应用自定义工具tripseparator:
private void Form1_Load(object sender, EventArgs e)
{
ToolStripSeparator toolStripSeparator1 = new CustomToolStripSeparator();
ToolStripSeparator toolStripSeparator2 = new CustomToolStripSeparator();
ToolStripSeparator toolStripSeparator3 = new CustomToolStripSeparator();
this.fileToolStripMenuItem.DropDownItems.Add("Save Project");
this.fileToolStripMenuItem.DropDownItems.Add(toolStripSeparator1);
this.fileToolStripMenuItem.DropDownItems.Add("Reload Project");
this.fileToolStripMenuItem.DropDownItems.Add(toolStripSeparator2);
this.fileToolStripMenuItem.DropDownItems.Add("Close Project");
this.fileToolStripMenuItem.DropDownItems.Add(toolStripSeparator3);
this.fileToolStripMenuItem.DropDownItems.Add("New Window");
}
The answer provided by Rashedul.Rubel is best answer to this problem. Rashedul.Rubel提供的答案是此问题的最佳答案。 Using this method one can also change the other behavior of separator. 使用此方法,还可以更改分隔符的其他行为。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.