260 lines
8.9 KiB
C#
260 lines
8.9 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.Windows.Forms;
|
|||
|
using ryCommon;
|
|||
|
|
|||
|
namespace ryControls
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
/// 多标签控件
|
|||
|
/// </summary>
|
|||
|
[DefaultEvent("SelectedIndexChanged")]
|
|||
|
[Description("多标签控件")]
|
|||
|
public partial class TabBar : UserControl
|
|||
|
{
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public TabBar()
|
|||
|
{
|
|||
|
InitializeComponent();
|
|||
|
}
|
|||
|
/// <summary>
|
|||
|
/// 选项发生变化时激发
|
|||
|
/// </summary>
|
|||
|
[Description("选项发生变化时")]
|
|||
|
public event EventHandler SelectedIndexChanged;
|
|||
|
private int _SelectedIndex = 0;
|
|||
|
/// <summary>
|
|||
|
/// 标签选择的序号
|
|||
|
/// </summary>
|
|||
|
public int SelectedIndex
|
|||
|
{
|
|||
|
get { return _SelectedIndex; }
|
|||
|
set
|
|||
|
{
|
|||
|
if (_SelectedIndex != value)
|
|||
|
{
|
|||
|
_SelectedIndex = value;
|
|||
|
if (TabControlEx != null)
|
|||
|
{
|
|||
|
if (TabControlEx.TabCount > value)
|
|||
|
{
|
|||
|
TabControlEx.SelectedIndex = value;
|
|||
|
}
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
for (int m = 0; m < list_ctl.Count; m++)
|
|||
|
{
|
|||
|
Label lbl2 = list_ctl[m];
|
|||
|
if (m == value)
|
|||
|
{
|
|||
|
lbl2.ForeColor = Selected_Color;
|
|||
|
PicTabSelect.Left = lbl2.Left;
|
|||
|
PicTabSelect.Top = this.Height - PicTabSelect.Height;
|
|||
|
PicTabSelect.BringToFront();
|
|||
|
PicTabSelect.Width = lbl2.Width;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
lbl2.ForeColor = this.ForeColor;
|
|||
|
}
|
|||
|
}
|
|||
|
SelectedIndexChanged?.Invoke(this, new EventArgs());
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private string _tab_list = "";
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public string TabList
|
|||
|
{
|
|||
|
get { return _tab_list; }
|
|||
|
set { _tab_list = value; ReDraw(); }
|
|||
|
}
|
|||
|
private Color _Selected_Color = Color.RoyalBlue;
|
|||
|
/// <summary>
|
|||
|
/// 选择的颜色
|
|||
|
/// </summary>
|
|||
|
public Color Selected_Color
|
|||
|
{
|
|||
|
get { return _Selected_Color; }
|
|||
|
set { _Selected_Color = value; ReDraw(); }
|
|||
|
}
|
|||
|
private Color _TabControl_Color = Color.FromArgb(233, 233, 233);
|
|||
|
/// <summary>
|
|||
|
/// 多标签控件颜色
|
|||
|
/// </summary>
|
|||
|
public Color TabControl_Color
|
|||
|
{
|
|||
|
get { return _TabControl_Color; }
|
|||
|
set {
|
|||
|
_TabControl_Color = value;
|
|||
|
if (TabControlEx != null)
|
|||
|
{
|
|||
|
TabControlEx.TabBackColor = TabControl_Color;
|
|||
|
for (int i = 0; i < TabControlEx.TabCount; i++)
|
|||
|
{
|
|||
|
TabControlEx.TabPages[i].BackColor = TabControl_Color;
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private TabControlEx tabControlEx;
|
|||
|
/// <summary>
|
|||
|
///
|
|||
|
/// </summary>
|
|||
|
public TabControlEx TabControlEx
|
|||
|
{
|
|||
|
get
|
|||
|
{
|
|||
|
return this.tabControlEx;
|
|||
|
}
|
|||
|
set
|
|||
|
{
|
|||
|
if (this.tabControlEx != value)
|
|||
|
{
|
|||
|
if (this.tabControlEx != null)
|
|||
|
{
|
|||
|
this.tabControlEx.SelectedIndexChanged -= TabControlEx_SelectedIndexChanged;
|
|||
|
}
|
|||
|
if (this.tabControlEx != null && this.tabControlEx.TabBar == this)
|
|||
|
{
|
|||
|
this.tabControlEx.InternalTabBar = null;
|
|||
|
}
|
|||
|
this.tabControlEx = value;
|
|||
|
if (value != null)
|
|||
|
{
|
|||
|
value.InternalTabBar = this;
|
|||
|
value.SelectedIndexChanged += TabControlEx_SelectedIndexChanged;
|
|||
|
value.BringToFront();
|
|||
|
value.TabBackground = null;
|
|||
|
value.ItemSize = new Size(0, 1);
|
|||
|
value.TabBackColor = TabControl_Color;
|
|||
|
for (int i = 0; i < value.TabCount; i++)
|
|||
|
{
|
|||
|
value.TabPages[i].BackColor = TabControl_Color;
|
|||
|
}
|
|||
|
value.Location = new Point(this.Left, this.Top + this.Height);
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
}
|
|||
|
private void TabControlEx_SelectedIndexChanged(object o, EventArgs e)
|
|||
|
{
|
|||
|
TabControlEx tab = (TabControlEx)o;
|
|||
|
for (int m = 0; m < list_ctl.Count; m++)
|
|||
|
{
|
|||
|
Label lbl2 = list_ctl[m];
|
|||
|
if (m == tab.SelectedIndex)
|
|||
|
{
|
|||
|
lbl2.ForeColor = Selected_Color;
|
|||
|
PicTabSelect.Left = lbl2.Left;
|
|||
|
PicTabSelect.Top = this.Height - PicTabSelect.Height;
|
|||
|
PicTabSelect.BringToFront();
|
|||
|
PicTabSelect.Width = lbl2.Width;
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
lbl2.ForeColor = this.ForeColor;
|
|||
|
}
|
|||
|
}
|
|||
|
_SelectedIndex = tab.SelectedIndex;
|
|||
|
SelectedIndexChanged?.Invoke(this, new EventArgs());
|
|||
|
}
|
|||
|
private List<Label> list_ctl = new List<Label>();
|
|||
|
private void ReDraw()
|
|||
|
{
|
|||
|
for (int i =this.Controls.Count-1; i>0; i--)
|
|||
|
{
|
|||
|
switch (this.Controls[i])
|
|||
|
{
|
|||
|
case Label lbl:
|
|||
|
this.Controls.Remove(lbl);
|
|||
|
break;
|
|||
|
}
|
|||
|
}
|
|||
|
list_ctl.Clear();
|
|||
|
string[] item = _tab_list.Split('|');
|
|||
|
int lbl_left = 0;
|
|||
|
Graphics gh = this.CreateGraphics();
|
|||
|
for (int i = 0; i < item.Length; i++)
|
|||
|
{
|
|||
|
Label _lbl = new Label
|
|||
|
{
|
|||
|
Parent = this,
|
|||
|
Text = item[i],
|
|||
|
AutoSize = false,
|
|||
|
Height = this.Height
|
|||
|
};
|
|||
|
SizeF sf = gh.MeasureString(item[i], _lbl.Font);
|
|||
|
_lbl.Width = (int)sf.Width+8;
|
|||
|
//_lbl.Width = 60;
|
|||
|
_lbl.Left = lbl_left;
|
|||
|
lbl_left += _lbl.Width;
|
|||
|
_lbl.TextAlign = ContentAlignment.MiddleCenter;
|
|||
|
_lbl.Visible = true;
|
|||
|
_lbl.ForeColor = this.ForeColor;
|
|||
|
_lbl.Tag = i;
|
|||
|
list_ctl.Add(_lbl);
|
|||
|
if (i == 0)
|
|||
|
{
|
|||
|
_lbl.ForeColor = Selected_Color;
|
|||
|
PicTabSelect.Left = _lbl.Left;
|
|||
|
PicTabSelect.Top = this.Height- PicTabSelect.Height;
|
|||
|
PicTabSelect.BringToFront();
|
|||
|
PicTabSelect.Width = _lbl.Width;
|
|||
|
}
|
|||
|
_lbl.Click += new EventHandler((object o, EventArgs e2) =>
|
|||
|
{
|
|||
|
int tag = _lbl.Tag.ToInt();
|
|||
|
Label lbl = (Label)o;
|
|||
|
if (TabControlEx == null)
|
|||
|
{
|
|||
|
for (int m = 0; m < list_ctl.Count; m++)
|
|||
|
{
|
|||
|
if(list_ctl[m]== lbl)
|
|||
|
{
|
|||
|
lbl.ForeColor = Selected_Color;
|
|||
|
PicTabSelect.Left = lbl.Left;
|
|||
|
PicTabSelect.Top = this.Height - PicTabSelect.Height;
|
|||
|
PicTabSelect.BringToFront();
|
|||
|
PicTabSelect.Width = lbl.Width;
|
|||
|
_SelectedIndex = m;
|
|||
|
SelectedIndexChanged?.Invoke(this, new EventArgs());
|
|||
|
}
|
|||
|
else
|
|||
|
{
|
|||
|
list_ctl[m].ForeColor = this.ForeColor;
|
|||
|
}
|
|||
|
}
|
|||
|
return;
|
|||
|
}
|
|||
|
if (tag.IsInRange(0, TabControlEx.TabCount))
|
|||
|
{
|
|||
|
TabControlEx.SelectedIndex = tag;
|
|||
|
}
|
|||
|
});
|
|||
|
}
|
|||
|
}
|
|||
|
|
|||
|
private void TabBar_ForeColorChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
ReDraw();
|
|||
|
}
|
|||
|
|
|||
|
private void TabBar_SizeChanged(object sender, EventArgs e)
|
|||
|
{
|
|||
|
ReDraw();
|
|||
|
}
|
|||
|
}
|
|||
|
}
|