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,里面放个按钮,点击的时 ...
随机推荐
- ThinkSNS+ 是如何计算字符显示长度的
什么是ThinkSNS+ ThinkSNS(简称TS),一款全平台综合性社交系统,目前最新版本为ThinkSNS+.ThinkSNS V4 ThinkSNS[简]. 今天我们来聊一下可能很多人都会头疼 ...
- IOS字符串截取保留小数点后两位
-(NSString*)getTheCorrectNum:(NSString*)tempString { //计算截取的长度 NSUInteger endLength = tempString.len ...
- JAVAFX-1 开发应用
为什么用javafx? 写这个专题的目的,其实也是好玩,原来的熟悉的使用swing 来开发java中的gui程序,其实早就知道有javafx 这个东西的,一致没有时间的玩,最近有时间所以学习.这个专题 ...
- Codeforces Round #564 (Div. 2) C. Nauuo and Cards
链接:https://codeforces.com/contest/1173/problem/C 题意: Nauuo is a girl who loves playing cards. One da ...
- Python基本的数据类型(补发)
python基本的数据类型 一.python的基本数据类型 int => 整数,主要用来进行数学运算 str ==> 字符串 可以用来保存少量数据并进行相应操作 bool ==> ...
- oracle中CAST函数使用简介【转】
CAST()函数可以进行数据类型的转换. CAST()函数的参数有两部分,源值和目标数据类型,中间用AS关键字分隔. 以下例子均通过本人测试. 一.转换列或值 语法:cast( 列名/值 as 数据类 ...
- .NET Core中Circuit Breaker
谈谈Circuit Breaker在.NET Core中的简单应用 前言 由于微服务的盛行,不少公司都将原来细粒度比较大的服务拆分成多个小的服务,让每个小服务做好自己的事即可. 经过拆分之后,就避免不 ...
- centOS6.5 关闭关盖待机
因为centOS安装在笔记本上面的,有时要把电脑放在一边,用SSH连接 所以需要关盖不休眠 用命令没找到怎么设置 后面在桌面电脑选项里面设置的,设置成黑屏或者不执行动作应该都是可以的.
- 走进docker的世界之入门篇
by zhouzhipeng from https://blog.zhouzhipeng.com/walk-in-docker-beginning.html本文可全文转载,但需要保留原作者和出处. 什 ...
- EF5 通用数据层 增删改查操作,泛型类
using System; using System.Collections.Generic; using System.Data.Entity.Infrastructure; using Syste ...