关闭

winform tabpage 绘制关闭按钮点击关闭事件和双击tab关闭页面

2494人阅读 评论(0) 收藏 举报
分类:

ps:尝试下新的编辑器,看看啥感觉哈。
界面效果图
说明:
1.button1用于添加tab页面
2.button2用户删除当前tab页面
3.tabcontrol 命名MainTabControl
关键部分:绘制关闭的“x”按钮点击关闭当前tab页面,双击tab标签关闭tab页面。

绘制”x”代码部分(网络上拷贝)

//绘制“X”号即关闭按钮  
        private void MainTabControl_DrawItem(object sender, DrawItemEventArgs e)
        {

            try
            {
                Rectangle myTabRect = this.MainTabControl.GetTabRect(e.Index);

                //先添加TabPage属性     
                e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text
                , this.Font, SystemBrushes.ControlText, myTabRect.X + 2, myTabRect.Y + 2);

                //再画一个矩形框  
                using (Pen p = new Pen(Color.White))
                {
                    myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                    myTabRect.Width = CLOSE_SIZE;
                    myTabRect.Height = CLOSE_SIZE;
                    e.Graphics.DrawRectangle(p, myTabRect);

                }

                //填充矩形框  
                Color recColor = e.State == DrawItemState.Selected ? Color.White : Color.White;
                using (Brush b = new SolidBrush(recColor))
                {
                    e.Graphics.FillRectangle(b, myTabRect);
                }

                //画关闭符号  
                using (Pen objpen = new Pen(Color.Black))
                {
                    //"\"线  
                    Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
                    Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
                    e.Graphics.DrawLine(objpen, p1, p2);

                    //"/"线  
                    Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
                    Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
                    e.Graphics.DrawLine(objpen, p3, p4);
                }

                e.Graphics.Dispose();
            }
            catch(Exception ex)
            {
                MessageBox.Show(ex.Message);
            }


        }
        //=======================================================================  

点击”x”关闭tab页面

//关闭按钮功能  
        private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                int x = e.X, y = e.Y;

                //计算关闭区域     
                Rectangle myTabRect = this.MainTabControl.GetTabRect(this.MainTabControl.SelectedIndex);

                myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 2);
                myTabRect.Width = CLOSE_SIZE;
                myTabRect.Height = CLOSE_SIZE;

                //如果鼠标在区域内就关闭选项卡     
                bool isClose = x > myTabRect.X && x < myTabRect.Right
                 && y > myTabRect.Y && y < myTabRect.Bottom;

                if (isClose == true)
                {
                    this.MainTabControl.TabPages.Remove(this.MainTabControl.SelectedTab);
                }
            }


        }

双击选项卡关闭tab页面

private void MainTabControl_MouseDoubleClick(object sender, EventArgs e) {
            MainTabControl.TabPages.Remove(MainTabControl.SelectedTab); 
        }

button1,button2 click事件

private void button1_Click(object sender, EventArgs e)
        {
            string title = "text";
            bool ifExits = false;
            foreach (TabPage tb in this.MainTabControl.TabPages) {
                if (tb.Text == title) {
                    ifExits=true; 
                }
            }
            if (!ifExits)
            {
                TabPage tabtage = new TabPage();
                tabtage.Text = title;
                tabtage.ToolTipText = title;
                TemplateForm form = new TemplateForm();
                form.FormBorderStyle = FormBorderStyle.None; //隐藏子窗体边框(去除最小花,最大化,关闭等按钮)
                form.TopLevel =
                false; //指示子窗体非顶级窗体
                tabtage.Controls.Add(form);//将子窗体载入panel
                form.Show();

                MainTabControl.TabPages.Add(tabtage);
                MainTabControl.SelectedTab = tabtage;
                //image = new Bitmap(TestTabPageTabControl.Properties.Resources.box_okay);  
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {
            if (MainTabControl.SelectedTab != null)
            {
                MainTabControl.TabPages.Remove(MainTabControl.SelectedTab);
            }
        }

form load事件

private void testForm_Load(object sender, System.EventArgs e)
        {
            //清空控件  
            //this.MainTabControl.TabPages.Clear();  
            //绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样  
            this.MainTabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
            this.MainTabControl.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE / 2);
            this.MainTabControl.DrawItem += new DrawItemEventHandler(this.MainTabControl_DrawItem);
            this.MainTabControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
            this.MainTabControl.MouseDoubleClick += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDoubleClick);
        }

代码都比较简单 流程也简单 效果也就不是特别好看 做一个记录,免得忘记了

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:22683次
    • 积分:537
    • 等级:
    • 排名:千里之外
    • 原创:27篇
    • 转载:33篇
    • 译文:0篇
    • 评论:0条
    博客专栏