zoukankan      html  css  js  c++  java
  • 【Winform-自定义控件】自定义Tab Control 带关闭符号(X)的标签页

    a form & a tabControl

    思路:

    DrawMode设一定要设为OwnerDrawFixed

    事件:Form_Load、tabControl1_DrawItem、tabControl1_DrawItem

    注意:

    如果 设置tabControl的SizeMode为fixed,则标签页的长度以最大的为标准
    如果 设置tabControl的SizeMode为normal,则标签页的长度随标题变化,
    此句代码:tabControl1.Padding = new System.Drawing.Point(21, 3); 表示‘X’与标题间有空隙。
    namespace Demo
    {
        public partial class Form7 : Form
        {
            const int LEADING_SPACE = 12;
            const int CLOSE_SPACE = 15;
            const int CLOSE_AREA = 15;
    
            public Form7()
            {
                InitializeComponent();
                this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;
            }
    
            private void Form7_Load(object sender, EventArgs e)
            {
                // get the inital length
                int tabLength = tabControl1.ItemSize.Width;
    
                // measure the text in each tab and make adjustment to the size
                for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
                {
                    TabPage currentPage = tabControl1.TabPages[i];
    
                    int currentTabLength = TextRenderer.MeasureText(currentPage.Text, tabControl1.Font).Width;
                    // adjust the length for what text is written
                    currentTabLength += LEADING_SPACE + CLOSE_SPACE + CLOSE_AREA;
    
                    if (currentTabLength > tabLength)
                    {
                        tabLength = currentTabLength;
                    }
                }
    
                // create the new size
                Size newTabSize = new Size(tabLength, tabControl1.ItemSize.Height);
                tabControl1.ItemSize = newTabSize;
            }
    
            private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
            {
                //add some extra space to the end of every tabPage
                tabControl1.Padding = new System.Drawing.Point(21, 3);
    
                //render a "x" mark at the end of the Tab caption 
                e.Graphics.DrawString("x", e.Font, Brushes.Black, e.Bounds.Right - CLOSE_AREA, e.Bounds.Top + 4);
                e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, e.Font, Brushes.Black, e.Bounds.Left + LEADING_SPACE, e.Bounds.Top + 4);
                e.DrawFocusRectangle();
            }
    
            private void tabControl1_MouseDown(object sender, MouseEventArgs e)
            {
                //Looping through the controls.
                for (int i = 0; i < this.tabControl1.TabPages.Count; i++)
                {
                    Rectangle r = tabControl1.GetTabRect(i);
                    //Getting the position of the "x" mark.
                    Rectangle closeButton = new Rectangle(r.Right - 15, r.Top + 4, 9, 7);
                    if (closeButton.Contains(e.Location))
                    {
                        if (MessageBox.Show("Would you like to Close this Tab?", "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                        {
                            this.tabControl1.TabPages.RemoveAt(i);
                            break;
                        }
                    }
                }
            }
        }
    }
  • 相关阅读:
    硬盘分区、寻址和系统启动过程
    剑指offer---05---用栈实现队列
    剑指offer---05---用栈实现队列
    服务端提供的JSON数据接口与用户端接收解析JSON数据
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
    xgqfrms™, xgqfrms® : xgqfrms's offical website of GitHub!
  • 原文地址:https://www.cnblogs.com/Sukie-s-home/p/6520683.html
Copyright © 2011-2022 走看看