简体   繁体   English

如何在visual studio中的menustrip中更改行分隔符?

[英]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.

相关问题 Winforms MenuStrip-如何更改下拉菜单项左侧的白线? - Winforms MenuStrip - How to change white line on left edge of dropdown items? Visual Studio 2019 menustrip 项目点击事件 - Visual Studio 2019 menustrip item click event 如何像Visual Studio中一样,将最近的文件菜单添加到菜单栏中:“文件”>“最近的文件”? - How can i add to menustrip a recent files menu like in visual studio: File > Recent Files? 如何在Visual Studio中更改数据库? - how to change the database in visual studio? 如何更改菜单条的保护级别,以便我可以以其他形式编写不是菜单条的代码 - How to change protection level of menustrip so I can code in other form where menustrip isn't it Visual Studio - 更改行号旁边的“行更改指示符”的颜色 - Visual Studio - change color of “line changes indicator” next to line numbers 如何删除MenuStrip控件下绘制的白线? - How can I remove the white line drawn under a MenuStrip control? 如何用多行分隔符将文本存储到数据库中? - How to store text with multi line separators into database? 如何分割具有多个分隔符的线? - How to Split a Line that have multiple separators? 如何从子窗口更改menuStrip菜单文本? - How do I change a menuStrip menu text from child window?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM