如何在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; }