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的更多相关文章

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

    C# 重绘tabControl,添加关闭按钮(页签) 调用方法 参数: /// <summary> /// 初始化 /// </summary> /// <param n ...

  2. C#重绘TabControl

    C#重绘TabControl的Tabpage标签,添加图片及关闭按钮 Code highlighting produced by Actipro CodeHighlighter (freeware)h ...

  3. 重绘TabControl

    本文转载自:http://blog.csdn.net/conmajia/article/details/7596718 作者:野比 (conmajia@gmail.com) 时间:May, 2012 ...

  4. WinForm中重绘TabControl选项卡标题

    最近开发WinForm频繁使用了TabControl控件,这个控件的选项卡没有BackgroundImage这个属性,那么如何为其各个选项卡添加背景图片呢?(这里说的是每个TabPage的头部,也就是 ...

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

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

  6. C# 自定义重绘TextBox

    using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.D ...

  7. C# 自定义重绘DataGridView

    using System.Collections.Generic; using System.ComponentModel; using System.Diagnostics; using Syste ...

  8. TabControl控件重绘

    原文地址:http://www.codeproject.com/Articles/91387/Painting-Your-Own-Tabs-Second-Edition 在网上看到重绘TabContr ...

  9. Windows开发进阶之VC++中如何实现对话框的界面重绘

    技术:Windows 系统+Visual studio 2008   概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...

随机推荐

  1. javascript压缩图片无刷新上传

    首先要说明,该方式我没有实现成功,主要问题点是压缩后能拿到压缩后图片地址,确不知道怎么上传上去,如果有哪位大牛可以指点一二,万分感谢. 图片压缩代码: compress: function(sourc ...

  2. 数据库中使用 Synonym和openquery

    如果,你想在一台数据库服务器上,查询另一个台数据服务器的数据该如何做呢?如果,你想在同一台数据服务器上,在不同的数据库之间查询数据,又该怎么办呢?那就让我为你介绍Synonym和openquery吧. ...

  3. CST和GMT时间的区别

    CST和GMT时间的区别 今天遇到一个奇怪的问题,在服务器端通过 c# 获取当前时间为 Fri Aug 28 09:37:46 CST 2009, 转化为 GMT时间为:28 Aug 2009 01: ...

  4. CSS3每日一练之选择器-结构性伪类选择器

    <!DOCTYPE HTML> <html> <head> <meta charset="gb2312"> <title> ...

  5. SpriteParticle II

    [SpriteParticle II] 1.Randomizing the Starting Position 2.Setting the Initial Angle 3.Setting a Part ...

  6. POJ-2785 4 Values whose Sum is 0(折半枚举 sort + 二分)

    题目链接:http://poj.org/problem?id=2785 题意是给你4个数列.要从每个数列中各取一个数,使得四个数的sum为0,求出这样的组合的情况个数. 其中一个数列有多个相同的数字时 ...

  7. How Tomcat Works(十三)

    本文分析tomcat容器的安全管理,servlet技术支持通过配置部署描述器(web.xml文件)来对受限内容进行访问控制:servlet容器是通过一个名为验证器的阀来支持安全限制的,当servlet ...

  8. 解决数据库datatime数据在DataGridView里不显示秒的解决

    在数据库中正确显示有分有秒,到dataset里的时候也有,但绑定到DataGridView里的时候就没有秒,解决办法: dataGridView1.Columns["record_time& ...

  9. 传统的Ado.net 参数设置:params SqlParameter[] commandParameters

    C#代码  ExecuteReader(string connectionString, CommandType commandType, string commandText, params Sql ...

  10. EF小节

    EF学习笔记——生成自定义实体类 http://blog.csdn.net/leftfist/article/details/24889819 --工具: 1.entity developer 2.D ...