【问题标题】:Removing the padding around a Tab control in WinForms [duplicate]删除 WinForms 中 Tab 控件周围的填充[重复]
【发布时间】:2026-02-15 00:50:01
【问题描述】:

可能重复:
How can I remove the border padding on container controls in WinForms?

我在 Visual Studio 2008 中开发了一个 Winforms 应用程序。在主窗体上,我有一个 Tab 控件。现在我正在尝试在标签页中使用 背景图片。我遇到的问题是选项卡控件周围似乎有一个粗边框。此外,选项卡控件不会覆盖整个表单,在表单和选项卡页之间的顶部留下一行空间。 (我在底部设置了标签页对齐方式)。所以选项卡控件周围的边框和顶部的空间线使我的页面看起来很难看。我试图提供与背景相同的图像来形成,但选项卡控件填充播放破坏运动。

任何让我的设计更好的想法都将不胜感激。

【问题讨论】:

  • 截图会有很多的帮助。
  • 继承控件并按照您希望的方式绘制它。这就是人们无法拥有非方形和/或矩形按钮的原因。我不明白投诉。简单的解决方案是设计自己的没有此边框的控件。
  • 哦!你如何做到这一点?请详细一点
  • @Ramhound 如果您只是想拥有背景图片,那就有点 OTT。
  • @Ramhound 同意,我确实认为一些自定义绘图实际上可能是为了解决这种情况 - 请参阅 TabControl and borders visual glitch

标签: c# winforms tabcontrol


【解决方案1】:

我同意这里制作的大多数 cmets。标准的 TabControl 在 Microsoft 部分绘制得非常糟糕……即使在 Windows Vista / 7 中它看起来也不是很好!您最好通过继承 TabControl 来编写自己的自定义实现,然后绘制您想要的其他内容。

您可以考虑将其用作新控件的模板。您只需在 OnPaint 和 OnPaintBackground 方法中添加一些很酷的设计/绘图工作。

namespace CustomControls
{
    #region USING

    using System;
    using System.ComponentModel;
    using System.Drawing;
    using System.Windows.Forms;

    #endregion

    public class CustomTabControl : TabControl
    {
        #region VARIABLES

        private int hotTrackTab = -1;

        #endregion

        #region INSTANCE CONSTRUCTORS

        public CustomTabControl() : base()
        {
            this.InitializeComponent();
        }

        #endregion

        #region INSTANCE METHODS

        private void InitializeComponent()
        {
            this.SetStyle(ControlStyles.UserPaint, true);
            this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
            this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            this.SetStyle(ControlStyles.ResizeRedraw, true);
            this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);

            this.DrawMode = TabDrawMode.OwnerDrawFixed;
        }

        private int GetTabUnderCursor()
        {
            Point cursor = this.PointToClient(Cursor.Position);
            for (int index = 0; index < this.TabPages.Count; index++)
            {
                if (this.GetTabRect(index).Contains(cursor))
                {
                    return index;
                }
            }
            return -1;
        }

        private void UpdateHotTrack()
        {
            int hot = GetTabUnderCursor();
            if (hot != this.hotTrackTab)
            {
                if (this.hotTrackTab != -1)
                {
                    this.Invalidate(this.GetTabRect(this.hotTrackTab));
                }
                this.hotTrackTab = hot;
                if (this.hotTrackTab != -1)
                {
                    this.Invalidate(this.GetTabRect(this.hotTrackTab));
                }
                this.Update();
            }
        }

        #endregion

        #region OVERRIDE METHODS

        protected override void OnMouseEnter(EventArgs e)
        {
            base.OnMouseEnter(e);
            this.UpdateHotTrack();
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            base.OnMouseLeave(e);
            this.UpdateHotTrack();
        }

        protected override void OnMouseMove(MouseEventArgs e)
        {
            base.OnMouseMove(e);
            this.UpdateHotTrack();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            switch (this.Alignment)
            {
                case TabAlignment.Bottom:
                case TabAlignment.Left:
                case TabAlignment.Right:
                case TabAlignment.Top:
                default:
                    throw new NotImplementedException();
            }
        }

        protected override void OnPaintBackground(PaintEventArgs pevent)
        {
            base.OnPaintBackground(pevent);
        }

        #endregion
    }
}

请记住,上面的代码绘制了一个完全空白的 TabControl,它只显示 DisplayRectangle。其他一切,包括您需要自己做的标签!

此外,要为各个 TabPages 绘制背景,您可能还需要重写和自定义实现 TabPage,但是您也许可以仅使用自定义选项卡控件来实现您正在寻找的结果。

看看这个 http://www.codeproject.com/Articles/42046/Customized-TabControl-by-Repainting-Microsoft-s-Pa

恕我直言,这更好... http://www.codeproject.com/Articles/38014/KRBTabControl

恕我直言,这更好... http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition

还可以查看 VB 论坛...我知道那里似乎有一些很棒的自定义选项卡控件!

【讨论】: