可自定义header背景色,headertext字体以及颜色,带关闭按钮。
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace PACS.Common.Controls
{
public partial class MyTabcontrol : TabControl
{
private const int CLOSE_SIZE = 15;
private bool _isEnterClose;//是否进入关闭区域
private int _enterIndex;//当前进入的tabpageIndex
public MyTabcontrol()
{
this.Padding = new Point(12, 3);
//DrawMode:指定用户还是系统来绘制标题
this.DrawMode = TabDrawMode.OwnerDrawFixed;
this.HotTrack = true;
this.DrawItem += new DrawItemEventHandler(this.MyTabControl_DrawItem);
this.MouseDown += new MouseEventHandler(this.MyTabControl_MouseDown);
this.MouseMove += MyTabcontrol_MouseMove;
}
private void MyTabcontrol_MouseMove(object sender, MouseEventArgs e)
{
int x = e.X, y = e.Y;
//计算关闭区域
for (int i = 0; i < this.TabPages.Count; i++)
{
Rectangle tab = this.GetTabRect(i);
tab.Offset(tab.Width - (CLOSE_SIZE + 1), tab.Height / 2 - CLOSE_SIZE / 2);
tab.Width = CLOSE_SIZE;
tab.Height = CLOSE_SIZE;
//如果鼠标在区域内就关闭选项卡
_isEnterClose = x > tab.X && x < tab.Right && y > tab.Y && y < tab.Bottom;
if (_isEnterClose)
{
_enterIndex = i;
return;
}
}
}
private void MyTabControl_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
//循环所有TabPages
for (int i = 0; i < this.TabPages.Count; i++)
{
Rectangle rect;
//是否为当前选中tab页
if (this.SelectedIndex == i)
{
rect = this.GetTabRect(this.SelectedIndex);
//填充选中tabpage页header颜色
e.Graphics.FillRectangle(new SolidBrush(SelectedHeaderColor), rect);
}
else
{
var orginRec = this.GetTabRect(i);
rect = new Rectangle(orginRec.Location.X, orginRec.Location.Y, orginRec.Width - 1, orginRec.Height);
//填充未选中tabpage页header颜色
e.Graphics.FillRectangle(new SolidBrush(HeaderBackColor), rect);
}
TabPage tbg = TabPages[i];
//绘制header文字
e.Graphics.DrawString(tbg.Text, this.Font, SystemBrushes.ControlText, rect.X + 2, rect.Y + 2);
if(tbg.Text == "首页")
continue;
//绘制关闭按钮的矩形框
var tabHeight = rect.Height;
Rectangle recClose = new Rectangle(rect.X + rect.Width - CLOSE_SIZE - 1, tabHeight / 2 - CLOSE_SIZE / 2, CLOSE_SIZE, CLOSE_SIZE);
e.Graphics.DrawRectangle(new Pen(Color.Transparent), recClose);
//填充关闭按钮的矩形框
Color recColor = e.State == DrawItemState.Selected ? Color.FromArgb(181, 224, 252) : Color.Transparent;
using (Brush b = new SolidBrush(recColor))
{
e.Graphics.FillEllipse(b, recClose);
}
//画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
//"\"线
Point p1 = new Point(recClose.X + 3, recClose.Y + 3);
Point p2 = new Point(recClose.X + recClose.Width - 3, recClose.Y + recClose.Height - 3);
e.Graphics.DrawLine(objpen, p1, p2);
//"/"线
Point p3 = new Point(recClose.X + 3, recClose.Y + recClose.Height - 3);
Point p4 = new Point(recClose.X + recClose.Width - 3, recClose.Y + 3);
e.Graphics.DrawLine(objpen, p3, p4);
}
}
//选中的标签页填充颜色
e.Graphics.Dispose();
}
catch (Exception)
{ }
}
private int _oldSelectedIndex;
public Action<string> DeletePage;
private void MyTabControl_MouseDown(object sender, MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
if (_isEnterClose == true)
{
if (_enterIndex == 0)
return;
DeletePage.Invoke(TabPages[_enterIndex].Name);
if (_enterIndex == _oldSelectedIndex)
{
if (_enterIndex == TabPages.Count - 1)
{
this.TabPages.Remove(TabPages[_enterIndex]);
this.SelectedTab = this.TabPages[this.TabPages.Count - 1];
_oldSelectedIndex = this.TabPages.Count - 1;
}
else
{
this.TabPages.Remove(TabPages[_enterIndex]);
this.SelectedTab = this.TabPages[_enterIndex];
_oldSelectedIndex = _enterIndex;
}
}
else if (_enterIndex < _oldSelectedIndex)
{
this.TabPages.Remove(TabPages[_enterIndex]);
this.SelectedTab = this.TabPages[_oldSelectedIndex - 1];
_oldSelectedIndex = _oldSelectedIndex - 1;
}
else
{
this.TabPages.Remove(TabPages[_enterIndex]);
this.SelectedTab = this.TabPages[_oldSelectedIndex];
}
}
else
{
_oldSelectedIndex = SelectedIndex;
}
}
}
private Color _headerColor = Color.White;
public Color SelectedHeaderColor
{
get { return _headerColor; }
set { _headerColor = value; }
}
private Color _headerBackColor = Color.White;
public Color HeaderBackColor
{
get { return _headerBackColor; }
set { _headerBackColor = value; }
}
}
}