zoukankan      html  css  js  c++  java
  • TabControl TabPage添加关闭按钮

    自定义控件代码如下:

    using System.Drawing;
    using System.Windows.Forms;
    
    namespace Demo.UC
    {
        public class KKTab : TabControl
        {
            private int IconWOrH = 16;
            private Image icon = null;
    
            public KKTab()
                : base()
            {
                this.DrawMode = TabDrawMode.OwnerDrawFixed;
    
                icon = Demo.Properties.Resources.close;
                IconWOrH = icon.Width;
                IconWOrH = icon.Height;
            }
    
            protected override void OnDrawItem(DrawItemEventArgs e)
            {
                Graphics g = e.Graphics;
                Rectangle r = GetTabRect(e.Index);
                if (e.Index == this.SelectedIndex)    //当前选中的Tab页,设置不同的样式以示选中
                {
                    Brush selected_color = Brushes.SteelBlue; //选中的项的背景色
                    g.FillRectangle(selected_color, r); //改变选项卡标签的背景色 
                    string title = this.TabPages[e.Index].Text;
                    g.DrawString(title, this.Font, new SolidBrush(Color.Black), new PointF(r.X , r.Y + 5));//PointF选项卡标题的位置 
                    r.Offset(r.Width - IconWOrH, 2);
                    g.DrawImage(icon, new Point(r.X, r.Y));//选项卡上的图标的位置 fntTab = new System.Drawing.Font(e.Font, FontStyle.Bold);
                }
                else//非选中的
                {
                    Brush selected_color = Brushes.AliceBlue; //选中的项的背景色
                    g.FillRectangle(selected_color, r); //改变选项卡标签的背景色 
                    string title = this.TabPages[e.Index].Text+"  ";
                    g.DrawString(title, this.Font, new SolidBrush(Color.Black), new PointF(r.X , r.Y + 5));//PointF选项卡标题的位置 
                    r.Offset(r.Width - IconWOrH, 2);
                    g.DrawImage(icon, new Point(r.X, r.Y));//选项卡上的图标的位置 
                }
            }
    
            protected override void OnMouseClick(MouseEventArgs e)
            {
                Point point = e.Location;
                Rectangle r = GetTabRect(this.SelectedIndex);
                r.Offset(r.Width - IconWOrH - 3, 2);
                r.Width = IconWOrH;
                r.Height = IconWOrH;
                if (r.Contains(point)) this.TabPages.RemoveAt(this.SelectedIndex);
            }
        }
    }
    

    注意:使用方式:界面加载需处理TabPage Text属性

     foreach (TabPage item in this.kkTab1.TabPages)
                {
                    item.Text = item.Text + "  ";
                }
    

    运行效果如下:

  • 相关阅读:
    516. 最长回文子序列
    NC50493 环形石子合并
    NC16650 采药
    NC16664 合唱队形
    NC51170 石子合并
    148. 合并果子
    NC25138 子串查询
    二维数组对角线 的 规律
    如何讲一个网页转换为jpg?(图片!)
    Java两倍 犯错题
  • 原文地址:https://www.cnblogs.com/YYkun/p/9199773.html
Copyright © 2011-2022 走看看