using System;
using System.Collections.Generic;
using System.Text;
using System.Windows.Forms;
using System.Drawing;
using System.Drawing.Imaging;
using System.Drawing.Text;
using System.ComponentModel;
// by conmajia@gmail.com
namespace ryControls
{
///
/// 多标签控件
///
[ToolboxBitmap(typeof(System.Windows.Forms.TabControl))]
public class TabControlEx : TabControl
{
#region Status Variables
readonly bool mouseDown = false;
//bool hijackParent = false;
#endregion
#region Graphics Variables
Image icon;
ImageAttributes ia;
readonly ColorMatrix cm;
readonly float[][] colorMatrix ={
new float[]{0.299f, 0.299f, 0.299f, 0, 0},
new float[]{0.587f, 0.587f, 0.587f, 0, 0},
new float[]{0.114f, 0.114f, 0.114f, 0, 0},
new float[]{0, 0, 0, 1, 0},
new float[]{0, 0, 0, 1, 0}};
#endregion
#region Objects
//Form parent;
//int FORM_DELTA = 20;
//int elapsed = 0;
//Timer timer;
//int lastLowerBound = 0;
//int currentLowerBound = 0;
#endregion
#region Properties
#endregion
#region Initiates
///
///
///
public TabControlEx()
{
SetStyles();
ia = new ImageAttributes();
cm = new ColorMatrix(colorMatrix);
ia.SetColorMatrix(cm, ColorMatrixFlag.Default, ColorAdjustType.Bitmap);
icon = new Bitmap(1, 1);
TabBackground = new Bitmap(this.GetType(), "TabButtonBackground.bmp");
//this.ItemSize = new Size(44, tabBackground.Height);
}
private TabBar tabbar;
///
/// 获取对应的标签
///
[Browsable(false)]
public TabBar TabBar
{
get
{
return this.tabbar;
}
}
internal TabBar InternalTabBar
{
get
{
return this.tabbar;
}
set
{
this.tabbar = value;
}
}
///
///
///
public Image TabBackground { get; set; }
#endregion
#region Private UI Methods
private void SetStyles()
{
base.SetStyle(
ControlStyles.UserPaint |
ControlStyles.OptimizedDoubleBuffer |
ControlStyles.AllPaintingInWmPaint |
ControlStyles.ResizeRedraw |
ControlStyles.SupportsTransparentBackColor,
true);
base.UpdateStyles();
}
// 计算控件底线
private void SetLowBound(Control container)
{
int tmp = 0;
Control c = null;
for (int i = 0; i < container.Controls.Count; i++)
{
c = container.Controls[i];
if (tmp < c.Bottom)
tmp = c.Bottom;
}
container.Tag = tmp;
}
#endregion
#region Tab Overrides
///
///
///
///
protected override void OnControlAdded(ControlEventArgs e)
{
SetLowBound(e.Control);
}
///
///
///
///
protected override void OnSelecting(TabControlCancelEventArgs e)
{
}
///
///
///
///
protected override void OnSelected(TabControlEventArgs e)
{
//parent.Text = e.TabPage.Text;
}
///
///
///
///
protected override void OnParentChanged(EventArgs e)
{
//if (parent == null)
// parent = this.FindForm();
//parent.Text = this.TabPages[0].Text;
}
///
///
///
public Color TabBackColor
{
get; set;
} = Color.White;
#endregion
#region Paint Override
///
///
///
///
protected override void OnPaint(PaintEventArgs e)
{
Rectangle TabControlArea = this.ClientRectangle;
Rectangle TabArea = new Rectangle(3, 24, this.ClientRectangle.Width - 7, this.ClientRectangle.Height - 28);// this.DisplayRectangle;
//----------------------------
// 控件内部颜色
Brush br = new SolidBrush(TabBackColor);
e.Graphics.FillRectangle(br, TabControlArea);
br.Dispose();
e.Graphics.TextRenderingHint
= TextRenderingHint.AntiAlias;
for (int i = 0; i < this.TabCount; i++)
{
if (this.SelectedIndex == i)
{
if (TabBackground != null)
e.Graphics.DrawImage(TabBackground, this.GetTabRect(i));
}
if (this.ImageList != null && (this.TabPages[i].ImageIndex > -1 || !string.IsNullOrEmpty(this.TabPages[i].ImageKey)))
{
if (this.ImageList.Images.Count > 0)
{
if (this.TabPages[i].ImageIndex > -1)
{
if (this.TabPages[i].ImageIndex < this.ImageList.Images.Count)
{
icon = this.ImageList.Images[this.TabPages[i].ImageIndex];
}
else
{
//icon = this.ImageList.Images[0];
}
}
else
if (!string.IsNullOrEmpty(this.TabPages[i].ImageKey) && this.ImageList.Images.ContainsKey(this.TabPages[i].ImageKey))
icon = this.ImageList.Images[this.TabPages[i].ImageKey];
if (mouseDown && (this.SelectedIndex != i))
e.Graphics.DrawImage(
icon,
new Rectangle(
this.GetTabRect(i).X + (this.GetTabRect(i).Width - icon.Width) / 2,
this.GetTabRect(i).Y,
icon.Width,
icon.Height),
0,
0,
icon.Width,
icon.Height,
GraphicsUnit.Pixel,
ia);
else
e.Graphics.DrawImage(
icon,
this.GetTabRect(i).X + (this.GetTabRect(i).Width - icon.Width) / 2,
this.GetTabRect(i).Y);
}
}
SizeF textSize
= e.Graphics.MeasureString(this.TabPages[i].Text, this.Font);
e.Graphics.DrawString(
this.TabPages[i].Text,
this.Font,
SystemBrushes.ControlLightLight,
this.GetTabRect(i).X + (this.GetTabRect(i).Width - textSize.Width) / 2 + 1,
this.GetTabRect(i).Y + 40);
e.Graphics.DrawString(
this.TabPages[i].Text,
this.Font,
SystemBrushes.ControlText,
this.GetTabRect(i).X + (this.GetTabRect(i).Width - textSize.Width) / 2,
this.GetTabRect(i).Y + 39);
}
}
#endregion
}
}