C# Winfrom TabControl美化


實例一:

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

namespace KUN.CONTROL.LIB.CONTROL
{
    public partial class KMenuTabControl : System.Windows.Forms.TabControl
    {
        #region 屬性、構造
        Color SelectedColor = Color.LightSkyBlue;
        Color MoveColor = Color.LightSeaGreen;
        Color FontColor = Color.Black;
        int TextLeft = 10;
        [Browsable(true)]
        [Description("選項卡標題左邊距"), Category("TextLeft"), DefaultValue(typeof(Int32), "10")]
        public int TitleTextLeft
        {
            get { return TextLeft; }
            set { this.TextLeft = value; }
        }

        [Browsable(true)]
        [Description("選項卡標題字體顏色"), Category("TitleColor"), DefaultValue(typeof(Color), "Black")]
        public Color TitleFontColor
        {
            get { return FontColor; }
            set { this.FontColor = value; }
        }

        [Browsable(true)]
        [Description("選項卡標題字體選中顏色"), Category("TitleColor"), DefaultValue(typeof(Color), "LightSkyBlue")]
        public Color TitleSelectedColor
        {
            get { return SelectedColor; }
            set { this.SelectedColor = value; }
        }

        [Browsable(true)]
        [Description("選項卡標題字體懸浮顏色"), Category("TitleColor"), DefaultValue(typeof(Color), "White")]
        public Color TitleMoveColor
        {
            get { return MoveColor; }
            set { this.MoveColor = value; }
        }

        [Browsable(true), Description("整個控件的背景色"), Category("外觀")]
        public Color TabControlBackColor { get; set; }

        [Browsable(true), Description("TabControl ItemSize"), Category("外觀")]
        public Size TabControlItemSize { get; set; }

        public KMenuTabControl()
        {
            this.SuspendLayout();
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.ResumeLayout(false);
            this.SizeMode = TabSizeMode.Fixed;
            this.Multiline = true;
            this.TabControlBackColor = Color.SeaShell;
            this.TabControlItemSize = new Size(100, 28);
            this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabMenu_DrawItem);
            this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
        }
        #endregion

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle tcRec = this.ClientRectangle;//整個tabControl的邊框
            e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);
            if (this.ItemSize!=this.TabControlItemSize)
            {
                this.ItemSize = TabControlItemSize;
            }

            StringFormat sf = new StringFormat();//封裝文本布局信息 
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Near;
            for (int i = 0; i < this.TabCount; i++)
            {
                Graphics g = e.Graphics;
                // int width = (int)g.MeasureString(this.Controls[i].Text, this.Font).Width + 40;
                Rectangle rect = this.GetTabRect(i);
                //  rect.Width = width;
                if (this.SelectedIndex == i)
                    g.FillRectangle(new SolidBrush(MoveColor), rect);
                else g.FillRectangle(new SolidBrush(SelectedColor), rect);

                SolidBrush brush = new SolidBrush(FontColor);
                //  rect.Width = width;
                rect.X += TextLeft;
                g.DrawString(this.Controls[i].Text, this.Font, brush, rect, sf);
                using (Pen objpen = new Pen(Color.Black))
                {
                    int tx = (int)(rect.X + (rect.Width - 30));
                    rect.X = tx - 2;
                    Point p5 = new Point(tx, 8);
                    Font font = new System.Drawing.Font("微軟雅黑", 12);
                    g.DrawString("〇", font, brush, rect, sf);
                    font = new System.Drawing.Font("微軟雅黑", 11);
                    rect.X = tx + 2;
                    rect.Y = rect.Y - 1;
                    g.DrawString("×", font, brush, rect, sf);
                }
            }
        }

        public override Rectangle DisplayRectangle
        {
            get
            {
                Rectangle rect = base.DisplayRectangle;
                return new Rectangle(rect.Left - 2, rect.Top - 2, rect.Width + 4, rect.Height + 5);
            }
        }

        int index = -1;
        protected override void OnMouseMove(MouseEventArgs e)
        {
            int Count = 0;
            try
            {
                Graphics g = this.CreateGraphics();
                SolidBrush brush = new SolidBrush(FontColor);
                StringFormat sf = new StringFormat();//封裝文本布局信息 
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Near;

                for (int i = 0; i < this.TabPages.Count; i++)
                {
                    TabPage tp = this.TabPages[i];
                    if (this.GetTabRect(i).Contains(e.Location) && tp != this.SelectedTab)
                    {
                        if (index != i)
                        {
                            if (Count == 0)
                            {
                                if (index != -1 && this.TabPages[index] != this.SelectedTab)
                                {
                                    g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));

                                    RectangleF tRectangle = this.GetTabRect(index);
                                    tRectangle.X += TextLeft;
                                    g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangle, sf);
                                }
                                Count = 1;
                            }
                            index = i;
                            g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(i));
                            RectangleF tRectangleF = this.GetTabRect(i);
                            tRectangleF.X += TextLeft;
                            g.DrawString(this.Controls[i].Text, this.Font, brush, tRectangleF, sf);
                            using (Pen objpen = new Pen(Color.Black))
                            {
                                int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
                                tRectangleF.X = tx - 2;
                                brush.Color = Color.White;
                                Font font = new System.Drawing.Font("微軟雅黑", 12);
                                g.DrawString("〇", font, brush, tRectangleF, sf);
                                font = new System.Drawing.Font("微軟雅黑", 11);
                                tRectangleF.X = tx + 2;
                                tRectangleF.Y = tRectangleF.Y - 1;
                                g.DrawString("×", font, brush, tRectangleF, sf);
                            }
                        }
                    }
                    if (this.GetTabRect(i).Contains(e.Location) && tp == this.SelectedTab)
                    {
                        if (index != -1 && index != this.SelectedIndex)
                        {
                            g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
                            RectangleF tRectangleF = this.GetTabRect(index);
                            tRectangleF.X += TextLeft;
                            g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
                            using (Pen objpen = new Pen(Color.Black))
                            {
                                int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
                                tRectangleF.X = tx - 2;
                                Font font = new System.Drawing.Font("微軟雅黑", 12);
                                g.DrawString("〇", font, brush, tRectangleF, sf);
                                font = new System.Drawing.Font("微軟雅黑", 11);
                                tRectangleF.X = tx + 2;
                                tRectangleF.Y = tRectangleF.Y - 1;
                                g.DrawString("×", font, brush, tRectangleF, sf);
                            }
                        }
                        index = -1;
                    }
                }
            }
            catch (Exception)
            {
            }
            Count = 0;
            base.OnMouseMove(e);
        }

        protected override void OnMouseLeave(EventArgs e)
        {
            try
            {
                Graphics g = this.CreateGraphics();
                if (index != -1 && this.TabPages[index] != this.SelectedTab)
                {
                    g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
                    SolidBrush brush = new SolidBrush(FontColor);
                    RectangleF tRectangleF = this.GetTabRect(index);
                    StringFormat sf = new StringFormat();//封裝文本布局信息 
                    sf.LineAlignment = StringAlignment.Center;
                    sf.Alignment = StringAlignment.Near;
                    tRectangleF.X += TextLeft;
                    g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
                    using (Pen objpen = new Pen(Color.Black))
                    {
                        int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
                        tRectangleF.X = tx - 2;
                        Point p5 = new Point(tx, 8);
                        Font font = new System.Drawing.Font("微軟雅黑", 12);
                        g.DrawString("〇", font, brush, tRectangleF, sf);
                        font = new System.Drawing.Font("微軟雅黑", 11);
                        tRectangleF.X = tx + 2;
                        tRectangleF.Y = tRectangleF.Y - 1;
                        g.DrawString("×", font, brush, tRectangleF, sf);
                    }
                }
            }
            catch (Exception)
            {
            }
            index = -1;
            base.OnMouseLeave(e);
        }
        /// <summary>
        /// 重繪控件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabMenu_DrawItem(object sender, DrawItemEventArgs e)
        {
            this.SetStyle(
            ControlStyles.UserPaint |                      // 控件將自行繪制,而不是通過操作系統來繪制  
            ControlStyles.OptimizedDoubleBuffer |          // 該控件首先在緩沖區中繪制,而不是直接繪制到屏幕上,這樣可以減少閃爍  
            ControlStyles.AllPaintingInWmPaint |           // 控件將忽略 WM_ERASEBKGND 窗口消息以減少閃爍  
            ControlStyles.ResizeRedraw |                   // 在調整控件大小時重繪控件  
            ControlStyles.SupportsTransparentBackColor,    // 控件接受 alpha 組件小於 255 的 BackColor 以模擬透明  
            true);                                         // 設置以上值為 true  
            this.UpdateStyles();
        }

        //關閉按鈕功能
        private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            int closeSize = 20;
            if (e.Button == MouseButtons.Left)
            {
                int x = e.X, y = e.Y;
                //計算關閉區域   
                Rectangle tab = this.GetTabRect(this.SelectedIndex);
                tab.Offset(tab.Width - (closeSize + 3), 4);
                tab.Width = closeSize;
                tab.Height = closeSize;

                if (this.TabCount == 1) return;

                //如果鼠標在區域內就關閉選項卡   
                bool isClose = x > tab.X && x < tab.Right && y > tab.Y && y < tab.Bottom;
                if (isClose == true)
                {
                    this.TabPages.Remove(this.SelectedTab);
                }
            }
        }

    }
}

實例二:

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

namespace KUN.CONTROL.LIB.CONTROL
{
    public partial class KTabControl : System.Windows.Forms.TabControl
    {
        [Browsable(true), Description("整個控件的背景色"), Category("外觀")]
        public Color TabControlBackColor { get; set; }

        [Browsable(true), Description("Tab的標題欄邊框顏色"), Category("外觀")]
        public Color TabBorderColor { get; set; }

        [Browsable(true), Description("當前激活Tab的標題欄背景色"), Category("外觀")]
        public Color ActivedTabBackColor { get; set; }

        [Browsable(true), Description("當前激活Tab的標題文字顏色"), Category("外觀")]
        public Color ActivedTabLabelColor { get; set; }

        [Browsable(true), Description("未激活Tab的標題欄背景色"), Category("外觀")]
        public Color InActivedTabBackColor { get; set; }

        [Browsable(true), Description("未激活Tab的標題文字顏色"), Category("外觀")]
        public Color InActivedTabLabelColor { get; set; }

        [Browsable(true), Description("Tab標題欄的大小"), Category("外觀")]
        public Size TabSize { get; set; }

        public KTabControl()
        {
            this.InitializeComponent();

            TabSet();

            this.TabBorderColor = Color.Black;
            this.ActivedTabLabelColor = Color.Black;
            this.InActivedTabLabelColor = Color.Black;
            this.ActivedTabBackColor = Color.White;
            this.InActivedTabBackColor = Color.FromArgb(0, 192, 192);
            this.TabControlBackColor = Color.Transparent;
            this.TabSize = new Size(100, 35);
        }

        protected override void OnMouseDoubleClick(MouseEventArgs e)
        {
            if (this.TabPages.Count == 1) return;
            this.TabPages.RemoveAt(this.SelectedIndex);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            Rectangle tcRec = this.ClientRectangle;//整個tabControl的邊框
            e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);

            for (int i = 0; i < this.TabPages.Count; i++)
            {
                Rectangle tabRectangle = new Rectangle(1, 1 + i * TabSize.Height, TabSize.Width, TabSize.Height);
                SolidBrush brush = new SolidBrush(this.InActivedTabLabelColor);
                StringFormat sf = new StringFormat();//封裝文本布局信息
                sf.LineAlignment = StringAlignment.Center;
                sf.Alignment = StringAlignment.Center;
                if (i == this.SelectedIndex)
                {
                    brush = new SolidBrush(this.ActivedTabLabelColor);
                    e.Graphics.FillRectangle(new SolidBrush(ActivedTabBackColor), tabRectangle);
                    e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
                }
                else
                {
                    e.Graphics.FillRectangle(new SolidBrush(InActivedTabBackColor), tabRectangle);
                    e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
                }
                e.Graphics.DrawString(this.Controls[i].Text, this.Font, brush, tabRectangle, sf);
            }
        }

        /// <summary>
        /// 設定控件繪制模式
        /// </summary>
        private void TabSet()
        {
            this.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.Alignment = TabAlignment.Left;
            this.SizeMode = TabSizeMode.Fixed;
            this.Multiline = true;
            base.SetStyle(
                ControlStyles.UserPaint |                      
                ControlStyles.OptimizedDoubleBuffer |           
                ControlStyles.AllPaintingInWmPaint |             
                ControlStyles.ResizeRedraw |                     
                ControlStyles.SupportsTransparentBackColor,      
                true);                                         
            base.UpdateStyles();
        }

        public override Rectangle DisplayRectangle
        {
            get
            {
                Rectangle rect = base.DisplayRectangle;
                return new Rectangle(rect.Left - 3, rect.Top - 3, rect.Width + 6, rect.Height + 5);
            }
        }
    }
}

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM