4

重複の可能性:
WinForms のコンテナー コントロールの境界パディングを削除するにはどうすればよいですか?

Visual Studio 2008 で Winforms アプリケーションを開発しました。メイン フォームにはTab コントロールがあります。現在、タブページに背景画像を使用しようとしています。私が直面している問題は、タブ コントロールの周りに太い境界線があるように見えることです。また、タブ コントロールはフォーム全体をカバーせず、フォームとタブ ページの間の上部に 1 行のスペースを残します。(タブページの配置を下部に設定しています)。そのため、タブコントロールの周りの境界線と上部のスペース行により、ページが見苦しくなります。背景と同じ画像を形成しようとしましたが、タブコントロールのパディングがスポイルスポーツを演じています。

私のデザインをより良くするためのアイデアをいただければ幸いです。

スクリーンショット

4

1 に答える 1

2

ここでのコメントの大部分に同意します。標準の TabControl は、Microsoft の Part では非常にうまく描画されません... 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
    }
}

上記のコードは、DisplayRectangle のみを表示する完全に空の TabControl を描画することに注意してください。あなたが自分で行う必要があるタブを含む他のすべて!

さらに、個々の 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 フォーラムを参照してください...素晴らしいカスタム タブ コントロールがそこにあるようです。

于 2012-08-10T08:33:37.507 に答える