在WinForms中去除Tab控件周围的填充

4
可能是重复的问题:
如何在WinForms中去除容器控件的边框填充?

我在Visual Studio 2008中开发了一个Winforms应用程序。在主窗体上,我有一个选项卡控件。现在我想在选项卡页上使用一个背景图片。我遇到的问题是选项卡控件似乎有一个厚厚的边框围绕着它。此外,选项卡控件没有覆盖整个窗体,在顶部留下一行空白。(我将选项卡页面的对齐方式设置为底部)。因此,选项卡控件周围有边框,顶部有一行空白,使我的页面看起来很丑陋。我试图将与窗体相同的图片作为背景,并使选项卡控件的填充不再产生影响。

欢迎提出任何改进设计的想法。

ScreenShot


5
截图会非常有帮助。 - CodeCaster
继承控件并以您想要的方式进行绘制。这就是人们能够拥有不是正方形和/或矩形的按钮的方法。我不明白抱怨在哪里。简单的解决方案是设计自己的控件,它没有这个边框。 - Security Hound
2
哦!你是怎么做到的?能再详细解释一下吗? - Shiridish
1
@Ramhound同意,我认为可能需要一些自定义绘图来解决这种情况 - 参见TabControl和边框视觉故障 - James
哇,那太丑了。标准背景有什么问题吗?你知道的,那个你可以真正阅读文本的背景。而且如果你不喜欢蓝色,你还可以更改它。 - Cody Gray
显示剩余3条评论
1个回答

2
我同意这里大部分的评论。标准的TabControl在微软的部分下绘制得非常糟糕...即使在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。包括选项卡在内的其他所有内容都需要您自己完成!
此外,为单个TabPage绘制背景,您可能需要覆盖并自定义实现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论坛...我知道我在那里看到了一些很棒的自定义选项卡控件!

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接