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. 【转】 ASP.NET网站路径中~(波浪线)解释

    刚开始学习ASP.NET的朋友可能会不理解路径中的-符代表什么,例如ImageUrl=”~/Images/SampleImage.jpg” 现在我们看看-代表什么意思.-是ASP.NET 的Web 应 ...

  2. Objective-C 学习笔记(1)

    文件描述: .h 类的声明文件,用户声明变量.函数(方法) .m 类的实现文件,用户实现.h中的函数(方法) 类的声明使用关键字 @interface.@end 类的实现使用关键字@implement ...

  3. oracle学习 十一 包+复合类型+自定义异常(持续更新)

    在这里讲一下包的概念, 二话不说上个例子 包头: create or replace package pck_test is procedure proc_report_salary(name nva ...

  4. emWin(ucGui)的Edit控件退格处理方法 worldsing

    在enWin(ucGui)中EDIT控件在数值模式(十进制/十六进制/二进制/浮点数)下编辑是,无法使用BackSpace键进行退格,主要涉及到的函数有: EDIT_SetBinMode() EDIT ...

  5. 创建类模式(五):单例(Singleton)

    定义 确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例. 单例模式一般情况下通过使用private的构造函数确保了在一个应用中只产生一个实例,并且是自行实例化. 和静态变量的区别 虽然 ...

  6. 用一行代码初始化ArrayList

    方法1: ArrayList<String> arrList1 = (ArrayList<String>) Arrays.asList("Buenos Aires&q ...

  7. 根据PID和VID得到USB转串口的串口号

    /******************************************************************************* * * FindAppUART.cpp ...

  8. MVVM知识库总结

    1.关于MVVM概念的理解和实际运用中的解析: http://www.cnblogs.com/hielvis/archive/2011/03/22/1991959.html http://msdn.m ...

  9. 初步认识pg_control文件之一

    这个据说是PostgreSQL的control file. 到底如何呢,先看看改名后如何,把pg_control文件改名,然后启动 Postgres,运行时得到信息: [postgres@pg101 ...

  10. 通过RDB还原用户误删除的邮件

    1.在任意一台邮箱服务器上新建RDB数据库目录:C:\rdb01 2.使用Windows Server Backup还原已备份的邮件数据(mbdb02)库到 C:\rdb01 目录下 3.创建RDB数 ...