如何在WinForms中去除容器控件的边框填充?

6
我将margin和padding设置为0 0 0 0,但这对我的TabControls没有任何影响。看一下:
这就是我所说的。我希望将边框粘在一起。
我该怎么做?
@Henk Holterman - 是的,有什么问题吗?

你有没有考虑过在控件的顶部(选项卡侧)看起来会如何? - H H
3个回答

11

微软程序员在TabPage的源代码中留下了一条注释(已编辑以适应页面):

//HACK: to ensure that the tabpage draws correctly (the border will get 
//  clipped and gradient fill will match correctly with the tabcontrol).
//  Unfortunately, there is no good way to determine the padding used 
//  on the tabpage.
//  I would like to use the following below, but GetMargins is busted 
//  in the theming API:
//VisualStyleRenderer visualStyleRenderer = new VisualStyleRenderer(VisualStyleElement.Tab.Pane.Normal);
//Padding themePadding = visualStyleRenderer.GetMargins(e.Graphics, MarginProperty.ContentMargins);

视觉样式一直是一个主要的错误制造工厂,尤其是对于TabControl而言。查看此答案,了解如何选择性地关闭TabControl的视觉样式,以便获得您所熟悉的行为。当然,这确实会改变外观。


2
我同意Henk的观点。容器控件周围有一个相同大小的边框(据我回忆,为9像素)。这个原因是为了防止您将控件压缩得太靠近边缘。如果您在顶部这样做,您的控件将与顶部的选项卡标题非常接近。它看起来很傻,并且会让用户感到困惑。WinForms在这里帮助您避免错误,而您甚至不知道。这也是最初采取此举的原因。
熟悉Microsoft的标准用户界面指南,特别是布局部分。请注意所有控件(对话框窗口本身、选项卡控件等)周围都有一个边框?在Visual C++资源编辑器中,它是7个对话框单位;WinForms使用像素规范。

    sample tab control, with border around edges
    spacing around a button control


但是看起来有点奇怪... VCL 允许我使用任何边距。请查看我的屏幕截图,其中有 3x7 像素我不需要。 - cnd
@nCdy:我不知道VCL是什么。这个屏幕截图是设计时间还是运行时?无论哪种情况,如果你认为它看起来很奇怪,我建议扩大边框而不是压缩它们。在你的代码中,白色空间不仅仅是有价值的。 - Cody Gray
这是来自运行时的。哦...那我会尝试完全移除边框。 - cnd

1
尝试这个。
public class TabControlEx : TabControl
{
    protected override void WndProc(ref Message m)
    {
        if (m.Msg == 0x1300 + 40)
        {
            RECT rc = (RECT)m.GetLParam(typeof(RECT));
            rc.Left -= 0;
            rc.Right += 3;
            rc.Top -= 0;
            rc.Bottom += 3;
            Marshal.StructureToPtr(rc, m.LParam, true);
        }
        base.WndProc(ref m);
    }

}
internal struct RECT { public int Left, Top, Right, Bottom; }

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接