繁体   English   中英

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

[英]How can I remove the border padding on container controls in WinForms?

我将margin和padding设置为0 0 0 0,但这对我的TabControls没有任何影响。 看:

在此输入图像描述

这就是我所说的。 我想把边界粘在一起。

我怎样才能做到这一点?

@Henk Holterman - 是的,它有什么问题?

由一位恼怒的Microsoft程序员(为了适应页面而编辑),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);

Visual Styles是一个主要的bug工厂,特别是TabControl。 检查此答案 ,以便有选择地为TabControl关闭它,这样您就可以获得您习惯的行为。 当然它确实改变了外观。

我同意Henk。 在容器控件周围有一个相同大小的边框(我的记忆中有9个像素)。 它的原因是为了防止你将控件压得太靠近边缘。 如果您在顶部执行此操作,则您的控件将太靠近顶部的选项卡标题。 它看起来很愚蠢,让用户感到困惑。 WinForms在这里拯救你自己,你甚至不知道它。 究竟是它首先完成的原因。

熟悉Microsoft的标准用户界面指南,尤其是布局部分 注意所有控件(对话框窗口本身,选项卡控件等)是如何围绕它们的边框的? 它是Visual C ++资源编辑器中的7个对话框单元; WinForms使用像素规范。

样本选项卡控件,边框周围有边框
按钮控件周围的间距

尝试这个

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; }

暂无
暂无

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

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