TabControl 和边框视觉故障

发布于 2024-12-09 20:39:47 字数 574 浏览 0 评论 0原文

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

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

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

有没有办法去掉那些白色边框?

I have these visual glitches on every tabControls when I am changing its tabPages BackColor and the BackColor of the form, as illustrated on the following images:

  • At the top of the tabPage, there is an interior one-pixel white border.
  • At the left of the tabPage, there is an interior three-pixels white border.
  • At the bottom of the tabPage, there is an interior one-pixel white border and an exterior two-pixels white border.
  • At the right of the tabPage, there is an interior one-pixel white border and an exterior two-pixels white border.

Top and left borders
Bottom borders
Top and right borders

Is there a way I can get rid of those white borders?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

能怎样 2024-12-16 20:39:47

这是我尝试的黑客行为。我使用 NativeWindowTabControl 上绘制以填充那些“白色”空间。我不会声称它是完美的:

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

我的最终结果:

在此处输入图像描述

Here's my attempted hack. I used a NativeWindow to draw over the TabControl to fill in those "white" spaces. I won't claim it's perfect:

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

And to hook it up:

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

My end result:

enter image description here

无人问我粥可暖 2024-12-16 20:39:47

你可以从控制继承

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

you can inherit from control

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; }
删除→记忆 2024-12-16 20:39:47

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

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

I recently ran into this problem and never found a good simple solution. That's when I thought about simply adjusting the control's clipping region. This seemed to work just fine with no noticeable side-effects. The 2 extra white pixels on the right side and the one extra white pixel at the bottom are no longer visible.

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);
    }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文