C# 自定义重绘TabControl
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 概述 应用程序界面是用户与应用程序之间的交互的桥梁和媒介,用户界面是应用程序中最重要的组成部分,也是最为直观的视觉体现.对用户而言 ...
随机推荐
- mediawiki 的使用 2
要想外部电脑能访问你的网站,网站部署好后,在LocalSettings.php 里将这句 $wgServer = "http://localhost"; 改成 $wgServer ...
- python GUI初步
- component to string
component to string string to component ObjectTextToBinary ObjectBinaryToText ReadComponent #include ...
- 一些推荐的spark/hadoop课程
为了分享给你们,也为自己. 感谢下面的老师们! 1.王家林DT大数据梦工厂的大数据IMF传奇行动课程 总的目录是: 第一阶段:Linux和Java零基础企业级实战 第二阶段:Hadoop和Hive零基 ...
- HDU 5867 Water problem (模拟)
Water problem 题目链接: http://acm.split.hdu.edu.cn/showproblem.php?pid=5867 Description If the numbers ...
- HD1085 Holding Bin-Laden Captive!
Problem Description We all know that Bin-Laden is a notorious terrorist, and he has disappeared for ...
- javascript日期格式化方法汇总
本文给大家汇总介绍了javascript格式化日期时间的几种常用方法,个人对最后一种个性化输出时间比较有兴趣,基本上只要项目中能用到都是使用这种,推荐给小伙伴们. 方法一: ? 1 2 3 4 5 6 ...
- How to create custom methods for use in spring security expression language annotations
From:http://stackoverflow.com/questions/6632982/how-to-create-custom-methods-for-use-in-spring-secur ...
- <!DOCTYPE> 标签的深度剖析以及使用选择
前言: 今天被问道“有没有仔细了解过<!DOCTYPE>标签?”,愣了一下,因为一开始在W3cschool上看到过建议使用XHTML Transitional DTD,之后就很听话地把Dr ...
- 【转】Android TouchEvent事件传递机制
Android TouchEvent事件传递机制 事件机制参考地址: http://www.cnblogs.com/sunzn/archive/2013/05/10/3064129.html ht ...