using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Drawing2D;

namespace GcDevicePc.ConfigDlg
{
    public partial class TabContorlEx : System.Windows.Forms.TabControl
    {
        //Image backImage;
        public TabContorlEx()
        {
            InitializeComponent();
           
            base.SetStyle(
                    ControlStyles.UserPaint |                      // 控件将自行绘制，而不是通过操作系统来绘制
                    ControlStyles.OptimizedDoubleBuffer |          // 该控件首先在缓冲区中绘制，而不是直接绘制到屏幕上，这样可以减少闪烁
                    ControlStyles.AllPaintingInWmPaint |           // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁
                    ControlStyles.ResizeRedraw |                   // 在调整控件大小时重绘控件
                    ControlStyles.SupportsTransparentBackColor,    // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明
                    true);                                         // 设置以上值为 true
            base.UpdateStyles();

            SizeMode = TabSizeMode.Fixed;  // 大小模式为固定
            ItemSize = new Size(55, 60);   // 设定每个标签的尺寸
        }

        public TabContorlEx(IContainer container)
        {
            container.Add(this);
            InitializeComponent();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
 
            Rectangle rec = this.ClientRectangle;
            SolidBrush bru = new SolidBrush(Color.FromArgb(228, 238, 250));
            e.Graphics.FillRectangle(bru, rec);
            e.Graphics.DrawRectangle(Pens.Black, rec);
       
            for (int i = 0; i < this.TabCount; i++)
            {

                Rectangle bounds = this.GetTabRect(i);

               if (this.ImageList != null)
               {
                   int index = this.TabPages[i].ImageIndex;
                   string key = this.TabPages[i].ImageKey;
                   Image icon = new Bitmap(55, 60);

                   if (index > -1)
                   {
                       icon = this.ImageList.Images[index];
                   }
                   if (!string.IsNullOrEmpty(key))
                   {
                       icon = this.ImageList.Images[key];
                   }
                   e.Graphics.DrawImage(
                       icon, this.GetTabRect(i));
               }
                                 
                if (this.SelectedIndex == i)
                {
                    if (this.ImageList != null)
                    {
                        int index = this.TabPages[i].ImageIndex;
                        string key = this.TabPages[i].ImageKey;
                        Image icon = new Bitmap(55, 60);

                        if (index > -1)
                        {
                            icon = this.ImageList.Images[index + 16];
                        }
                        if (!string.IsNullOrEmpty(key))
                        {
                            icon = this.ImageList.Images[key +16];
                        }
                        e.Graphics.DrawImage(
                            icon, this.GetTabRect(i));
                    }
                }           
            }
        
            base.OnPaint(e);
        }

    }

}
