简体   繁体   English

如何删除MenuStrip控件下绘制的白线?

[英]How can I remove the white line drawn under a MenuStrip control?

I read a few other articles about how people want to customize the colors and gradients of a MenuStrip. 我读了一些关于人们如何定制MenuStrip的颜色和渐变的文章。

What I want to do is remove the gradient so that the MenuStrip is the same color as the rest of the form which, for me, is the default settings used when creating a new WinForms project. 我想要做的是删除渐变,以便MenuStrip与表单的其余部分颜色相同,对我来说,这是创建新的WinForms项目时使用的默认设置。 I tried changing the RenderMode to 'System' and it works sort of, but it leaves a white line the length of the MenuStrip when I build and run it. 我尝试将RenderMode更改为'System'并且它可以工作,但是当我构建并运行它时,它会在MenuStrip的长度上留下一条白线。 Do I have to do some drawing and painting? 我是否需要做一些绘画和绘画? Or is there an easier way? 或者有更简单的方法吗?

This is basically the same question as this one 这基本上是因为这个同样的问题一个

The answer references this Microsoft bug post 答案引用了这篇微软的帖子

It seems to be an issue all the way from 2005. Although the comments say that it is a MS bug which will not be fixed, there is a workaround which involves implementing your own renderer: 从2005年开始,这似乎是一个问题。虽然评论说这是一个无法修复的MS错误,但有一个解决方法涉及实现自己的渲染器:

public class MySR : ToolStripSystemRenderer
{
    public MySR()
    {
    }

    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        //base.OnRenderToolStripBorder(e);
    }
}

Then all you have to do is set your menustrip's renderer to the one you just implemented: 然后,您只需将menustrip的渲染器设置为刚刚实现的渲染器:

menustrip1.Renderer = new MySR();

I just tried it out and it seems to work just fine. 我刚试了一下它似乎工作得很好。

I agree with Yetti but if you wish to maintain your borders you can try this. 我同意Yetti,但如果你想保持你的边界,你可以试试这个。 Replace Brush with your background Color 用您的背景颜色替换画笔

protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
  base.OnRenderToolStripBorder(e);
  e.Graphics.FillRectangle(Brushes.Black, e.ConnectedArea);
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM