317 lines
12 KiB
C#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using System.Drawing.Drawing2D;
using System.Drawing.Imaging;
namespace AMT_SW_GUI
{
public partial class AMTFlatTabControl : TabControl
{
#region Public Methods
public AMTFlatTabControl()
{
this.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
this.SetStyle(ControlStyles.UserPaint, true);
this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
this.SetStyle(ControlStyles.AllPaintingInWmPaint, true);
}
#endregion Public Methods
#region Protected Methods
private Bitmap PaintTabHeader(Size size, string tabHeaderText, Font textFont, Color backgroundColor, Color tabBackcolorTop, Color tabBackcolorBottom, Color textColor, Color tabBorderColor)
{
Bitmap bm = new Bitmap(size.Width, size.Height, PixelFormat.Format32bppArgb);
Graphics g = Graphics.FromImage(bm);
Rectangle rect = new Rectangle(0, 0, size.Width, size.Height);
const int cornerSize = 10;
const float sweepAngle = 90.0f;
SolidBrush backgroundBrush;
SolidBrush textBrush;
LinearGradientBrush backcolorBrush;
Pen borderPen;
g.SmoothingMode = SmoothingMode.AntiAlias;
//Draw background
backgroundBrush = new SolidBrush(backgroundColor);
g.FillRectangle(backgroundBrush, rect);
backgroundBrush.Dispose();
//Paint tabHeader
Rectangle tl = new Rectangle(rect.Left, rect.Top, cornerSize, cornerSize);
Rectangle tr = new Rectangle(rect.Width - cornerSize - 2, 0, cornerSize, cornerSize);
GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
path.AddArc(tl, 180, sweepAngle);
path.AddArc(tr, 270, sweepAngle);
path.AddLine(new Point(rect.Width - 2, rect.Height), new Point(rect.Left, rect.Height));
path.CloseAllFigures();
//Fill tabHeader
backcolorBrush = new LinearGradientBrush(new Rectangle(0, 0, this.ItemSize.Height, this.ItemSize.Height), tabBackcolorTop, tabBackcolorBottom, LinearGradientMode.Vertical);
g.FillPath(backcolorBrush, path);
backcolorBrush.Dispose();
//Draw border
if (tabBorderColor != Color.Empty)
{
borderPen = new Pen(tabBorderColor, 1);
g.DrawPath(borderPen, path);
borderPen.Dispose();
}
StringFormat sf = new StringFormat();
sf.Alignment = StringAlignment.Center;
sf.Trimming = StringTrimming.EllipsisCharacter;
sf.LineAlignment = StringAlignment.Center;
if (this.RightToLeft == RightToLeft.Yes)
{
sf.FormatFlags |= StringFormatFlags.DirectionRightToLeft;
}
textBrush = new SolidBrush(textColor);
g.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault;//.AntiAliasGridFit;
g.DrawString(tabHeaderText, textFont, textBrush, rect, sf);
textBrush.Dispose();
sf.Dispose();
return bm;
}
protected override void OnPaint(PaintEventArgs e)
{
try
{
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
const int cornerSize = 10;
const float sweepAngle = 90.0f;
SolidBrush sb = new SolidBrush(backgroundColor);
Rectangle tabControlRect = new Rectangle(this.ClientRectangle.Left - 2, this.ClientRectangle.Top - 1, this.Width + 10, this.Height + 1);
e.Graphics.FillRectangle(sb, tabControlRect);
sb.Dispose();
if (TabCount != 0)
{
Rectangle tabHeader0 = GetTabRect(0);
Rectangle tl = new Rectangle(tabHeader0.Left, tabHeader0.Bottom, cornerSize, cornerSize);
Rectangle bl = new Rectangle(tabHeader0.Left, this.ClientRectangle.Bottom - cornerSize - 1, cornerSize, cornerSize);
Rectangle br = new Rectangle(this.ClientRectangle.Width - cornerSize - 1, this.ClientRectangle.Bottom - cornerSize - 1, cornerSize, cornerSize);
Rectangle tr = new Rectangle(this.ClientRectangle.Width - cornerSize - 1, tabHeader0.Bottom, cornerSize, cornerSize);
GraphicsPath path = new System.Drawing.Drawing2D.GraphicsPath();
//add top line before
Rectangle selectedTab = this.GetTabRect(this.SelectedIndex);
path.AddLine(selectedTab.Right - 2, selectedTab.Bottom, this.ClientRectangle.Width - cornerSize, selectedTab.Bottom);
path.AddArc(tr, 270, sweepAngle);
path.AddArc(br, 360, sweepAngle);
path.AddArc(bl, 90, sweepAngle);
if (this.SelectedIndex == 0)
{
path.AddLine(tabHeader0.Left, this.ClientRectangle.Bottom - cornerSize, tabHeader0.Left, tabHeader0.Bottom);
}
else
{
path.AddArc(tl, 180, sweepAngle);
path.AddLine(cornerSize, selectedTab.Bottom, selectedTab.Left, selectedTab.Bottom);
}
SolidBrush activeBrush = new SolidBrush(activeBottomColor);
e.Graphics.FillPath(activeBrush, path);
if (activeBorderColor != Color.Empty)
{
Pen borderPen = new Pen(activeBorderColor, 1);
e.Graphics.DrawPath(borderPen, path);
}
}
for (int i = 0; i < TabCount; i++)
{
Rectangle tabRect = GetTabRect(i);
if (this.SelectedIndex == i)
{
Font font = new Font(this.Font.FontFamily, this.Font.Size, FontStyle.Bold);
Bitmap bm = PaintTabHeader(tabRect.Size, this.TabPages[i].Text, font, backgroundColor, activeTopColor, activeBottomColor, this.TabPages[i].ForeColor, activeBorderColor);
if (this.RightToLeft == RightToLeft.Yes)
{
bm.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
if (this.RightToLeft == RightToLeft.Yes)
{
e.Graphics.DrawImage(bm, tabRect.X - 1, tabRect.Top + 1);
}
else
{
e.Graphics.DrawImage(bm, tabRect.X, tabRect.Top + 1);
}
bm.Dispose();
}
else
{
Size size;
if (i == 0)
{
size = new Size(tabRect.Width - 2, tabRect.Height - 2);
}
else
{
size = new Size(tabRect.Width, tabRect.Height - 2);
}
Bitmap bm = PaintTabHeader(size, this.TabPages[i].Text, this.Font, backgroundColor, inactiveTopColor, inactiveBottomColor, this.TabPages[i].ForeColor, inactiveBorderColor);
if (this.RightToLeft == RightToLeft.Yes)
{
bm.RotateFlip(RotateFlipType.RotateNoneFlipX);
}
if (i == 0)
{
if (this.RightToLeft == RightToLeft.Yes)
{
e.Graphics.DrawImage(bm, tabRect.X + 1, tabRect.Top + 2);
}
else
{
e.Graphics.DrawImage(bm, tabRect.X + 2, tabRect.Top + 2);//default
}
}
else
{
if (this.RightToLeft == RightToLeft.Yes)
{
e.Graphics.DrawImage(bm, tabRect.X - 1, tabRect.Top + 2);
}
else
{
e.Graphics.DrawImage(bm, tabRect.X, tabRect.Top + 2);//default
}
}
bm.Dispose();
}
}
}
catch (Exception)
{
}
}
protected override void OnMouseHover(EventArgs e)
{
Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
Invalidate();
}
protected override void OnMouseMove(MouseEventArgs e)
{
Invalidate();
}
protected override void OnMouseClick(MouseEventArgs e)
{
Invalidate();
}
#endregion Protected Methods
#region properties
Color backgroundColor = Color.FromArgb(255, 223, 233, 238);
public Color BackgroundColor
{
get { return backgroundColor; }
set
{
if (value != Color.Empty)
backgroundColor = value;
}
}
Color inactiveTopColor = Color.FromArgb(255, 152, 187, 214);
public Color InactiveTabTopColor
{
get { return inactiveTopColor; }
set
{
if (value != Color.Empty)
inactiveTopColor = value;
}
}
Color inactiveBottomColor = Color.FromArgb(255, 102, 151, 188);
public Color InactiveTabBottomColor
{
get { return inactiveBottomColor; }
set
{
if (value != Color.Empty)
inactiveBottomColor = value;
}
}
Color inactiveBorderColor = Color.Empty;
public Color InactiveTabBorderColor
{
get { return inactiveBorderColor; }
set
{
if (value != Color.Empty)
inactiveBorderColor = value;
}
}
Color activeTopColor = Color.FromArgb(255, 250, 249, 249);
public Color ActiveTabTopColor
{
get { return activeTopColor; }
set
{
if (value != Color.Empty)
activeTopColor = value;
}
}
Color activeBottomColor = Color.FromArgb(255, 250, 249, 249);
public Color ActiveTabBottomColor
{
get { return activeBottomColor; }
set
{
if (value != Color.Empty)
activeBottomColor = value;
}
}
Color activeBorderColor = Color.FromArgb(255, 84, 143, 195);
public Color ActiveTabBorderColor
{
get { return activeBorderColor; }
set { activeBorderColor = value; }
}
#endregion
}
}