using System.Drawing;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Runtime.InteropServices;
using System;
using System.Drawing.Text;
using System.ComponentModel; namespace ControlExs.ControlExs.CTabControl
{
public class CTabControl : TabControl
{
public CTabControl()
: base()
{
SetStyles();
} private void SetStyles()
{
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.DoubleBuffer |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor, true);
base.UpdateStyles();
} private Color _backColor = Color.FromArgb(, , );
[Browsable(true)]
[EditorBrowsable(EditorBrowsableState.Always)]
[DefaultValue(typeof(Color), "23, 169, 254")]
public override Color BackColor
{
get { return _backColor; }
set
{
_backColor = value;
base.Invalidate(true);
}
} private Color _borderColor = Color.FromArgb(, , );
[DefaultValue(typeof(Color), "23, 169, 254")]
[Description("TabContorl边框色")]
public Color BorderColor
{
get { return _borderColor; }
set
{
_borderColor = value;
base.Invalidate(true);
}
} private Color _headSelectedBackColor = Color.FromArgb(, , );
[DefaultValue(typeof(Color), "23, 169, 254")]
[Description("TabPage头部选中后的背景颜色")]
public Color HeadSelectedBackColor
{
get { return _headSelectedBackColor; }
set { _headSelectedBackColor = value; }
} private Color _headSelectedBorderColor = Color.FromArgb(, , );
[DefaultValue(typeof(Color), "23, 169, 254")]
[Description("TabPage头部选中后的边框颜色")]
public Color HeadSelectedBorderColor
{
get { return _headSelectedBorderColor; }
set { _headSelectedBorderColor = value; }
} private Color _headerBackColor = Color.FromArgb(, , );
[DefaultValue(typeof(Color), "23, 169, 254")]
[Description("TabPage头部默认边框颜色")]
public Color HeaderBackColor
{
get { return _headerBackColor; }
set { _headerBackColor = value; }
} protected override void OnPaintBackground(PaintEventArgs pevent)
{
if (this.DesignMode == true)
{
LinearGradientBrush backBrush = new LinearGradientBrush(
this.Bounds,
SystemColors.ControlLightLight,
SystemColors.ControlLight,
LinearGradientMode.Vertical);
pevent.Graphics.FillRectangle(backBrush, this.Bounds);
backBrush.Dispose();
}
else
{
this.PaintTransparentBackground(pevent.Graphics, this.ClientRectangle);
}
} /// <summary>
/// TabContorl 背景色设置
/// </summary>
/// <param name="g"></param>
/// <param name="clipRect"></param>
protected void PaintTransparentBackground(Graphics g, Rectangle clipRect)
{
if ((this.Parent != null))
{
clipRect.Offset(this.Location);
PaintEventArgs e = new PaintEventArgs(g, clipRect);
GraphicsState state = g.Save();
g.SmoothingMode = SmoothingMode.HighSpeed;
try
{
g.TranslateTransform((float)-this.Location.X, (float)-this.Location.Y);
this.InvokePaintBackground(this.Parent, e);
this.InvokePaint(this.Parent, e);
}
finally
{
g.Restore(state);
clipRect.Offset(-this.Location.X, -this.Location.Y);
//新加片段,待测试
using (SolidBrush brush = new SolidBrush(_backColor))
{
clipRect.Inflate(, );
g.FillRectangle(brush, clipRect);
}
}
}
else
{
System.Drawing.Drawing2D.LinearGradientBrush backBrush = new System.Drawing.Drawing2D.LinearGradientBrush(this.Bounds, SystemColors.ControlLightLight, SystemColors.ControlLight, System.Drawing.Drawing2D.LinearGradientMode.Vertical);
g.FillRectangle(backBrush, this.Bounds);
backBrush.Dispose();
}
} protected override void OnPaint(PaintEventArgs e)
{
// Paint the Background
base.OnPaint(e);
this.PaintTransparentBackground(e.Graphics, this.ClientRectangle);
this.PaintAllTheTabs(e);
this.PaintTheTabPageBorder(e);
this.PaintTheSelectedTab(e);
} private void PaintAllTheTabs(System.Windows.Forms.PaintEventArgs e)
{
if (this.TabCount > )
{
for (int index = ; index < this.TabCount; index++)
{
this.PaintTab(e, index);
}
}
} private void PaintTab(System.Windows.Forms.PaintEventArgs e, int index)
{
GraphicsPath path = this.GetPath(index);
this.PaintTabBackground(e.Graphics, index, path);
this.PaintTabBorder(e.Graphics, index, path);
this.PaintTabText(e.Graphics, index);
this.PaintTabImage(e.Graphics, index);
} /// <summary>
/// 设置选项卡头部颜色
/// </summary>
/// <param name="graph"></param>
/// <param name="index"></param>
/// <param name="path"></param>
private void PaintTabBackground(System.Drawing.Graphics graph, int index, System.Drawing.Drawing2D.GraphicsPath path)
{
Rectangle rect = this.GetTabRect(index);
System.Drawing.Brush buttonBrush = new System.Drawing.Drawing2D.LinearGradientBrush(rect, SystemColors.ControlLightLight, SystemColors.ControlLight, LinearGradientMode.Vertical); //非选中时候的 TabPage 页头部背景色
//System.Drawing.Brush buttonBrush = new System.Drawing.SolidBrush(_headerBackColor);
if (index == this.SelectedIndex)
{
//buttonBrush = new System.Drawing.SolidBrush(SystemColors.ControlLightLight); // TabPage 选中时候页头部背景色
buttonBrush = new System.Drawing.SolidBrush(_headSelectedBackColor);
}
graph.FillPath(buttonBrush, path);
buttonBrush.Dispose();
} /// <summary>
/// 设置选项卡头部边框色
/// </summary>
/// <param name="graph"></param>
/// <param name="index"></param>
/// <param name="path"></param>
private void PaintTabBorder(System.Drawing.Graphics graph, int index, System.Drawing.Drawing2D.GraphicsPath path)
{
//Pen borderPen = new Pen(SystemColors.ControlDark);
Pen borderPen = new Pen(_borderColor);// TabPage 非选中时候的 TabPage 头部边框色
if (index == this.SelectedIndex)
{
//borderPen = new Pen(ThemedColors.ToolBorder);
borderPen = new Pen(_headSelectedBorderColor); // TabPage 选中后的 TabPage 头部边框色
}
graph.DrawPath(borderPen, path);
borderPen.Dispose();
} private void PaintTabImage(System.Drawing.Graphics g, int index)
{
Image tabImage = null;
if (this.TabPages[index].ImageIndex > - && this.ImageList != null)
{
tabImage = this.ImageList.Images[this.TabPages[index].ImageIndex];
}
else if (this.TabPages[index].ImageKey.Trim().Length > && this.ImageList != null)
{
tabImage = this.ImageList.Images[this.TabPages[index].ImageKey];
}
if (tabImage != null)
{
Rectangle rect = this.GetTabRect(index);
g.DrawImage(tabImage, rect.Right - rect.Height - , , rect.Height - , rect.Height - );
}
} private void PaintTabText(System.Drawing.Graphics graph, int index)
{
Rectangle rect = this.GetTabRect(index); Rectangle rect2 = new Rectangle(rect.Left, rect.Top, rect.Width, rect.Height);
//Rectangle rect2 = new Rectangle(rect.Left + 16, rect.Top + 1, rect.Width - 6, rect.Height); //if (index == 0)
//{
// rect2 = new Rectangle(rect.Left + rect.Height, rect.Top + 1, rect.Width - rect.Height, rect.Height);
//} string tabtext = this.TabPages[index].Text; System.Drawing.StringFormat format = new System.Drawing.StringFormat();
format.Alignment = StringAlignment.Near;
format.LineAlignment = StringAlignment.Center;
format.Trimming = StringTrimming.EllipsisCharacter; Brush forebrush = null; if (this.TabPages[index].Enabled == false)
{
forebrush = SystemBrushes.ControlDark;
}
else
{
forebrush = SystemBrushes.ControlText;
} Font tabFont = this.Font;
if (index == this.SelectedIndex)
{
tabFont = new Font(this.Font, FontStyle.Bold);
//if (index == 0)
//{
// rect2 = new Rectangle(rect.Left + rect.Height, rect.Top + 1, rect.Width - rect.Height + 5, rect.Height);
//}
}
graph.DrawString(tabtext, tabFont, forebrush, rect2, format);
} /// <summary>
/// 设置 TabPage 内容页边框色
/// </summary>
/// <param name="e"></param>
private void PaintTheTabPageBorder(System.Windows.Forms.PaintEventArgs e)
{
if (this.TabCount > )
{
Rectangle borderRect = this.TabPages[].Bounds;
borderRect.Inflate(, );
//ControlPaint.DrawBorder(e.Graphics, borderRect, ThemedColors.ToolBorder, ButtonBorderStyle.Solid);
ControlPaint.DrawBorder(e.Graphics, borderRect, this.BorderColor, ButtonBorderStyle.Solid);
}
} /// <summary>
/// // TabPage 页头部间隔色
/// </summary>
/// <param name="e"></param>
private void PaintTheSelectedTab(System.Windows.Forms.PaintEventArgs e)
{
Rectangle selrect;
int selrectRight = ; switch (this.SelectedIndex)
{
case -:
break;
case :
selrect = this.GetTabRect(this.SelectedIndex);
selrectRight = selrect.Right;
e.Graphics.DrawLine(SystemPens.ControlLightLight, selrect.Left + , selrect.Bottom + , selrectRight - , selrect.Bottom + );
break;
default:
selrect = this.GetTabRect(this.SelectedIndex);
selrectRight = selrect.Right;
e.Graphics.DrawLine(SystemPens.ControlLightLight, selrect.Left + - selrect.Height, selrect.Bottom + , selrectRight - , selrect.Bottom + );
break;
}
} private GraphicsPath GetPath(int index)
{
System.Drawing.Drawing2D.GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.Reset(); Rectangle rect = this.GetTabRect(index); switch (Alignment)
{
case TabAlignment.Top: break;
case TabAlignment.Bottom: break;
case TabAlignment.Left: break;
case TabAlignment.Right: break;
} if (index == )
{
path.AddLine(rect.Left - , rect.Top + , rect.Left - , rect.Top + );
path.AddLine(rect.Left - , rect.Top + , rect.Right, rect.Top + );
path.AddLine(rect.Right, rect.Top + , rect.Right, rect.Bottom);
path.AddLine(rect.Right, rect.Bottom, rect.Left - , rect.Bottom);
}
else
{
if (index == this.SelectedIndex)
{
path.AddLine(rect.Left - , rect.Top, rect.Left - , rect.Top);
path.AddLine(rect.Left - , rect.Top, rect.Right, rect.Top);
path.AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom);
path.AddLine(rect.Right, rect.Bottom + , rect.Left - , rect.Bottom + );
}
else
{
path.AddLine(rect.Left - , rect.Top, rect.Left - , rect.Top);
path.AddLine(rect.Left - , rect.Top, rect.Right, rect.Top);
path.AddLine(rect.Right, rect.Top, rect.Right, rect.Bottom);
path.AddLine(rect.Right, rect.Bottom + , rect.Left - , rect.Bottom + );
}
}
return path;
} [DllImport("user32.dll")]
private static extern IntPtr SendMessage(IntPtr hWnd, int Msg, IntPtr wParam, IntPtr lParam); private const int WM_SETFONT = 0x30;
private const int WM_FONTCHANGE = 0x1d; protected override void OnCreateControl()
{
base.OnCreateControl();
this.OnFontChanged(EventArgs.Empty);
} protected override void OnFontChanged(EventArgs e)
{
base.OnFontChanged(e);
IntPtr hFont = this.Font.ToHfont();
SendMessage(this.Handle, WM_SETFONT, hFont, (IntPtr)(-));
SendMessage(this.Handle, WM_FONTCHANGE, IntPtr.Zero, IntPtr.Zero);
this.UpdateStyles();
//this.ItemSize = new Size(0, this.Font.Height + 2);
}
}
}
C# 自定义重绘TabControl的更多相关文章
-
C# 重绘tabControl,添加关闭按钮(页签)
C# 重绘tabControl,添加关闭按钮(页签) 调用方法 参数: /// <summary> /// 初始化 /// </summary> /// <param n ...
-
C#重绘TabControl
C#重绘TabControl的Tabpage标签,添加图片及关闭按钮 Code highlighting produced by Actipro CodeHighlighter (freeware)h ...
-
重绘TabControl
本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...
-
WinForm中重绘TabControl选项卡标题
最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...
-
C# 重绘tabControl,添加关闭按钮(续)
在上一篇随笔中,添加关闭按钮是可以实现 ,但细心一点就会发现,每次关闭一个选项卡,tableControl都会自动跳到第一个页面,显然 这不是我们想要的,为此,我修改了部分的代码.除此之外,我还添加了 ...
-
C# 自定义重绘TextBox
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...
-
C# 自定义重绘DataGridView
using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...
-
TabControl控件重绘
原文地址:http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition 在网上看到重绘TabContr ...
-
Windows开发进阶之VC++中如何实现对话框的界面重绘
技术:Windows 系统+Visual studio 2008 概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...
随机推荐
-
iOS图片拉伸技巧
纵观移动市场,一款移动app,要想长期在移动市场立足,最起码要包含以下几个要素:实用的功能.极强的用户体验.华丽简洁的外观.华丽外观的背后,少不了美工的辛苦设计,但如果开发人员不懂得怎么合理展示这些设 ...
-
【JAVA小结】字符串比较是否相等
public class CompareObject1 { public static void main(String[] args) { String str1 = new String(&quo ...
-
Greedy:Protecting the Flowers(POJ 3262)
保护花朵 题目大意:就是农夫有很多头牛在践踏花朵,这些牛每分钟破坏D朵花,农夫需要把这些牛一只一只运回去,这些牛各自离牛棚都有T的路程(有往返,而且往返的时候这只牛不会再破坏花),问怎么运才能使被践踏 ...
-
SQL Server磁盘I/O性能分析
SQL Server中的I/O操作类型: 1.对于内存中没有缓存的数据,第一次访问时需要将数据从所在的页面从数据文件中读取到内存中 2.在任何Insert/Update/Delete提交前,SQL S ...
-
python正则详解
正则表达式概述 正则表达式,又称正规表示式.正规表示法.正规表达式.规则表达式.常规表示法(英语:Regular Expression,在代码中常简写为regex.regexp或RE),是计算机科学的 ...
-
python爬微信公众号前10篇历史文章(2)-拼接URL&;发送http请求
如何拼接想要的url http://weixin.sogou.com/weixin?type=1&page=1&ie=utf8&query=%E5%A4%A7%E7%BA%BD ...
-
JQuery系统梳理
JQuery在前端网页开发中可以说是非常常用了,它所拥有的强大功能足以让我们完成各式各样的效果. 一.JQuery基础语法 1. 使用JQuery必须先导入jquery.x.x.x.js文件: 2. ...
-
EF CodeFirst系列(6)---配置1对1,1对多,多对多关系
这一节介绍EF CodeFirst模式中的1对0/1,1对多,多对多关系的配置,只有梳理清楚实体间的关系,才能进行愉快的开发,因此这节虽然很简单但是还是记录了一下. 1. 1对0/1关系配置 1. 通 ...
-
Vi命令:如何删除全部内容
Vi命令:如何删除全部内容? 在命令模式下,输入:.,$d 一回车就全没了. 表示从当前行到末行全部删除掉. 用gg表示移动到首行.
-
互评Beta版本(Hello World!——SkyHunter)
1 基于NABCD评论作品,及改进建议 SkyHunter这款游戏我很喜欢,小时候总玩飞机类的游戏,这款游戏我上课的时候试玩了,在我电脑上运行是很好玩的,音乐震撼,画面玄幻,富有金属音乐的味道,游戏内 ...