作为Winfrom开发者来说,我们很多时候会用到tabcontrol来实现和网页标签页相关的效果。同时微软自带的控件样式不符合我们的需求,我们该如何去实现更加美观且可以自定义的组合控件呢?带着这个问题进入我们今天的主题,组合控件tabcontrol实现。
我们用的是界面库为DSkin.dll,简单介绍下,DSkin是干什么的。界面库设计工具DSKin是DSkin界面库官网(www.dskin.cn)开发的Winfrom DirectUI界面库。Dskin可以动态Alpha混合,支持窗体和控件的透明度混合效果,以及多个控件叠加的效果,支持窗体设置透明色和透明图片直接呈现出来,制作异形窗体简单快速,以此同时,Dskin内置了许多个虚拟控件,绘制高效,无句柄,资源占用极低,控件可以相互嵌套和组合。相对于微软的原生Winform控件库而言,用Dskin开发的界面即使设置大张的背景图及控件比较多的时候不会出现闪烁。
现在正式切入我们今天的主题,我们先看下界面效果图,如图1所示界面显示了两种效果,分别是正常的标签页和激活的标签页。该界面效果可以通过CreateTempItemCount来创建效果效果,然后通过扩展的属性来调整样式。扩展属性如图2所示,这些属性是我们创建的属性,可以对属性进行修改或新增从而符合我们的需求。
图1 效果展示图
图2 控件扩展属性说明
我们组合控件是继承于DSkin.Controls.DSkinUserControl,控件主要由DSkinTabBar、DSkinTabControl组合实现。界面设计效果如图3所示,除了上述的主要控件之外,我们还添加了DSkinScrollBar(滚动条)、RichTextBox(富文本编辑框,用于计算文字所占宽度)、ContextMenuStrip(右键菜单栏)、ToolTip(提示框,用于属性滑过Item时显示对应的文本信息)。
图3 控件组合界面展示
由于我们的继承对象为UserControl,为此我们的组合控件除了具有了UserControl本身拥有的属性,我们还可以自己扩展属性。比如上述讲到的CreateTempItemCount,那如何创建呢?我们可以定义一个Public属性方法,我们可以设置该属性的默认值、指定属性或事件的功能说明,对应的代码关键词分别为DefaultValue、Description。实例代码如下:
private int _CreateTempItemCount = 0;
[DefaultValue(0)]
[Description("用于测试查看效果,实际使用请将其移除,设置为0清空")]
public int CreateTempItemCount
{
get { return _CreateTempItemCount; }
set
{
_CreateTempItemCount = value;
this.dSkinTabBar.Items.Clear();
this.dSkinTabControl.TabPages.Clear();
for (int i = 0; i < _CreateTempItemCount; i++)
{
TempCreate(i.ToString(), "测试样式" + i);
}
}
}
扩展属性也创建完了,接下来就是创建标签页及页面绑定关系,由于我们使用的是TabControl,其本身就具有标签页的功能,我们为了让DSkinTabBar来替代标签功能,我们需要对TabControl的"ItemSize"属性设置为"0,1",同时将DSkinTabBar与DSkinTabControl绑定起来,则属性TabControl与我们创建的DSkinTabControl关联,这样即可相互影响。
我们的控件要求功能既能加载Control同时加载Form,因此我们需要针对两种不同的情况创建不同的方法。我们定义了两个属性dicControl和dicForm,用于右键菜单栏使用以及释放资源使用。
#region 定义
///
/// 用于关闭释放资源Control
///
Dictionary dicControl = new Dictionary();
///
/// 用于关闭释放资源Form
///
Dictionary dicForm = new Dictionary();
#endregion
我们标签页的宽度除了可以自定义设置,同时还可以根据传入的文本内自动计算所需要的宽度。这次将采用到RichTextBox控件来辅助实现,具体实现代码如下:
#region 计算Item的宽度
///
/// 计算显示框高度和宽度,英文字体和中文以及标点、数字的宽度各不相同,需计算
///
/// 文本
/// 宽度
private int CalcItemWidth(string mess, int width)
{
int Linewidth; //显示对话内容容器的宽度和高度
//临时建立一个容器装入内容
RichTextBox canv_Rich = new RichTextBox();
//先取全部Rtf的值
canv_Rich.Rtf = mess;
//再按照Txt判断文字
//判断中文
MatchCollection zh = Regex.Matches(canv_Rich.Text, @"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
//判断中文标点
MatchCollection zhdot = Regex.Matches(canv_Rich.Text, @"[,。;?~!:‘“”’【】()]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
//判断数字
MatchCollection en = Regex.Matches(canv_Rich.Text, @"[1234567890]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
//其余为英文,并计算总宽度
Linewidth = (zh.Count + zhdot.Count) * 13 + en.Count * 8 + (canv_Rich.Text.Length - zh.Count - zhdot.Count - en.Count) * 7;
//接收数据内容中是否包含图像
int havePic = 0;
havePic = Regex.Matches(canv_Rich.Rtf, "Paint.Picture").Count;
//加上图像宽度
Linewidth += havePic * 33;
//判断极值
//if (Linewidth > (12 * 13)) { Linewidth = 12 * 13; }
if (Linewidth < width) { Linewidth = width; }
return Linewidth;
}
#endregion
由于篇幅的原因,下方直接贴出代码,代码中有相对的说明,应该可以看到懂。
TabControlYd.cs完整代码如下:
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 DSkin.DirectUI;
using DSkin.Controls;
using System.Diagnostics;
using System.Runtime;
using System.Text.RegularExpressions;
namespace SCommon
{
public partial class TabControlYd : DSkin.Controls.DSkinUserControl
{
#region 构造函数
public TabControlYd()
{
InitializeComponent();
this.Dock = DockStyle.Fill;
}
#endregion
#region 定义
///
/// 用于关闭释放资源Control
///
Dictionary dicControl = new Dictionary();
///
/// 用于关闭释放资源Form
///
Dictionary dicForm = new Dictionary();
#endregion
#region 属性
private Color _DownItemColor = Color.FromArgb(60, 150, 180);
[DefaultValue(typeof(Color), "60,150,180")]
[Description("鼠标按下Item背景色")]
public Color DownItemColor
{
get { return _DownItemColor; }
set
{
_DownItemColor = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.BaseColor = _DownItemColor;
}
}
}
private Color _NormalItemColor = Color.FromArgb(80, 200, 240);
[DefaultValue(typeof(Color), "80,200,240")]
[Description("Item正常的背景色")]
public Color NormalItemColor
{
get { return _NormalItemColor; }
set
{
_NormalItemColor = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.BaseColor = _NormalItemColor;
}
}
}
private Padding _ItemMargin = new Padding(10, 10, 0, 0);
[DefaultValue(typeof(Padding), "10, 10, 0, 0")]
[Description("Item与Item之间的距离")]
public Padding ItemMargin
{
get { return _ItemMargin; }
set
{
_ItemMargin = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.Margin = _ItemMargin;
}
}
}
private Color _ItemBackColor = Color.FromArgb(135, 206, 235);
[DefaultValue(typeof(Color), "135,206,235")]
[Description("标签页栏背景色设置")]
public Color ItemBackColor
{
get { return _ItemBackColor; }
set
{
_ItemBackColor = value;
this.dSkinTabBar.BackColor = _ItemBackColor;
}
}
private int _TabbarHeight = 40;
[DefaultValue(40)]
[Description("标签页栏高度设置")]
public int TabbarHeight
{
get { return _TabbarHeight; }
set
{
_TabbarHeight = value;
this.dSkinTabBar.Height = _TabbarHeight;
}
}
private Font _ItemFont = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Bold);
[DefaultValue(typeof(Font), "微软雅黑, 9F, System.Drawing.FontStyle.Bold")]
[Description("Item字体设置")]
public Font ItemFont
{
get { return _ItemFont; }
set
{
_ItemFont = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.Font = _ItemFont;
}
}
}
private Color _ItemForeColor = Color.FromArgb(255, 255, 255);
[DefaultValue(typeof(Color), "255,255,255")]
[Description("设置Item字体颜色")]
public Color ItemForeColor
{
get { return _ItemForeColor; }
set
{
_ItemForeColor = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.ForeColor = _ItemForeColor;
}
}
}
private Size _ItemSize = new System.Drawing.Size(120, 35);
[DefaultValue(typeof(Size), "120,35")]
[Description("设置Item大小")]
public Size ItemSize
{
get { return _ItemSize; }
set
{
_ItemSize = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.Size = _ItemSize;
}
}
}
private bool _IsShowButtonBorder = true;
[DefaultValue(true)]
[Description("是否显示边框")]
public bool IsShowButtonBorder
{
get { return _IsShowButtonBorder; }
set
{
_IsShowButtonBorder = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.ShowButtonBorder = _IsShowButtonBorder;
}
}
}
private int _ItemRadius = 15;
[DefaultValue(15)]
[Description("圆弧的大小")]
public int ItemRadius
{
get { return _ItemRadius; }
set
{
_ItemRadius = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.Radius = _ItemRadius;
}
}
}
private Color _ItemBorderColor = Color.FromArgb(224, 224, 224);
[DefaultValue(typeof(Color), "224,224,224")]
[Description("设置Item的边框颜色")]
public Color ItemBorderColor
{
get { return _ItemBorderColor; }
set
{
_ItemBorderColor = value;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.ButtonBorderColor = _ItemBorderColor;
}
}
}
private bool _IsShowMouseItem = true;
[DefaultValue(true)]
[Description("是否显示删除按钮")]
public bool IsShowMouseItem
{
get { return _IsShowMouseItem; }
set { _IsShowMouseItem = value; }
}
private int _CreateTempItemCount = 0;
[DefaultValue(0)]
[Description("用于测试查看效果,实际使用请将其移除,设置为0清空")]
public int CreateTempItemCount
{
get { return _CreateTempItemCount; }
set
{
_CreateTempItemCount = value;
this.dSkinTabBar.Items.Clear();
this.dSkinTabControl.TabPages.Clear();
for (int i = 0; i < _CreateTempItemCount; i++)
{
TempCreate(i.ToString(), "测试样式" + i);
}
}
}
///
/// 摘要:【外部快捷键操作】
/// 0代表:关闭当前标签页
/// 1代表:除此之外全部关闭
/// 2代表:关闭所有的标签页
/// default:关闭当前标签页
///
public int ExternalShortcuts
{
set
{
switch (value)
{
case 0:
关闭当前标签页ToolStripMenuItem_Click(null, null);
break;
case 1:
除此之外全部关闭ToolStripMenuItem_Click(null, null);
break;
case 2:
关闭所有的标签页ToolStripMenuItem_Click(null, null);
break;
default:
关闭当前标签页ToolStripMenuItem_Click(null, null);
break;
}
}
}
#endregion
#region 临时创建
private void TempCreate(string key, string text)
{
if (this.dSkinTabControl != null)
{
this.dSkinTabControl.TabPages.Add(key, text);
this.dSkinTabControl.SelectTab(key);
DSkinTabItem _dSkinTabItem = new DSkinTabItem();
_dSkinTabItem.Text = text;
_dSkinTabItem.Font = _ItemFont;
_dSkinTabItem.ForeColor = _ItemForeColor;
_dSkinTabItem.Tag = key;
_dSkinTabItem.MouseEnter += new System.EventHandler(this.dSkinTabItem_MouseEnter);
_dSkinTabItem.MouseLeave += new System.EventHandler(this.dSkinTabItem_MouseLeave);
_dSkinTabItem.MouseClick += new System.EventHandler(this.dSkinTabItem_MouseClick);
// _dSkinTabItem.BaseColor = _NormalItemColor;
_dSkinTabItem.IsPureColor = true;
_dSkinTabItem.ButtonBorderColor = _ItemBorderColor;
_dSkinTabItem.Margin = _ItemMargin;
_dSkinTabItem.Radius = _ItemRadius;
_dSkinTabItem.ShowButtonBorder = _IsShowButtonBorder;
_dSkinTabItem.Size = _ItemSize;
this.dSkinTabBar.Items.Add(_dSkinTabItem);
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.BaseColor = _NormalItemColor;
}
_dSkinTabItem.BaseColor = _DownItemColor;
DuiButton _duiButton = new DuiButton();
_duiButton.Tag = key;
_duiButton.NormalImage = Properties.Resources.tabpage_close_normal;
_duiButton.Location = new Point(_dSkinTabItem.Width - 20, (_dSkinTabItem.Height - 16) / 2);
_duiButton.MouseEnter += new System.EventHandler(this.duiButton_MouseEnter);
_duiButton.MouseLeave += new System.EventHandler(this.duiButton_MouseLeave);
_duiButton.MouseDown += new System.EventHandler(this.duiButton_MouseDown);
_duiButton.MouseClick += new System.EventHandler(this.duiButton_MouseClick);
_dSkinTabItem.Controls.Add(_duiButton);
_duiButton.Visible = false;
}
}
#endregion
#region 方法
///
/// 用于判断是否已经添加了控件
///
private Dictionary dictTabContrils = new Dictionary();
///
/// TabControl添加标签页Control
///
/// 需要添加的控件
/// 区别专用
/// 显示在Item上的文本
public void AddItemControl(Control _control, string key, string text)
{
if (key == null) return;
if (!dictTabContrils.ContainsKey(key))
{
if (this.dSkinTabControl != null)
{
DSkinTabItem _dSkinTabItem = new DSkinTabItem();
_dSkinTabItem.Text = text;
_dSkinTabItem.ToolTip = text;
_dSkinTabItem.Font = _ItemFont;
_dSkinTabItem.ForeColor = _ItemForeColor;
_dSkinTabItem.Tag = key;
_dSkinTabItem.MouseEnter += new System.EventHandler(this.dSkinTabItem_MouseEnter);
_dSkinTabItem.MouseLeave += new System.EventHandler(this.dSkinTabItem_MouseLeave);
_dSkinTabItem.MouseClick += new System.EventHandler(this.dSkinTabItem_MouseClick);
if (_IsShowMouseItem)
_dSkinTabItem.ContextMenuStrip = this.contextMenuStrip;
// _dSkinTabItem.BaseColor = _NormalItemColor;
_dSkinTabItem.IsPureColor = true;
_dSkinTabItem.ButtonBorderColor = _ItemBorderColor;
_dSkinTabItem.Margin = _ItemMargin;
_dSkinTabItem.Radius = _ItemRadius;
_dSkinTabItem.ShowButtonBorder = _IsShowButtonBorder;
_dSkinTabItem.Size = _ItemSize;
this.richTextBox1.Text = text;
_dSkinTabItem.Width = CalcItemWidth(this.richTextBox1.Rtf, _ItemSize.Width);
this.dSkinTabBar.Items.Add(_dSkinTabItem);
IsShowScrollBar();
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.BaseColor = _NormalItemColor;
}
_dSkinTabItem.BaseColor = _DownItemColor;
dictTabContrils.Add(key, _control);
this.dSkinTabControl.TabPages.Add(key, text);
this.dSkinTabControl.TabPages[key].AutoScroll = true;
this.dSkinTabControl.SelectTab(key);
_control.Dock = DockStyle.Fill;
_control.Show();
this.dSkinTabControl.TabPages[key].Controls.Add(_control);
if (!dicControl.ContainsKey(key))
dicControl.Add(key, _control);
if (_IsShowMouseItem)
{
DuiButton _duiButton = new DuiButton();
_duiButton.Tag = key;
_duiButton.NormalImage = Properties.Resources.tabpage_close_normal;
_duiButton.Cursor = System.Windows.Forms.Cursors.Hand;
_duiButton.Location = new Point(_dSkinTabItem.Width - 20, (_dSkinTabItem.Height - 16) / 2);
_duiButton.MouseEnter += new System.EventHandler(this.duiButton_MouseEnter);
_duiButton.MouseLeave += new System.EventHandler(this.duiButton_MouseLeave);
_duiButton.MouseDown += new System.EventHandler(this.duiButton_MouseDown);
_duiButton.MouseClick += new System.EventHandler(this.duiButton_MouseClick);
_dSkinTabItem.Controls.Add(_duiButton);
_duiButton.Visible = false;
}
// SCommon.ResettingFont.SettingFont2Control(_control);
}
}
else
{
this.dSkinTabControl.SelectTab(key);
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.Tag.ToString() != key)
item.BaseColor = _NormalItemColor;
else
item.BaseColor = _DownItemColor;
}
}
}
///
/// TabControl添加标签页form
///
/// 需要添加的控件
/// 区别专用
/// 显示在Item上的文本
public void AddItemControlForForm(Form form, string key, string text)
{
if (!dictTabContrils.ContainsKey(key))
{
if (this.dSkinTabControl != null)
{
DSkinTabItem _dSkinTabItem = new DSkinTabItem();
_dSkinTabItem.Text = text;
_dSkinTabItem.ToolTip = text;
_dSkinTabItem.Font = _ItemFont;
_dSkinTabItem.ForeColor = _ItemForeColor;
_dSkinTabItem.Tag = key;
_dSkinTabItem.MouseEnter += new System.EventHandler(this.dSkinTabItem_MouseEnter);
_dSkinTabItem.MouseLeave += new System.EventHandler(this.dSkinTabItem_MouseLeave);
_dSkinTabItem.MouseClick += new System.EventHandler(this.dSkinTabItem_MouseClick);
_dSkinTabItem.MouseDoubleClick += _dSkinTabItem_MouseDoubleClick;
if (_IsShowMouseItem)
_dSkinTabItem.ContextMenuStrip = this.contextMenuStrip;
// _dSkinTabItem.BaseColor = _NormalItemColor;
_dSkinTabItem.IsPureColor = true;
_dSkinTabItem.ButtonBorderColor = _ItemBorderColor;
_dSkinTabItem.Margin = _ItemMargin;
_dSkinTabItem.Radius = _ItemRadius;
_dSkinTabItem.ShowButtonBorder = _IsShowButtonBorder;
_dSkinTabItem.Size = _ItemSize;
this.richTextBox1.Text = text;
_dSkinTabItem.Width = CalcItemWidth(this.richTextBox1.Rtf, _ItemSize.Width);
this.dSkinTabBar.Items.Add(_dSkinTabItem);
IsShowScrollBar();
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
item.BaseColor = _NormalItemColor;
}
_dSkinTabItem.BaseColor = _DownItemColor;
dictTabContrils.Add(key, form);
this.dSkinTabControl.TabPages.Add(key, text);
this.dSkinTabControl.TabPages[key].AutoScroll = true;
this.dSkinTabControl.SelectTab(key);
form.TopLevel = false;
form.AutoScroll = true;
form.Dock = DockStyle.Fill;
form.Show();
this.dSkinTabControl.TabPages[key].Controls.Add(form);
if (!dicForm.ContainsKey(key))
dicForm.Add(key, form);
if (_IsShowMouseItem)
{
DuiButton _duiButton = new DuiButton();
_duiButton.Tag = key;
_duiButton.NormalImage = Properties.Resources.tabpage_close_normal;
_duiButton.Location = new Point(_dSkinTabItem.Width - 20, (_dSkinTabItem.Height - 16) / 2);
_duiButton.MouseEnter += new System.EventHandler(this.duiButton_MouseEnter);
_duiButton.MouseLeave += new System.EventHandler(this.duiButton_MouseLeave);
_duiButton.MouseDown += new System.EventHandler(this.duiButton_MouseDown);
_duiButton.MouseClick += new System.EventHandler(this.duiButton_MouseClick);
_dSkinTabItem.Controls.Add(_duiButton);
_duiButton.Visible = false;
}
// SCommon.ResettingFont.SettingFont2Control(form);
}
}
else
{
this.dSkinTabControl.SelectTab(key);
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.Tag.ToString() != key)
item.BaseColor = _NormalItemColor;
else
item.BaseColor = _DownItemColor;
}
}
}
void _dSkinTabItem_MouseDoubleClick(object sender, DuiMouseEventArgs e)
{
DSkinTabItem item = (DSkinTabItem)sender;
if (dicForm.ContainsKey(item.Tag.ToString()))
{
SCommon.PublicForms.SimpleDragDropForm _form = new SCommon.PublicForms.SimpleDragDropForm(this, item.Tag.ToString(), dicForm[item.Tag.ToString()]);
_form.Text = item.Text;
_form.Show();
dSkinTabBar.Items.Remove(item);
dictTabContrils.Remove(item.Tag.ToString());
dSkinTabControl.TabPages.RemoveByKey(item.Tag.ToString());
dicForm.Remove(item.Tag.ToString());
}
}
///
/// 判断TabPages是否含有指定控件
///
///
///
public bool IsContainsKey(string key)
{
if (this.dSkinTabControl.TabPages.ContainsKey(key))
return true;
else
return false;
}
#endregion
#region 鼠标事件
private void duiButton_MouseLeave(object sender, EventArgs e)
{
DuiButton btn = (DuiButton)sender;
btn.NormalImage = Properties.Resources.tabpage_close_normal;
}
private void duiButton_MouseEnter(object sender, MouseEventArgs e)
{
DuiButton btn = (DuiButton)sender;
btn.NormalImage = Properties.Resources.tabpage_mousemove;
}
private void duiButton_MouseClick(object sender, DuiMouseEventArgs e)
{
DuiButton btn = (DuiButton)sender;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.Tag.ToString() == btn.Tag.ToString())
{
if (_IsCloseingTips)
{
messageYesNo _messageYesNo = new messageYesNo(0, "您确定要关闭当前选项卡?");
if (_messageYesNo.ShowDialog() != DialogResult.OK)
return;
else
{
if (ClickEvent != null)
ClickEvent("", item.Tag.ToString());
}
}
if (UserControlClosing != null)
UserControlClosing(sender, e);
dSkinTabBar.Items.Remove(item);
dictTabContrils.Remove(item.Tag.ToString());
dSkinTabControl.TabPages.RemoveByKey(btn.Tag.ToString());
if (dicControl.ContainsKey(btn.Tag.ToString()))
{
dicControl[btn.Tag.ToString()].Dispose();
dicControl.Remove(btn.Tag.ToString());
}
if (dicForm.ContainsKey(btn.Tag.ToString()))
{
dicForm[btn.Tag.ToString()].Close();
dicForm[btn.Tag.ToString()].Dispose();
dicForm.Remove(btn.Tag.ToString());
}
if (ChangTageKey != null)
{
if (dSkinTabBar.Items.Count > 0)
ChangTageKey(dSkinTabControl.SelectedTab.Name);
}
break;
}
}
if (_IsCloseingTips)
{
if (ClickEvent != null)
ClickEvent("统计", dSkinTabBar.Items.Count.ToString());
}
else
{
if (ClickEvent != null)
ClickEvent("统计", dSkinTabBar.Items.Count.ToString());
}
IsShowScrollBar();
}
private void duiButton_MouseDown(object sender, DuiMouseEventArgs e)
{
DuiButton btn = (DuiButton)sender;
btn.NormalImage = Properties.Resources.tabpage_mousedown;
}
private void dSkinTabItem_MouseEnter(object sender, MouseEventArgs e)
{
DSkinTabItem item = (DSkinTabItem)sender;
foreach (DuiButton btn in item.Controls)
{
btn.Visible = true;
}
}
private void dSkinTabItem_MouseLeave(object sender, EventArgs e)
{
DSkinTabItem item = (DSkinTabItem)sender;
foreach (DuiButton btn in item.Controls)
{
btn.Visible = false;
}
}
private void dSkinTabItem_MouseClick(object sender, DuiMouseEventArgs e)
{
DSkinTabItem item = (DSkinTabItem)sender;
foreach (DSkinTabItem _dskinTabItem in dSkinTabBar.Items)
{
_dskinTabItem.BaseColor = _NormalItemColor;
}
item.BaseColor = _DownItemColor;
}
#endregion
#region 判断是不是在设计器
public static bool IsDesignMode()
{
bool returnFlag = false;
#if DEBUG
if (LicenseManager.UsageMode == LicenseUsageMode.Designtime)
{
returnFlag = true;
}
else if (Process.GetCurrentProcess().ProcessName == "devenv")
{
returnFlag = true;
}
#endif
return returnFlag;
}
#endregion
#region 标签页关闭事件
private void 关闭当前标签页ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (this.dSkinTabControl.TabPages.Count > 0)
{
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.BaseColor == _DownItemColor)
{
dSkinTabBar.Items.Remove(item);
dictTabContrils.Remove(item.Tag.ToString());
dSkinTabControl.TabPages.RemoveByKey(item.Tag.ToString());
if (dicControl.ContainsKey(item.Tag.ToString()))
{
dicControl[item.Tag.ToString()].Dispose();
dicControl.Remove(item.Tag.ToString());
}
if (dicForm.ContainsKey(item.Tag.ToString()))
{
dicForm[item.Tag.ToString()].Close();
dicForm[item.Tag.ToString()].Dispose();
dicForm.Remove(item.Tag.ToString());
}
break;
}
}
IsShowScrollBar();
}
if (this.dSkinTabControl.TabPages.Count > 0)
{
DSkinTabItem item = (DSkinTabItem)dSkinTabBar.Items[0];
item.BaseColor = _DownItemColor;
}
}
catch (Exception) { }
}
private void 关闭所有的标签页ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
if (this.dSkinTabControl.TabPages.Count > 0)
{
this.dSkinTabBar.Items.Clear();
this.dSkinTabControl.TabPages.Clear();
dictTabContrils.Clear();
}
if (_IsCloseingTips)
{
if (ClickEvent != null)
ClickEvent("统计", dSkinTabBar.Items.Count.ToString());
}
else
{
if (ClickEvent != null)
ClickEvent("统计", dSkinTabBar.Items.Count.ToString());
}
IsShowScrollBar();
if (dicControl.Count > 0)
{
foreach (string key in dicControl.Keys)
{
if (!dicControl[key].IsDisposed)
dicControl[key].Dispose();
}
}
if (dicForm.Count > 0)
{
foreach (string key in dicForm.Keys)
{
dicForm[key].Close();
if (!dicForm[key].IsDisposed)
dicForm[key].Dispose();
}
}
System.GC.Collect();
dicControl.Clear();
dicForm.Clear();
}
catch (Exception) { }
}
private void 除此之外全部关闭ToolStripMenuItem_Click(object sender, EventArgs e)
{
try
{
DSkinTabItem _DSkinTabItem = new DSkinTabItem();
TabPage _TabPage = new TabPage();
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.BaseColor == _DownItemColor)
{
_DSkinTabItem = item;
_TabPage = this.dSkinTabControl.TabPages[item.Tag.ToString()];
}
else
dictTabContrils.Remove(item.Tag.ToString());
}
this.dSkinTabBar.Items.Clear();
this.dSkinTabControl.TabPages.Clear();
this.dSkinTabBar.Items.Add(_DSkinTabItem);
this.dSkinTabControl.Controls.Add(_TabPage);
IsShowScrollBar();
foreach (string key in dicControl.Keys)
{
if (key != _DSkinTabItem.Tag.ToString())
{
if (!dicControl[key].IsDisposed)
{
dicControl[key].Dispose();
dicControl.Remove(key);
}
}
}
foreach (string key in dicForm.Keys)
{
if (key != _DSkinTabItem.Tag.ToString())
{
dicForm[key].Close();
if (!dicForm[key].IsDisposed)
{
dicForm[key].Dispose();
dicForm.Remove(key);
}
}
}
System.GC.Collect();
}
catch (Exception) { }
}
#endregion
#region 滚动条改变事件
///
/// 滚动条改变事件
///
///
///
private void dSkinScrollBar_ValueChanged(object sender, EventArgs e)
{
double TempValue = 0.0;
TempValue = this.dSkinScrollBar.Value / 10d;
this.dSkinTabBar.Value = TempValue;
}
#endregion
#region 是否显示滚动条
///
/// 是否显示滚动条
///
private void IsShowScrollBar()
{
int tempWidth = 0;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
tempWidth += item.Width;
}
if ((this.dSkinTabBar.Items.Count * (int)ItemMargin.Left + tempWidth) > this.dSkinTabBar.Width)
this.dSkinScrollBar.Visible = true;
else
this.dSkinScrollBar.Visible = false;
}
#endregion
#region 外部更改内部Item文本显示内容
///
/// 外部更改内部Item文本显示内容
///
/// 需要显示的文本
/// 当时传入的key值
public void SetItemText(string text, string key)
{
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.Tag.ToString() == key)
{
item.Text = text;
break;
}
}
}
#endregion
#region 外部关闭选项卡
///
/// 外部关闭选项卡
///
/// 传入对应的key
public void CloseCurrentItems(string key)
{
try
{
if (this.dSkinTabControl.TabPages.Count > 0)
{
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.Tag.ToString() == key)
{
dSkinTabBar.Items.Remove(item);
dictTabContrils.Remove(item.Tag.ToString());
dSkinTabControl.TabPages.RemoveByKey(item.Tag.ToString());
if (dicControl.ContainsKey(key))
{
dicControl[key].Dispose();
dicControl.Remove(key);
}
if (dicForm.ContainsKey(key))
{
dicForm[key].Dispose();
dicForm.Remove(key);
}
break;
}
}
IsShowScrollBar();
}
}
catch (Exception) { }
}
#endregion
#region 判断关闭是否开启提醒功能
private bool _IsCloseingTips = false;
///
/// 判断关闭是否开启提醒功能
///
public bool IsCloseingTips
{
get { return _IsCloseingTips; }
set { _IsCloseingTips = value; }
}
#endregion
#region 委托文本
public delegate void ClickDelegateHander(string str, string key); //声明一个委托
public event ClickDelegateHander ClickEvent;//声明一个事件
public delegate void ChangTageKeyDelegateHander(string tagkey); //声明一个委托
public event ChangTageKeyDelegateHander ChangTageKey;//声明一个事件
#endregion
#region 返回当前标签页的Key
///
/// 返回当前标签页的Key
///
///
public string ReturnKey()
{
string key = string.Empty;
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.BaseColor == _DownItemColor)
{
key = item.Tag.ToString();
break;
}
}
return key;
}
#endregion
#region 事件委托
//定义委托
public delegate void ClickHandle(object sender, EventArgs e);
//定义事件
public event ClickHandle UserControlClosing;
#endregion
#region 外部关内存所有的标签页
///
/// 外部关内存所有的标签页
///
public void CloseAllItems()
{
if (dSkinTabBar.Items.Count > 0)
关闭所有的标签页ToolStripMenuItem_Click(null, null);
}
#endregion
#region TabControlSelectedIndexChanged 将切换的key可以传出
private void dSkinTabControl_SelectedIndexChanged(object sender, EventArgs e)
{
if (ChangTageKey != null)
{
if (dSkinTabBar.Items.Count > 0)
{
ChangTageKey(dSkinTabControl.SelectedTab.Name);
}
}
}
#endregion
#region 选择对应的标签页
///
/// 选择对应的标签页
///
///
public void SelectTabPage(string key)
{
if (this.dSkinTabControl.TabPages.ContainsKey(key))
this.dSkinTabControl.SelectTab(key);
foreach (DSkinTabItem item in dSkinTabBar.Items)
{
if (item.Tag.ToString() != key)
item.BaseColor = _NormalItemColor;
else
item.BaseColor = _DownItemColor;
}
}
#endregion
#region 计算Item的宽度
///
/// 计算显示框高度和宽度,英文字体和中文以及标点、数字的宽度各不相同,需计算
///
/// 文本
/// 宽度
private int CalcItemWidth(string mess, int width)
{
int Linewidth; //显示对话内容容器的宽度和高度
//临时建立一个容器装入内容
RichTextBox canv_Rich = new RichTextBox();
//先取全部Rtf的值
canv_Rich.Rtf = mess;
//再按照Txt判断文字
//判断中文
MatchCollection zh = Regex.Matches(canv_Rich.Text, @"[\u4e00-\u9fa5]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
//判断中文标点
MatchCollection zhdot = Regex.Matches(canv_Rich.Text, @"[,。;?~!:‘“”’【】()]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
//判断数字
MatchCollection en = Regex.Matches(canv_Rich.Text, @"[1234567890]", RegexOptions.IgnoreCase | RegexOptions.Singleline);
//其余为英文,并计算总宽度
Linewidth = (zh.Count + zhdot.Count) * 13 + en.Count * 8 + (canv_Rich.Text.Length - zh.Count - zhdot.Count - en.Count) * 7;
//接收数据内容中是否包含图像
int havePic = 0;
havePic = Regex.Matches(canv_Rich.Rtf, "Paint.Picture").Count;
//加上图像宽度
Linewidth += havePic * 33;
//判断极值
//if (Linewidth > (12 * 13)) { Linewidth = 12 * 13; }
if (Linewidth < width) { Linewidth = width; }
return Linewidth;
}
#endregion
#region 返回TabPage
///
/// 返回TabPage
///
/// 对应的KEY
///
public TabPage ReturnTabPage(string key)
{
TabPage _TabPage = null;
if (this.dSkinTabControl.TabPages.ContainsKey(key))
_TabPage = this.dSkinTabControl.TabPages[key];
return _TabPage;
}
#endregion
}
}
TabControlYd.Designer.cs完整代码如下:
namespace SCommon
{
partial class TabControlYd
{
///
/// 必需的设计器变量。
///
private System.ComponentModel.IContainer components = null;
///
/// 清理所有正在使用的资源。
///
/// 如果应释放托管资源,为 true;否则为 false。
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region 组件设计器生成的代码
///
/// 设计器支持所需的方法 - 不要
/// 使用代码编辑器修改此方法的内容。
///
private void InitializeComponent()
{
this.components = new System.ComponentModel.Container();
this.dSkinTabBar = new DSkin.Controls.DSkinTabBar();
this.dSkinTabControl = new DSkin.Controls.DSkinTabControl();
this.contextMenuStrip = new System.Windows.Forms.ContextMenuStrip(this.components);
this.关闭当前标签页ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.关闭所有的标签页ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.除此之外全部关闭ToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.dSkinScrollBar = new DSkin.Controls.DSkinScrollBar();
this.toolTip1 = new System.Windows.Forms.ToolTip(this.components);
this.richTextBox1 = new System.Windows.Forms.RichTextBox();
this.contextMenuStrip.SuspendLayout();
this.SuspendLayout();
//
// dSkinTabBar
//
this.dSkinTabBar.BackColor = System.Drawing.Color.SkyBlue;
this.dSkinTabBar.BitmapCache = true;
this.dSkinTabBar.Dock = System.Windows.Forms.DockStyle.Top;
this.dSkinTabBar.EnabledLayoutContent = true;
this.dSkinTabBar.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.dSkinTabBar.Location = new System.Drawing.Point(0, 0);
this.dSkinTabBar.Name = "dSkinTabBar";
this.dSkinTabBar.Orientation = System.Windows.Forms.Orientation.Horizontal;
this.dSkinTabBar.Size = new System.Drawing.Size(611, 40);
this.dSkinTabBar.TabControl = this.dSkinTabControl;
this.dSkinTabBar.TabIndex = 1;
this.dSkinTabBar.Text = "dSkinTabBar1";
//
// dSkinTabControl
//
this.dSkinTabControl.BitmapCache = false;
this.dSkinTabControl.Dock = System.Windows.Forms.DockStyle.Fill;
this.dSkinTabControl.HoverBackColors = new System.Drawing.Color[] {
System.Drawing.Color.Transparent,
System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))))};
this.dSkinTabControl.ImgSize = new System.Drawing.Size(0, 0);
this.dSkinTabControl.ItemBackgroundImage = null;
this.dSkinTabControl.ItemBackgroundImageHover = null;
this.dSkinTabControl.ItemBackgroundImageSelected = null;
this.dSkinTabControl.ItemSize = new System.Drawing.Size(0, 1);
this.dSkinTabControl.Location = new System.Drawing.Point(0, 52);
this.dSkinTabControl.Name = "dSkinTabControl";
this.dSkinTabControl.NormalBackColors = new System.Drawing.Color[] {
System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(128)))), ((int)(((byte)(128)))), ((int)(((byte)(128))))),
System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))))};
this.dSkinTabControl.PageImagePosition = DSkin.Controls.ePageImagePosition.Left;
this.dSkinTabControl.SelectedBackColors = new System.Drawing.Color[] {
System.Drawing.Color.FromArgb(((int)(((byte)(100)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255))))),
System.Drawing.Color.FromArgb(((int)(((byte)(200)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))), ((int)(((byte)(255)))))};
this.dSkinTabControl.Size = new System.Drawing.Size(611, 273);
this.dSkinTabControl.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
this.dSkinTabControl.TabIndex = 2;
this.dSkinTabControl.UpdownBtnArrowNormalColor = System.Drawing.Color.Black;
this.dSkinTabControl.UpdownBtnArrowPressColor = System.Drawing.Color.Gray;
this.dSkinTabControl.UpdownBtnBackColor = System.Drawing.Color.White;
this.dSkinTabControl.UpdownBtnBorderColor = System.Drawing.Color.Black;
this.dSkinTabControl.SelectedIndexChanged += new System.EventHandler(this.dSkinTabControl_SelectedIndexChanged);
//
// contextMenuStrip
//
this.contextMenuStrip.Items.AddRange(new System.Windows.Forms.ToolStripItem[] {
this.关闭当前标签页ToolStripMenuItem,
this.关闭所有的标签页ToolStripMenuItem,
this.除此之外全部关闭ToolStripMenuItem});
this.contextMenuStrip.Name = "contextMenuStrip";
this.contextMenuStrip.Size = new System.Drawing.Size(173, 70);
//
// 关闭当前标签页ToolStripMenuItem
//
this.关闭当前标签页ToolStripMenuItem.Name = "关闭当前标签页ToolStripMenuItem";
this.关闭当前标签页ToolStripMenuItem.Size = new System.Drawing.Size(172, 22);
this.关闭当前标签页ToolStripMenuItem.Text = "关闭当前标签页";
this.关闭当前标签页ToolStripMenuItem.Click += new System.EventHandler(this.关闭当前标签页ToolStripMenuItem_Click);
//
// 关闭所有的标签页ToolStripMenuItem
//
this.关闭所有的标签页ToolStripMenuItem.Name = "关闭所有的标签页ToolStripMenuItem";
this.关闭所有的标签页ToolStripMenuItem.Size = new System.Drawing.Size(172, 22);
this.关闭所有的标签页ToolStripMenuItem.Text = "关闭所有的标签页";
this.关闭所有的标签页ToolStripMenuItem.Click += new System.EventHandler(this.关闭所有的标签页ToolStripMenuItem_Click);
//
// 除此之外全部关闭ToolStripMenuItem
//
this.除此之外全部关闭ToolStripMenuItem.Name = "除此之外全部关闭ToolStripMenuItem";
this.除此之外全部关闭ToolStripMenuItem.Size = new System.Drawing.Size(172, 22);
this.除此之外全部关闭ToolStripMenuItem.Text = "除此之外全部关闭";
this.除此之外全部关闭ToolStripMenuItem.Click += new System.EventHandler(this.除此之外全部关闭ToolStripMenuItem_Click);
//
// dSkinScrollBar
//
this.dSkinScrollBar.ArrowHeight = 12;
this.dSkinScrollBar.ArrowHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(180)))), ((int)(((byte)(180)))), ((int)(((byte)(180)))));
this.dSkinScrollBar.ArrowNormalColor = System.Drawing.Color.Gray;
this.dSkinScrollBar.ArrowPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(80)))), ((int)(((byte)(80)))));
this.dSkinScrollBar.ArrowScrollBarGap = 3;
this.dSkinScrollBar.Dock = System.Windows.Forms.DockStyle.Top;
this.dSkinScrollBar.LargeChange = 1;
this.dSkinScrollBar.Location = new System.Drawing.Point(0, 40);
this.dSkinScrollBar.Maximum = 10;
this.dSkinScrollBar.Minimum = 0;
this.dSkinScrollBar.Name = "dSkinScrollBar";
this.dSkinScrollBar.Orientation = System.Windows.Forms.Orientation.Horizontal;
this.dSkinScrollBar.ScrollBarHoverColor = System.Drawing.Color.FromArgb(((int)(((byte)(158)))), ((int)(((byte)(158)))), ((int)(((byte)(158)))));
this.dSkinScrollBar.ScrollBarHoverImage = null;
this.dSkinScrollBar.ScrollBarLenght = 20;
this.dSkinScrollBar.ScrollBarNormalColor = System.Drawing.Color.Gray;
this.dSkinScrollBar.ScrollBarNormalImage = null;
this.dSkinScrollBar.ScrollBarPadding = 1;
this.dSkinScrollBar.ScrollBarPartitionWidth = new System.Windows.Forms.Padding(5);
this.dSkinScrollBar.ScrollBarPressColor = System.Drawing.Color.FromArgb(((int)(((byte)(80)))), ((int)(((byte)(80)))), ((int)(((byte)(80)))));
this.dSkinScrollBar.ScrollBarPressImage = null;
this.dSkinScrollBar.ShowArrow = true;
this.dSkinScrollBar.Size = new System.Drawing.Size(611, 12);
this.dSkinScrollBar.SmallChange = 1;
this.dSkinScrollBar.TabIndex = 3;
this.dSkinScrollBar.Text = "dSkinScrollBar";
this.dSkinScrollBar.Value = 0;
this.dSkinScrollBar.Visible = false;
this.dSkinScrollBar.ValueChanged += new System.EventHandler(this.dSkinScrollBar_ValueChanged);
//
// richTextBox1
//
this.richTextBox1.Location = new System.Drawing.Point(234, 1);
this.richTextBox1.Name = "richTextBox1";
this.richTextBox1.Size = new System.Drawing.Size(191, 33);
this.richTextBox1.TabIndex = 4;
this.richTextBox1.Text = "";
this.richTextBox1.Visible = false;
//
// TabControlYd
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 17F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.BackColor = System.Drawing.Color.White;
this.Controls.Add(this.richTextBox1);
this.Controls.Add(this.dSkinTabControl);
this.Controls.Add(this.dSkinScrollBar);
this.Controls.Add(this.dSkinTabBar);
this.Font = new System.Drawing.Font("微软雅黑", 9F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(134)));
this.Margin = new System.Windows.Forms.Padding(3, 4, 3, 4);
this.Name = "TabControlYd";
this.Size = new System.Drawing.Size(611, 325);
this.contextMenuStrip.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
private System.Windows.Forms.ContextMenuStrip contextMenuStrip;
private System.Windows.Forms.ToolStripMenuItem 关闭当前标签页ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 关闭所有的标签页ToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem 除此之外全部关闭ToolStripMenuItem;
private DSkin.Controls.DSkinScrollBar dSkinScrollBar;
private DSkin.Controls.DSkinTabBar dSkinTabBar;
private DSkin.Controls.DSkinTabControl dSkinTabControl;
private System.Windows.Forms.ToolTip toolTip1;
private System.Windows.Forms.RichTextBox richTextBox1;
}
}
我们创建好控件生成之后,直接拖拽到对应的页面就可以。委托事件调用方法为:
this.tabControlYd1.ClickEvent += tabControlYd1_ClickEvent;
this.tabControlYd1.ChangTageKey += tabControlYd1_ChangTageKey;