Files
2026-03-19 20:44:33 +08:00

210 lines
8.0 KiB
C#

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;
using System.Runtime.InteropServices;
namespace TemuKJD.UserComponent
{
public partial class UcTabControl : TabControl
{
public UcTabControl()
{
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;
this.MouseDown += UcTabControl_MouseDown;
}
private void UcTabControl_MouseDown(object sender, MouseEventArgs e)
{
if (this.TabCount == 0)
return;
if (e.Button == MouseButtons.Left)
{
if (!showCloseButton)
return;
int x = e.X, y = e.Y;
Rectangle rect = this.GetTabRect(this.SelectedIndex);
Rectangle closeRect = new Rectangle(rect.X + rect.Width - 16, rect.Y + rect.Height / 2 - 4, 8, 8);
if (x >= closeRect.X && y >= closeRect.Y && x <= closeRect.X + closeRect.Width && y <= closeRect.Y + closeRect.Height)
{
this.TabPages.Remove(this.SelectedTab);
}
}
}
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 tabSelectColor = Color.Red;
[Browsable(true), Category("自定义分组"), Description("Tab选中颜色")]
public Color TabSelectColor
{
get { return tabSelectColor; }
set
{
tabSelectColor = value;
this.Invalidate();
}
}
private Color tabTextColor = Color.Gray;
[Browsable(true), Category("自定义分组"), Description("Tab选中颜色")]
public Color TabTextColor
{
get { return tabTextColor; }
set
{
tabTextColor = 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 bool showCloseButton = false;
[Browsable(true), Category("自定义分组"), Description("显示关闭页面按钮")]
public bool ShowCloseButton
{
get { return showCloseButton; }
set
{
showCloseButton = value;
if (showCloseButton)
this.Padding = new Point(10, this.Padding.Y);
else
this.Padding = new Point(6, this.Padding.Y);
this.Invalidate();
}
}
#region 去掉或减小TabControl的边框(去掉的话+-3)
protected override void WndProc(ref Message m)
{
if ((m.Msg == TCM_ADJUSTRECT))
{
RECT rc = (RECT)m.GetLParam(typeof(RECT));
//Adjust these values to suit, dependant upon Appearance
rc.Left -= 2;
rc.Right += 0;
rc.Top -= 0;
rc.Bottom += 0;
Marshal.StructureToPtr(rc, m.LParam, true);
}
base.WndProc(ref m);
}
private const Int32 TCM_FIRST = 0x1300;
private const Int32 TCM_ADJUSTRECT = (TCM_FIRST + 40);
private struct RECT
{
public Int32 Left;
public Int32 Top;
public Int32 Right;
public Int32 Bottom;
}
#endregion
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));
// 绘制边框
//e.Graphics.DrawRectangle(new Pen(backgroundColor), 0, 0, this.Width, this.Height);
//e.Graphics.DrawLine(new Pen(tabSelectColor), 2, this.GetTabRect(0).Bottom, this.Width - 6, this.GetTabRect(0).Bottom);
// 绘制tab控件
for (int i = 0; i < this.TabCount; i++)
{
// Calculate text position
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;
if (showCloseButton)
textPoint.X -= 5;
// 绘制标题
e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAliasGridFit;
if (this.SelectedIndex == i)
e.Graphics.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(TabSelectTextColor), textPoint.X, textPoint.Y);
else
e.Graphics.DrawString(this.TabPages[i].Text, this.Font, new SolidBrush(tabTextColor), textPoint.X, textPoint.Y);
// 绘制tab框
//e.Graphics.DrawRectangle(Pens.Red, this.GetTabRect(i));
if (this.SelectedIndex == i)
{
for (int j = 0; j < 2; j++)
e.Graphics.DrawLine(new Pen(tabSelectColor),
new Point(this.GetTabRect(i).Left + 0, this.GetTabRect(i).Bottom - j),
new Point(this.GetTabRect(i).Right - 5, this.GetTabRect(i).Bottom - j));
}
// 绘制关闭按钮
if (showCloseButton)
{
using (Pen pen = new Pen(Color.Gray, 1.4f))
{
Point p1 = new Point(bounds.X + bounds.Width - 15, bounds.Y + bounds.Height / 2 - 3);
Point p2 = new Point(bounds.X + bounds.Width - 9, bounds.Y + bounds.Height / 2 + 3);
e.Graphics.DrawLine(pen, p1, p2);
Point p3 = new Point(bounds.X + bounds.Width - 9, bounds.Y + bounds.Height / 2 - 3);
Point p4 = new Point(bounds.X + bounds.Width - 15, bounds.Y + bounds.Height / 2 + 3);
e.Graphics.DrawLine(pen, p3, p4);
}
}
}
}
}
}