实例一:

using System;
using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; namespace KUN.CONTROL.LIB.CONTROL
{
public partial class KMenuTabControl : System.Windows.Forms.TabControl
{
#region 属性、构造
Color SelectedColor = Color.LightSkyBlue;
Color MoveColor = Color.LightSeaGreen;
Color FontColor = Color.Black;
int TextLeft = 10;
[Browsable(true)]
[Description("选项卡标题左边距"), Category("TextLeft"), DefaultValue(typeof(Int32), "10")]
public int TitleTextLeft
{
get { return TextLeft; }
set { this.TextLeft = value; }
} [Browsable(true)]
[Description("选项卡标题字体颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "Black")]
public Color TitleFontColor
{
get { return FontColor; }
set { this.FontColor = value; }
} [Browsable(true)]
[Description("选项卡标题字体选中颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "LightSkyBlue")]
public Color TitleSelectedColor
{
get { return SelectedColor; }
set { this.SelectedColor = value; }
} [Browsable(true)]
[Description("选项卡标题字体悬浮颜色"), Category("TitleColor"), DefaultValue(typeof(Color), "White")]
public Color TitleMoveColor
{
get { return MoveColor; }
set { this.MoveColor = value; }
} [Browsable(true), Description("整个控件的背景色"), Category("外观")]
public Color TabControlBackColor { get; set; } [Browsable(true), Description("TabControl ItemSize"), Category("外观")]
public Size TabControlItemSize { get; set; } public KMenuTabControl()
{
this.SuspendLayout();
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.ResumeLayout(false);
this.SizeMode = TabSizeMode.Fixed;
this.Multiline = true;
this.TabControlBackColor = Color.SeaShell;
this.TabControlItemSize = new Size(100, 28);
this.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabMenu_DrawItem);
this.MouseDown += new System.Windows.Forms.MouseEventHandler(this.MainTabControl_MouseDown);
}
#endregion protected override void OnPaint(PaintEventArgs e)
{
Rectangle tcRec = this.ClientRectangle;//整个tabControl的边框
e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec);
if (this.ItemSize!=this.TabControlItemSize)
{
this.ItemSize = TabControlItemSize;
} StringFormat sf = new StringFormat();//封装文本布局信息
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Near;
for (int i = 0; i < this.TabCount; i++)
{
Graphics g = e.Graphics;
// int width = (int)g.MeasureString(this.Controls[i].Text, this.Font).Width + 40;
Rectangle rect = this.GetTabRect(i);
// rect.Width = width;
if (this.SelectedIndex == i)
g.FillRectangle(new SolidBrush(MoveColor), rect);
else g.FillRectangle(new SolidBrush(SelectedColor), rect); SolidBrush brush = new SolidBrush(FontColor);
// rect.Width = width;
rect.X += TextLeft;
g.DrawString(this.Controls[i].Text, this.Font, brush, rect, sf);
using (Pen objpen = new Pen(Color.Black))
{
int tx = (int)(rect.X + (rect.Width - 30));
rect.X = tx - 2;
Point p5 = new Point(tx, 8);
Font font = new System.Drawing.Font("微软雅黑", 12);
g.DrawString("〇", font, brush, rect, sf);
font = new System.Drawing.Font("微软雅黑", 11);
rect.X = tx + 2;
rect.Y = rect.Y - 1;
g.DrawString("×", font, brush, rect, sf);
}
}
} public override Rectangle DisplayRectangle
{
get
{
Rectangle rect = base.DisplayRectangle;
return new Rectangle(rect.Left - 2, rect.Top - 2, rect.Width + 4, rect.Height + 5);
}
} int index = -1;
protected override void OnMouseMove(MouseEventArgs e)
{
int Count = 0;
try
{
Graphics g = this.CreateGraphics();
SolidBrush brush = new SolidBrush(FontColor);
StringFormat sf = new StringFormat();//封装文本布局信息
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Near; for (int i = 0; i < this.TabPages.Count; i++)
{
TabPage tp = this.TabPages[i];
if (this.GetTabRect(i).Contains(e.Location) && tp != this.SelectedTab)
{
if (index != i)
{
if (Count == 0)
{
if (index != -1 && this.TabPages[index] != this.SelectedTab)
{
g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index)); RectangleF tRectangle = this.GetTabRect(index);
tRectangle.X += TextLeft;
g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangle, sf);
}
Count = 1;
}
index = i;
g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(i));
RectangleF tRectangleF = this.GetTabRect(i);
tRectangleF.X += TextLeft;
g.DrawString(this.Controls[i].Text, this.Font, brush, tRectangleF, sf);
using (Pen objpen = new Pen(Color.Black))
{
int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
tRectangleF.X = tx - 2;
brush.Color = Color.White;
Font font = new System.Drawing.Font("微软雅黑", 12);
g.DrawString("〇", font, brush, tRectangleF, sf);
font = new System.Drawing.Font("微软雅黑", 11);
tRectangleF.X = tx + 2;
tRectangleF.Y = tRectangleF.Y - 1;
g.DrawString("×", font, brush, tRectangleF, sf);
}
}
}
if (this.GetTabRect(i).Contains(e.Location) && tp == this.SelectedTab)
{
if (index != -1 && index != this.SelectedIndex)
{
g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
RectangleF tRectangleF = this.GetTabRect(index);
tRectangleF.X += TextLeft;
g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
using (Pen objpen = new Pen(Color.Black))
{
int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
tRectangleF.X = tx - 2;
Font font = new System.Drawing.Font("微软雅黑", 12);
g.DrawString("〇", font, brush, tRectangleF, sf);
font = new System.Drawing.Font("微软雅黑", 11);
tRectangleF.X = tx + 2;
tRectangleF.Y = tRectangleF.Y - 1;
g.DrawString("×", font, brush, tRectangleF, sf);
}
}
index = -1;
}
}
}
catch (Exception)
{
}
Count = 0;
base.OnMouseMove(e);
} protected override void OnMouseLeave(EventArgs e)
{
try
{
Graphics g = this.CreateGraphics();
if (index != -1 && this.TabPages[index] != this.SelectedTab)
{
g.FillRectangle(new SolidBrush(SelectedColor), this.GetTabRect(index));
SolidBrush brush = new SolidBrush(FontColor);
RectangleF tRectangleF = this.GetTabRect(index);
StringFormat sf = new StringFormat();//封装文本布局信息
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Near;
tRectangleF.X += TextLeft;
g.DrawString(this.Controls[index].Text, this.Font, brush, tRectangleF, sf);
using (Pen objpen = new Pen(Color.Black))
{
int tx = (int)(tRectangleF.X + (tRectangleF.Width - 30));
tRectangleF.X = tx - 2;
Point p5 = new Point(tx, 8);
Font font = new System.Drawing.Font("微软雅黑", 12);
g.DrawString("〇", font, brush, tRectangleF, sf);
font = new System.Drawing.Font("微软雅黑", 11);
tRectangleF.X = tx + 2;
tRectangleF.Y = tRectangleF.Y - 1;
g.DrawString("×", font, brush, tRectangleF, sf);
}
}
}
catch (Exception)
{
}
index = -1;
base.OnMouseLeave(e);
}
/// <summary>
/// 重绘控件
/// </summary>
/// <param name="sender"></param>
/// <param name="e"></param>
private void tabMenu_DrawItem(object sender, DrawItemEventArgs e)
{
this.SetStyle(
ControlStyles.UserPaint | // 控件将自行绘制,而不是通过操作系统来绘制
ControlStyles.OptimizedDoubleBuffer | // 该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁
ControlStyles.AllPaintingInWmPaint | // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁
ControlStyles.ResizeRedraw | // 在调整控件大小时重绘控件
ControlStyles.SupportsTransparentBackColor, // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明
true); // 设置以上值为 true
this.UpdateStyles();
} //关闭按钮功能
private void MainTabControl_MouseDown(object sender, MouseEventArgs e)
{
int closeSize = 20;
if (e.Button == MouseButtons.Left)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle tab = this.GetTabRect(this.SelectedIndex);
tab.Offset(tab.Width - (closeSize + 3), 4);
tab.Width = closeSize;
tab.Height = closeSize; if (this.TabCount == 1) return; //如果鼠标在区域内就关闭选项卡
bool isClose = x > tab.X && x < tab.Right && y > tab.Y && y < tab.Bottom;
if (isClose == true)
{
this.TabPages.Remove(this.SelectedTab);
}
}
} }
}

实例二:

using System.ComponentModel;
using System.Drawing;
using System.Windows.Forms; namespace KUN.CONTROL.LIB.CONTROL
{
public partial class KTabControl : System.Windows.Forms.TabControl
{
[Browsable(true), Description("整个控件的背景色"), Category("外观")]
public Color TabControlBackColor { get; set; } [Browsable(true), Description("Tab的标题栏边框颜色"), Category("外观")]
public Color TabBorderColor { get; set; } [Browsable(true), Description("当前激活Tab的标题栏背景色"), Category("外观")]
public Color ActivedTabBackColor { get; set; } [Browsable(true), Description("当前激活Tab的标题文字颜色"), Category("外观")]
public Color ActivedTabLabelColor { get; set; } [Browsable(true), Description("未激活Tab的标题栏背景色"), Category("外观")]
public Color InActivedTabBackColor { get; set; } [Browsable(true), Description("未激活Tab的标题文字颜色"), Category("外观")]
public Color InActivedTabLabelColor { get; set; } [Browsable(true), Description("Tab标题栏的大小"), Category("外观")]
public Size TabSize { get; set; } public KTabControl()
{
this.InitializeComponent(); TabSet(); this.TabBorderColor = Color.Black;
this.ActivedTabLabelColor = Color.Black;
this.InActivedTabLabelColor = Color.Black;
this.ActivedTabBackColor = Color.White;
this.InActivedTabBackColor = Color.FromArgb(0, 192, 192);
this.TabControlBackColor = Color.Transparent;
this.TabSize = new Size(100, 35);
} protected override void OnMouseDoubleClick(MouseEventArgs e)
{
if (this.TabPages.Count == 1) return;
this.TabPages.RemoveAt(this.SelectedIndex);
} protected override void OnPaint(PaintEventArgs e)
{
Rectangle tcRec = this.ClientRectangle;//整个tabControl的边框
e.Graphics.FillRectangle(new SolidBrush(this.TabControlBackColor), tcRec); for (int i = 0; i < this.TabPages.Count; i++)
{
Rectangle tabRectangle = new Rectangle(1, 1 + i * TabSize.Height, TabSize.Width, TabSize.Height);
SolidBrush brush = new SolidBrush(this.InActivedTabLabelColor);
StringFormat sf = new StringFormat();//封装文本布局信息
sf.LineAlignment = StringAlignment.Center;
sf.Alignment = StringAlignment.Center;
if (i == this.SelectedIndex)
{
brush = new SolidBrush(this.ActivedTabLabelColor);
e.Graphics.FillRectangle(new SolidBrush(ActivedTabBackColor), tabRectangle);
e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
}
else
{
e.Graphics.FillRectangle(new SolidBrush(InActivedTabBackColor), tabRectangle);
e.Graphics.DrawRectangle(new Pen(this.TabBorderColor), tabRectangle);
}
e.Graphics.DrawString(this.Controls[i].Text, this.Font, brush, tabRectangle, sf);
}
} /// <summary>
/// 设定控件绘制模式
/// </summary>
private void TabSet()
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.Alignment = TabAlignment.Left;
this.SizeMode = TabSizeMode.Fixed;
this.Multiline = true;
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor,
true);
base.UpdateStyles();
} public override Rectangle DisplayRectangle
{
get
{
Rectangle rect = base.DisplayRectangle;
return new Rectangle(rect.Left - 3, rect.Top - 3, rect.Width + 6, rect.Height + 5);
}
}
}
}

  

C# Winfrom TabControl美化的更多相关文章

  1. C# Winfrom UI 美化

    Winfrom UI 美化 此处只做演示,未进行页面布局.... 1.CSkin:此处只显示一种样式供参考,可继承其他样式——略 2.MetroFramework.Design 3.Ribbon 4. ...

  2. Winform 中tabcontrol 美化

    需要对tabcontrol按照美工出的图进行美化 对tabpage页进行标题设置,首先对整个tabcontrol的DrawMode设置为OwnerDrawFixed,由于需要对标题宽度有要求,设置si ...

  3. WPF TabControl美化

    <Window.Resources> <!-- TabItem的样式 --> <Style TargetType="{x:Type TabItem}" ...

  4. Winfrom UI 美化 MetroModernUI库应用实例

    使用方式: 选择项目==>右键==>管理NuGet安装包==>输入Metro==> ==>添加选项卡(自定义命名,例如Metrol UI)==>浏览 ==>加 ...

  5. C# winform 界面美化技巧(扁平化设计)

    关于C#界面美化的一些小技巧 在不使用第三方控件如 IrisSkin 的前提下,依然可以对winform做出让人眼前一亮的美化 首先,我们先来实现主界面的扁平化 此处分为两个步骤,第一步是更改winf ...

  6. WPF自定义TabControl样式

    WPF自定义TabControl,TabControl美化 XAML代码: <TabControl x:Class="SunCreate.Common.Controls.TabCont ...

  7. Winform 美化

    首先,我们先来实现主界面的扁平化 此处分为两个步骤,第一步是更改winform自带的MainForm窗体属性,第二步是添加窗体事件. 将主窗体FormBorderStyle更改为None,这样就得到了 ...

  8. 使用Blend设计出符合效果的WPF界面

    之前不会用blend,感觉好难的,但美工给出的效果自己有没办法实现,所以研究了一下blend,感觉没有想象中的那么难 废话不多说,开始界面设计 今天拿到美工给的一个界面效果图 这个界面说实话,还可以吧 ...

  9. TabControl控件的美化

    文件下载:http://files.cnblogs.com/zfanlong1314/TabControlEX.rar 本文转载:http://www.cnblogs.com/lmlblog/arch ...

随机推荐

  1. linux操作备份

    ---------------------------------------------------------------------------------------------------- ...

  2. golang web框架设计3:controller设计

    继续学习golang web框架设计 controller作用 MVC设计模式里面的这个C,控制器. Model是后台返回的数据: View是渲染页面,通常是HTML的模板页面: Controller ...

  3. Jmeter 逻辑控制器 之 While Controller

    一.认识 While Controller 如下图,创建一个While Controller (While 循环控制器) 设置界面如下: Condition (function or variable ...

  4. OPC Utgard的数据访问方式

    1.同步读取某个点位的值 Item项的read()方法 Server server = new Server(BaseConfiguration.getCLSIDConnectionInfomatio ...

  5. SQL中有关DQL、DML、DDL、DCL的概念与区别?

    SQL(Structure Query Language)结构化查询语言是数据库的核心语言,是高级的非过程化编程语言.它功能强大,效率高,简单易学易维护.SQL语言基本上独立于数据库本身.使用的机器. ...

  6. 【ES 系列1】介绍与方案设计

    简介 ElasticSearch是一个高度可扩展的开源全文搜索和分析引擎.它允许您快速.近实时地存储.搜索和分析大量数据.它通常被用作驱动具有复杂搜索功能和需求的应用程序的底层引擎/技术.适用于需要大 ...

  7. 【ARM-Linux开发】Linux链接

    链接有两种方式:硬链接和软链接. (一)软链接 软链接又叫做符号链接.基本命令为: [plain] view plain copy ln -s sourcePlace newPlace 软链接可以链接 ...

  8. 【编程开发】非对称加密过程详解(基于RSA非对称加密算法实现)

    1.非对称加密过程:         假如现实世界中存在A和B进行通讯,为了实现在非安全的通讯通道上实现信息的保密性.完整性.可用性(即信息安全的三个性质),A和B约定使用非对称加密通道进行通讯,具体 ...

  9. 洛谷 题解 P2010 【回文日期】

    因为有8个字符,所以可得出每一年只有一个回文日期. 因此只要判断每一年就行了. 做法: 我们先把年倒过来,例如2018年就倒为8102,就得出8102就是回文日期的后四个字符,我们只要判断一下有没有这 ...

  10. poj1222(高斯消元法解异或方程组+开关问题)

    题目链接:https://vjudge.net/problem/POJ-1222 题意:给定一个5×6的01矩阵,改变一个点的状态时它上下左右包括它自己的状态都会翻转,因为翻转2次等价与没有翻转,那么 ...