using System; using System.Collections.Generic; using System.ComponentModel; using System.Drawing; using System.Data; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Work.UserComponents { public partial class UcTabControlMetro : TabControl { public UcTabControlMetro() { InitializeComponent(); base.SetStyle( ControlStyles.UserPaint | // 控件将自行绘制,而不是通过操作系统来绘制 ControlStyles.OptimizedDoubleBuffer | // 该控件首先在缓冲区中绘制,而不是直接绘制到屏幕上,这样可以减少闪烁 ControlStyles.AllPaintingInWmPaint | // 控件将忽略 WM_ERASEBKGND 窗口消息以减少闪烁 ControlStyles.ResizeRedraw | // 在调整控件大小时重绘控件 ControlStyles.SupportsTransparentBackColor, // 控件接受 alpha 组件小于 255 的 BackColor 以模拟透明 true); // 设置以上值为 true base.UpdateStyles(); //this.SizeMode = TabSizeMode.Fixed; } private Color backgroundColor = Color.FromArgb(251, 251, 251); [Browsable(true), Category("自定义分组"), Description("Tab控件背景")] public Color BackgroundColor { get { return backgroundColor; } set { backgroundColor = value; this.Invalidate(); } } private Color borderColor = Color.FromArgb(0, 174, 157); [Browsable(true), Category("自定义分组"), Description("Tab控件边框颜色")] public Color BorderColor { get { return borderColor; } set { borderColor = value; this.Invalidate(); } } private Color tabSelectColor = Color.Red; [Browsable(true), Category("自定义分组"), Description("Tab选中颜色")] public Color TabSelectColor { get { return tabSelectColor; } set { tabSelectColor = value; this.Invalidate(); } } private Color tabSelectTextColor = Color.Gray; [Browsable(true), Category("自定义分组"), Description("Tab选中文字颜色")] public Color TabSelectTextColor { get { return tabSelectTextColor; } set { tabSelectTextColor = value; this.Invalidate(); } } private Color tabUnSelectTextColor = Color.White; [Browsable(true), Category("自定义分组"), Description("Tab未选中文字颜色")] public Color TabUnSelectTextColor { get { return tabUnSelectTextColor; } set { tabUnSelectTextColor = value; this.Invalidate(); } } private bool showCloseButton = false; [Browsable(true), Category("自定义分组"), Description("显示关闭页面按钮")] public bool ShowCloseButton { get { return showCloseButton; } set { showCloseButton = value; this.Invalidate(); } } Size oldSize = new Size(); protected override void OnPaint(PaintEventArgs e) { if (TabPages.Count > 0 && oldSize.Width != this.Size.Width && this.SizeMode == TabSizeMode.Fixed) { //this.SizeMode = TabSizeMode.Fixed; this.ItemSize = new Size((int)Math.Floor(1.0 * (this.Width - 4) / TabPages.Count), 40); oldSize.Width = this.Size.Width; } // 绘制背景 e.Graphics.FillRectangle(new SolidBrush(backgroundColor), new RectangleF(0, 0, this.Width, this.Height)); // 绘制边框 int startX = this.Margin.Left; int startY = this.Margin.Top; if (this.TabCount > 0) { startX = this.GetTabRect(0).Top; startY = this.GetTabRect(0).Bottom; } e.Graphics.DrawRectangle(new Pen(tabSelectColor), startX, startY, this.Width - this.Margin.Right - startX, this.Height - this.Margin.Bottom - startY); // 绘制tab控件 for (int i = 0; i < this.TabCount; i++) { // 计算tab标题位置 Rectangle bounds = this.GetTabRect(i); PointF textPoint = new PointF(); SizeF textSize = TextRenderer.MeasureText(this.TabPages[i].Text, this.Font); // 加上每个标签的左偏移量X textPoint.X = bounds.X + (bounds.Width - textSize.Width) / 2; textPoint.Y = bounds.Y + (bounds.Height - textSize.Height) / 2; // 当前选中的tab if (this.SelectedIndex == i) { // 绘制tab框 var rect = this.GetTabRect(i); // 底部和背景色相同 e.Graphics.DrawLine(new Pen(backgroundColor), new Point(rect.Left, rect.Bottom), new Point(rect.Right, rect.Bottom)); e.Graphics.DrawLine(new Pen(tabSelectColor), new Point(rect.Left, rect.Bottom), new Point(rect.Left, rect.Top)); e.Graphics.DrawLine(new Pen(tabSelectColor), new Point(rect.Left, rect.Top), new Point(rect.Right, rect.Top)); e.Graphics.DrawLine(new Pen(tabSelectColor), new Point(rect.Right, rect.Top), new Point(rect.Right, rect.Bottom)); // 绘制tab标题文字 //e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; e.Graphics.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(tabSelectTextColor), textPoint.X, textPoint.Y); } else { // 绘制tab框 e.Graphics.FillRectangle(new SolidBrush(tabSelectColor), this.GetTabRect(i)); // 绘制tab标题文字 //e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.SystemDefault; e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit; e.Graphics.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(tabUnSelectTextColor), textPoint.X, textPoint.Y); } // 绘制关闭按钮 if (showCloseButton) { using (Pen pen = new Pen(Color.Gray, 1f)) { Point p1 = new Point(bounds.X + 10, bounds.Y + 10); Point p2 = new Point(bounds.X + bounds.Width - 10, bounds.Y + bounds.Height - 10); e.Graphics.DrawLine(pen, p1, p2); Point p3 = new Point(bounds.X + 10, bounds.Y + bounds.Height - 10); Point p4 = new Point(bounds.X + bounds.Width - 10, bounds.Y + 10); e.Graphics.DrawLine(pen, p3, p4); } } } } } }