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

发布于 2024-10-17 17:47:06 字数 221 浏览 1 评论 0原文

我读了一些其他文章,了解人们如何自定义 MenuStrip 的颜色和渐变。

我想要做的是删除渐变,以便 MenuStrip 与表单的其余部分具有相同的颜色,对我来说,这是创建新的 WinForms 项目时使用的默认设置。我尝试将 RenderMode 更改为“System”,它可以正常工作,但是当我构建并运行它时,它会留下一条与 MenuStrip 的长度相同的白线。我必须做一些绘画吗?或者有更简单的方法吗?

I read a few other articles about how people want to customize the colors and gradients of a 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. 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. Do I have to do some drawing and painting? Or is there an easier way?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

屌丝范 2024-10-24 17:47:06

这基本上与这个一个< /a>

答案引用了这个Microsoft bug post

这似乎是从 2005 年开始就一直存在的问题。尽管评论说这是一个 MS bug,无法修复,有一种解决方法涉及实现您自己的渲染器:

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

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

然后您所要做的就是将菜单条的渲染器设置为您刚刚实现的渲染器:

menustrip1.Renderer = new MySR();

我刚刚尝试了一下,它似乎工作得很好。

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:

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:

menustrip1.Renderer = new MySR();

I just tried it out and it seems to work just fine.

物价感观 2024-10-24 17:47:06

我同意 Yetti 的观点,但如果你想维持你的边界,你可以尝试这个。将画笔替换为您的背景颜色

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

I agree with Yetti but if you wish to maintain your borders you can try this. Replace Brush with your background Color

protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
{
  base.OnRenderToolStripBorder(e);
  e.Graphics.FillRectangle(Brushes.Black, e.ConnectedArea);
}
美人迟暮 2024-10-24 17:47:06

@Yetti 的答案是正确的,但有一个小问题。它还删除了菜单下拉部分(单击某个项目时打开的菜单)的边框。为了避免这种情况,请在 OnRenderToolStripBorder 中执行以下操作:

public class MySR : ToolStripSystemRenderer
{
    public MySR()
    {
    }
    
    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        if (e.AffectedBounds == e.ToolStrip.Bounds) return;
        base.OnRenderToolStripBorder(e);
    }
}

@Yetti's answer is correct, but has a small issue. It also removes the border from the dropdown part of the menu (the one that opens when you click on an item). In order to avoid this, do the following in OnRenderToolStripBorder:

public class MySR : ToolStripSystemRenderer
{
    public MySR()
    {
    }
    
    protected override void OnRenderToolStripBorder(ToolStripRenderEventArgs e)
    {
        if (e.AffectedBounds == e.ToolStrip.Bounds) return;
        base.OnRenderToolStripBorder(e);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文