C# 重绘tabControl,添加关闭按钮(页签)

调用方法

参数:

 /// <summary>/// 初始化/// </summary>/// <param name="tabcontrol">TacControl控件</param>/// <param name="fo">主程序this.Font</param>DrawTabControl dtc=new DrawTabControl(tabControl1,this.Font);
dtc.ClearPage();

类 

 #region 重绘tablecontrolpublic class DrawTabControl{TabControl tabControl1 = null;Font font1 = null;public DrawTabControl() { }public DrawTabControl(TabControl tabcontrol,Font fo){tabControl1 = tabcontrol;font1 = fo;}const int CLOSE_SIZE = 15;//tabPage标签图片Bitmap image = new Bitmap("D:\\power_003.png");public void ClearPage(){//清空控件//this.MainTabControl.TabPages.Clear();//绘制的方式OwnerDrawFixed表示由窗体绘制大小也一样this.tabControl1.DrawMode = TabDrawMode.OwnerDrawFixed;this.tabControl1.Padding = new System.Drawing.Point(CLOSE_SIZE, CLOSE_SIZE - 8);this.tabControl1.DrawItem += new DrawItemEventHandler(this.tabControl1_DrawItem);this.tabControl1.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControl1_MouseDown);}private void tabControl1_DrawItem(object sender, DrawItemEventArgs e){try{Rectangle myTabRect = this.tabControl1.GetTabRect(e.Index);//先添加TabPage属性   e.Graphics.DrawString(this.tabControl1.TabPages[e.Index].Text, this.font1, 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)){////=============================================//自己画X////"\"线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);////=============================================//使用图片//Bitmap bt = new Bitmap(image);//Point p5 = new Point(myTabRect.X, 4);//e.Graphics.DrawImage(bt, p5);//e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.font1, objpen.Brush, p5);
                }e.Graphics.Dispose();}catch (Exception){ }}private void tabControl1_MouseDown(object sender, MouseEventArgs e){if (e.Button == MouseButtons.Left){int x = e.X, y = e.Y;//计算关闭区域   Rectangle myTabRect = this.tabControl1.GetTabRect(this.tabControl1.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.tabControl1.TabPages.Remove(this.tabControl1.SelectedTab);}}}}#endregion 

转载于:https://www.cnblogs.com/chcong/p/4311991.html

C# 重绘tabControl,添加关闭按钮(页签)相关推荐

  1. 用ASP.NET 重绘TabControl代码

    在www.codeproject.com 看到一个关于重绘tabControl的例了,觉得挺有意思的.照着修改一下,有一些东西自己并没有去改,使得代码很短,同时也有一些功能并没实现的.具休可到http ...

  2. c#-winform重绘Tabcontrol控件,标签带Logo图标

    模仿网页浏览器标签重绘Tabcontrol控件,每个标签页左上角的Logo图标可以自定义,当然图标也可以挪到右边,直接上图.

  3. C# 重绘tabControl,添加关闭按钮(续)

    在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,tableControl都会自动跳到第一个页面,显然 这不是我们想要的,为此,我修改了部分的代码.除此之外,我还添加了 ...

  4. C#中关于WinForm中重绘TabControl选项卡标题的问题

    这里说的是每个TabPage的头部,也就是标题,不是工作区域. 最开始用到TabControl的时候,我的每个选项卡是写死的,而后由于项目需求又动态添加了TabControl并生成各个选项卡,而两次我 ...

  5. js动态添加html页签(JavaScript 拼接html标签代码)

    目录 情形:需求 实现:分析 实现 页面:前端 总结:仔细再仔细 情形: 最近遇到这样一个情况: 需要实现动态增加页面,可以添加页面,页面的内容需要能保存,修改和删除. 页面的名字可编辑,并且要实现单 ...

  6. MFC-CListCtrl重绘,添加按钮到单元格

    原文链接 MFC Listctrl 不支持单元格颜色设置,以及单击其中某一单元格时高亮显示,要想达成自己的目的,就只能对其重绘. 关于单元格中按钮的添加,说一下思路,首先要重写CButton类,将其单 ...

  7. C#重绘TabControl控件的源码(转)

    代码   1using System;   2 using System.Collections.Generic;   3 using System.ComponentModel;   4 using ...

  8. Ant design vue pro 添加多页签

    1.修改模板 /src/layouts/BasicLayout.vue 添加如下代码 第一处: <!-- layout content --><a-layout-content :s ...

  9. Jquery UI Tabs 动态添加页签,并跳转到新页签

    需求: 1.tabs默认只有一个页签,但是需要点击某按钮,动态添加页签(添加多个) 2.tabs动态添加页签后,需要跳转到新添加的页签 查找tabs api以及tabs的源码后,发现tabs没有直接实 ...

  10. 渲染:重绘,重排/重新布局,重设样式

    2010 update: Lo, the Web Performance Advent Calendar hath moved 2010年更新: Lo, Web Performance Advent ...

最新文章

  1. 无监督学习距离监督学习还有多远?Hinton组新作解读
  2. 免费的XShell替代品,同时支持Windows,macOS,Linux!又来一款国产良心工具....
  3. 专访 TensorFlow 贡献者唐源:掌握 Google 深度学习框架的正确姿势
  4. ipv6前缀长度计算_IPv6和IPv4中对比看RIP
  5. 最短Hamilton路径(状压dp)
  6. 带left join 的sql的执行顺序
  7. [LeetCode] Best Time to Buy and Sell Stock 买卖股票的最佳时间
  8. AI大咖们的18岁照,你能认对几个?
  9. python卸载_技术 | Python 包安装和卸载的几种方式
  10. 计算机829大纲,829计算机基础考试大纲
  11. 软考中级-软件设计师-查缺补漏
  12. windows超级终端介绍及widows7超级终端下载
  13. 2021-10-12 CHIP类PCB封装的创建
  14. LU分解 LDL分解 Cholesky分解
  15. verilog 学习笔记2 异步复位串联T触发器
  16. 成考专科计算机专业,我是计算机专科生,成考想换个专业,请问学什 – 手机爱问...
  17. Cups打印机驱动延迟Bug解决
  18. GT sport真实赛道详解 - Brands Hatch | 伯蘭士赫治GP賽車場
  19. 关于青岛某电视厂商新研发中心周边环境的思考
  20. 【手把手教学】利用七牛云免费CDN服务为自己网站启用图片CDN加速 - 免费版10G/月

热门文章

  1. 资源分享 | ArcGis engine 10.4
  2. 计算机三位科学家,华南理工大学这三位年轻科学家太优秀了!
  3. 简便方法搭建Harbor镜像仓库
  4. 大学心理学课本_2019年北京师范大学765真题分析
  5. PG Vs MySQL ,到底谁更强?
  6. git如何选择性合并_看小姐姐用动图展示10大Git命令
  7. 代码统计工具有哪几种_抖音小程序如何开发及类型有哪几种?
  8. 查看谁连接oracle,oracle如何查看当前有哪些用户连接到数据库
  9. 图表位置下移_excel图表技能:如何更准确的表现营业额的变化趋势
  10. python os.access_Python用access判断文件是否被占用的实例方法