TabControl和边界视觉故障

当我更改其tabPages BackColor和表单的BackColor时,我在每个tabControls上都有这些视觉故障,如下图所示:

  • tabPage的顶部,有一个内部单像素白色边框。
  • tabPage的左侧,有一个内部三像素白色边框。
  • tabPage的底部,有一个内部单像素白色边框和一个外部双像素白色边框。
  • tabPage的右侧,有一个内部单像素白色边框和一个外部双像素白色边框。

顶部和左侧边框底部边界顶部和右边界

有没有办法摆脱那些白色边界?

这是我的黑客攻击。 我使用NativeWindow绘制TabControl来填充那些“白色”空格。 我不会声称它是完美的:

 public class TabPadding : NativeWindow { private const int WM_PAINT = 0xF; private TabControl tabControl; public TabPadding(TabControl tc) { tabControl = tc; tabControl.Selected += new TabControlEventHandler(tabControl_Selected); AssignHandle(tc.Handle); } void tabControl_Selected(object sender, TabControlEventArgs e) { tabControl.Invalidate(); } protected override void WndProc(ref Message m) { base.WndProc(ref m); if (m.Msg == WM_PAINT) { using (Graphics g = Graphics.FromHwnd(m.HWnd)) { //Replace the outside white borders: if (tabControl.Parent != null) { g.SetClip(new Rectangle(0, 0, tabControl.Width - 2, tabControl.Height - 1), CombineMode.Exclude); using (SolidBrush sb = new SolidBrush(tabControl.Parent.BackColor)) g.FillRectangle(sb, new Rectangle(0, tabControl.ItemSize.Height + 2, tabControl.Width, tabControl.Height - (tabControl.ItemSize.Height + 2))); } //Replace the inside white borders: if (tabControl.SelectedTab != null) { g.ResetClip(); Rectangle r = tabControl.SelectedTab.Bounds; g.SetClip(r, CombineMode.Exclude); using (SolidBrush sb = new SolidBrush(tabControl.SelectedTab.BackColor)) g.FillRectangle(sb, new Rectangle(r.Left - 3, r.Top - 1, r.Width + 4, r.Height + 3)); } } } } } 

把它连接起来:

 public Form1() { InitializeComponent(); var tab = new TabPadding(tabControl1); } 

我的最终结果:

在此处输入图像描述

你可以从控制inheritance

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

我最近遇到了这个问题,从未找到一个很好的简单解决方案。 那是我想到简单地调整控件的剪辑区域的时候。 这似乎工作得很好,没有明显的副作用。 右侧的2个额外白色像素和底部的一个额外白色像素不再可见。

 class TabControlEx : TabControl { protected override void WndProc(ref Message m) { if (m.Msg == 0x0005) // WM_SIZE { int Width = unchecked((short)m.LParam); int Height = unchecked((short)((uint)m.LParam >> 16)); // Remove the annoying white pixels on the outside of the tab control // by adjusting the control's clipping region to exclude the 2 pixels // on the right and one pixel on the bottom. Region = new Region(new Rectangle(0, 0, Width - 2, Height - 1)); } base.WndProc(ref m); } } 

使用MaterialSkin从NuGet包添加MaterialSkin [右键单击您的解决方案 – 单击管理NuGet包 – 搜索MaterialSkin.dll]然后在工具箱中拖动MaterialSkin.dll并使用材质外观tabControl