110,536
社区成员
发帖
与我相关
我的任务
分享
[ToolboxBitmap(typeof(TabControl))]
public partial class Tab : TabControl
{
static Image img;
public Tab():base()
{
//this.SetStyle(ControlStyles.SupportsTransparentBackColor, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.UserPaint, true);
this.ItemSize = new Size(0,15);
this.ResizeRedraw = true;
img = ControlCommon.GetImageFromResource("eTab.png");//GetResBitmap("Tangxu.Controls.Images.eTab.png");
}
~Tab()
{
if (img != null)
{
img.Dispose();
}
}
protected override void OnPaintBackground(PaintEventArgs pevent)
{
this.InvokePaintBackground(this.Parent, pevent);
this.InvokePaint(this.Parent, pevent);
}
protected override void OnPaint(PaintEventArgs e)
{
this.PaintAllTheTabs(e);
this.PaintTheTabPageBorder(e);
}
private void PaintAllTheTabs(System.Windows.Forms.PaintEventArgs e)
{
if (this.TabCount > 0)
{
for (int index = 0; index < this.TabCount; index++)
{
this.PaintTab(e, index);
}
}
}
private void PaintTab(System.Windows.Forms.PaintEventArgs e, int index)
{
this.PaintTabBackground(e.Graphics, index);
this.PaintTabText(e.Graphics, index);
}
private void PaintTheTabPageBorder(System.Windows.Forms.PaintEventArgs e)
{
if (this.TabCount > 0)
{
Rectangle borderRect = this.TabPages[0].Bounds;
borderRect.Inflate(1, 1);
ControlPaint.DrawBorder(e.Graphics, borderRect,ControlCommon.BorderColor, ButtonBorderStyle.Solid);
}
}
#region Draw TabItem Background
private void PaintTabBackground(System.Drawing.Graphics graph, int index)
{
Rectangle rect = this.GetTabRect(index);
if (index == this.SelectedIndex)
{
if (index == 0)
{
graph.DrawImage(img, new Rectangle(rect.X + 2, rect.Y, rect.Width - 2, 16), new Rectangle(0, 0, 91, 21), GraphicsUnit.Pixel);
}
else
{
graph.DrawImage(img, new Rectangle(rect.X, rect.Y, rect.Width, 16), new Rectangle(0, 0, 91, 21), GraphicsUnit.Pixel);
}
}
else
{
if (index == 0)
{
graph.DrawImage(img, new Rectangle(rect.X + 2, rect.Y, rect.Width - 2, 16), new Rectangle(91, 0, 80, 21), GraphicsUnit.Pixel);
}
else
{
graph.DrawImage(img, new Rectangle(rect.X, rect.Y, rect.Width, 16), new Rectangle(91, 0, 80, 21), GraphicsUnit.Pixel);
}
}
}
#endregion
private void PaintTabText(System.Drawing.Graphics graph, int index)
{
Rectangle rect = this.GetTabRect(index);
Rectangle rect2 = new Rectangle(rect.X + 8, rect.Top + 2, rect.Width - rect.Height, rect.Height);
if (index >= 0)
{
rect2 = new Rectangle(rect.X+8, rect.Top+2, rect.Width - 6, rect.Height);
}
if (index == this.SelectedIndex)
{
if (index >= 0)
{
rect2 = new Rectangle(rect.X + 8, rect.Top, rect.Width - 6, rect.Height);
}
else
{
rect2 = new Rectangle(rect.X + 8, rect.Top , 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;
SolidBrush forebrush = new SolidBrush(this.ForeColor);
graph.DrawString(tabtext, this.Font, forebrush, rect2, format);
forebrush.Dispose();
}
#region Get Image from Resouce File
protected Bitmap GetResBitmap(string str)
{
Stream sm;
sm = FindStream(str);
if (sm == null) return null;
return new Bitmap(sm);
}
protected Stream FindStream(string str)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string[] resNames = assembly.GetManifestResourceNames();
foreach (string s in resNames)
{
if (s == str)
{
return assembly.GetManifestResourceStream(s);
}
}
return null;
}
#endregion
}