TabControl重写,添加关闭按钮
class userTabControl : TabControl
{
const int CLOSE_SIZE = 15;
protected override void OnInvalidated(InvalidateEventArgs e)
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.Padding = new System.Drawing.Point(CLOSE_SIZE, 0);
base.OnInvalidated(e);
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
try
{
Rectangle myTabRect = this.GetTabRect(e.Index);
//先添加TabPage属性
if (e.State == DrawItemState.Selected)
{
e.Graphics.FillRectangle(new SolidBrush(Color.LightYellow), myTabRect);
}
Brush br = e.State == DrawItemState.Selected ? Brushes.Red : SystemBrushes.ControlText;
e.Graphics.DrawString(this.TabPages[e.Index].Text, this.Font, br, myTabRect.X + 2, myTabRect.Y + 2);
//再画一个矩形框
using (Pen p = new Pen(Color.White))
{
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 3), 0);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
}
//画关闭符号
Color clr = e.State == DrawItemState.Selected ? Color.Red : Color.Green;
using (Pen objpen = new Pen(clr, float.Parse("1.5")))
{
////=============================================
//自己画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);
//e.Graphics.DrawString(this.MainTabControl.TabPages[e.Index].Text, this.Font, objpen.Brush, p5);
}
e.Graphics.Dispose();
}
catch (Exception)
{ }
base.OnDrawItem(e);
}
protected override void OnDoubleClick(EventArgs e)
{
if (this.SelectedIndex >= 0)
{
this.TabPages.RemoveAt(this.SelectedIndex);
}
base.OnDoubleClick(e);
}
protected override void OnMouseDown(MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
try
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = this.GetTabRect(this.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.TabPages.Remove(this.SelectedTab);
}
}
catch
{
}
}
base.OnMouseDown(e);
}
}
使用方法:
1、添加userTabControl类。
2、添加TabControl控件,将Design文件中
this.tabControl1 = new System.Windows.Forms.TabControl(); 改为 this.tabControl1 = new userTabControl();
TabControl重写,添加关闭按钮的更多相关文章
- C# 重绘tabControl,添加关闭按钮(页签)
C# 重绘tabControl,添加关闭按钮(页签) 调用方法 参数: /// <summary> /// 初始化 /// </summary> /// <param n ...
- C# 重绘tabControl,添加关闭按钮(续)
在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,tableControl都会自动跳到第一个页面,显然 这不是我们想要的,为此,我修改了部分的代码.除此之外,我还添加了 ...
- TabControl TabPage添加关闭按钮
自定义控件代码如下: using System.Drawing; using System.Windows.Forms; namespace Demo.UC { public class KKTab ...
- TabControl里面添加From
private void dynamicDll() { string dllName = "dll"; Assembly ass = Assembly.Load(dllName); ...
- 向tabcontrol中添加form
昨天花了一天的时间去找一个错误,关系是这样的,我添加一个tabcontrol就叫tc1好了,然后在tc1中再动态添加一个父窗体l叫form1,要把form1添加进tabcontrol就要先新建一个ta ...
- Qt 【Qlistview + delegate 为item重写个关闭按钮】
效果图是这样的. 实现的过程是listview + delegate 本身我想是用listwidget + delegate[网上查询到不可实现] 之前也试过在item中添加布局跟控件,但是在点击的时 ...
- Tabcontrol动态添加TabPage(获取或设置当前选项卡及其属性)
http://blog.csdn.net/xiongxyt2/article/details/6920575 •MultiLine 属性用true 或false来确定是否可以多行显示 •Appeara ...
- 转: 向tabControl中添加一个Form(C#)
- WPF自适应可关闭的TabControl 类似浏览器的标签页
效果如图: 虽然说是自适应可关闭的TabControl,但TabControl并不需要改动,不如叫自适应可关闭的TabItem. 大体思路:建一个用户控件,继承自TabItem,里面放个按钮,点击的时 ...
随机推荐
- 一些我推荐的和想上的网络课程(Coursera, edX, Udacity)
从面向找工作的角度出发,我觉得以下课程有很大帮助: 首推Robert Sedgewick,也是我觉得对我帮助最大的老师,讲课特点是能把复杂的算法讲解清楚(典型例子:红黑树,KMP算法) 他在Cours ...
- 利用system.reflection遍历一个类的变量成员
假设有下面一个类,在程序中已初始化,如何获取里面的变量成员name,age,onduty及其值呢? public class Employee { public string name; public ...
- myeclipse 最佳设置
http://www.cnblogs.com/wuyifu/p/3593035.html
- 微服务的.NET Core示例框架
eShopOnContainers 是一个基于微服务的.NET Core示例框架 https://www.cnblogs.com/fengqingyangNo1/p/9438428.html 找到一个 ...
- is null 的优化
bk1gx7xwj9du6SELECT * FROM HEADERS GJH WHERE ATTRIBUTE10 IS NULL FOR UPDATE 优化建议如下: 短期来说:建立一个索引来优化: ...
- (转)Linux日志管理+ last lastlog lastb
Linux日志管理+ last lastlog lastb 原文:http://blog.csdn.net/xin_y/article/details/53440707 日志管理 日志通常存放在 /v ...
- Spring连接数据库
public class Book { private int bookid; private String bookname; private String bookauthor; private ...
- VS 解决方案文件结构分析
VS2013 解决方案文件结构分析 Visual Studio 的解决方案文件是一个文本文件,其中的内容不是太复杂,有些时候 Visual Studio 会把这个文件搞乱,理解一下这个文件的结构,对我 ...
- Angular2中实现基于TypeScript的对象合并方法:extend()
TypeScript里面没有现成的合并对象的方法,这里借鉴jQuery里的$.extend()方法.写了一个TypeScript的对象合并方法,使用方法和jQuery一样. 部分代码和jQuery代码 ...
- unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计
unity3d + photon + grpc + nodejs + postgis/postgresql 游戏服务器设计 最近做玩票性质的游戏项目,客户端技术是 unity3d 和 android. ...