TabControl和边界视觉故障

Oti*_*iel 14 c# tabcontrol winforms

tabControls每当我更改它tabPages BackColorBackColor表单时,我都会遇到这些视觉故障,如下图所示:

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

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

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

Lar*_*ech 17

这是我的黑客攻击.我用a 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));
        }
      }
    }
  }
}
Run Code Online (Sandbox Code Playgroud)

把它连接起来:

public Form1() {
  InitializeComponent();
  var tab = new TabPadding(tabControl1);
}
Run Code Online (Sandbox Code Playgroud)

我的最终结果:

在此输入图像描述

  • 哇,谢谢你.非常感谢你!究竟是人们期望TabControl做什么(再次"微软,为什么?!") (2认同)

Vib*_*nRC 5

你可以从控制继承

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; }
Run Code Online (Sandbox Code Playgroud)

  • 这是此页面上唯一适合我的解决方案,但是我必须将 Left 调整为 -= 4,Right 调整为 += 2,Top 调整为 -= 2。谢谢 (2认同)