抄袭师姐的代码,实现标签选择颜色变化,给标签添加关闭标示。无法实现鼠标移到标签上发生明显变化。
public MainForm()
{this.tabControlForInfoM.DrawItem += new DrawItemEventHandler(tabControlForInfoM_DrawItem);
tabControlForInfoM.MouseDown += new System.Windows.Forms.MouseEventHandler(this.tabControlForInfoM_MouseDown);
}
/*****************************************************************************
* 函数功能:重绘tabpage
* 函数说明:如果 DrawMode 属性设置为 OwnerDrawFixed,则当 TabControl 需要绘制它的每一个选项卡时发生
* **************************************************************************/
private void tabControlForInfoM_DrawItem(object sender, DrawItemEventArgs e)
{
try
{
Rectangle myTabRect = this.tabControlForInfoM.GetTabRect(e.Index);//返回该选项卡控件中的指定选项卡的边框
Rectangle origRect = myTabRect;
Pen p = new Pen(Color.White);
Color recColor = Color.WhiteSmoke;
Brush b = new SolidBrush(recColor);
e.Graphics.DrawRectangle(p, myTabRect);//画边框
e.Graphics.FillRectangle(b, myTabRect);//填充矩形框内颜色
//先添加TabPage 名称
e.Graphics.DrawString(this.tabControlForInfoM.TabPages[e.Index].Text
, this.Font, SystemBrushes.ControlText, myTabRect.X +18, myTabRect.Y + 8);
if (e.Index == tabControlForInfoM.SelectedIndex)
{
e.Graphics.FillRectangle(new SolidBrush(Color.Aqua), myTabRect);
//再画一个矩形框
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 5), 5);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
e.Graphics.DrawRectangle(p, myTabRect);
//填充矩形框
e.Graphics.FillRectangle(new SolidBrush(Color.DodgerBlue), myTabRect);
//画关闭符号
using (Pen objpen = new Pen(Color.Black))
{
//"\"线
Point p1 = new Point(myTabRect.X + 3, myTabRect.Y + 3);
Point p2 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + myTabRect.Height - 3);
e.Graphics.DrawLine(objpen, p1, p2);
//"/"线
Point p3 = new Point(myTabRect.X + 3, myTabRect.Y + myTabRect.Height - 3);
Point p4 = new Point(myTabRect.X + myTabRect.Width - 3, myTabRect.Y + 3);
e.Graphics.DrawLine(objpen, p3, p4);
}
//写标签文字
e.Graphics.DrawString(this.tabControlForInfoM.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, origRect.X + 18, origRect.Y + 8);
p.Dispose();
b.Dispose();
e.Graphics.Dispose();
}
TabPage newpag=tabControlForInfoM.TabPages[e.Index];
if(newpag.Tag==(object)1)
{
TabPage newp = tabControlForInfoM.TabPages[e.Index];
e.Graphics.FillRectangle(Brushes.Yellow, myTabRect);
e.Graphics.DrawString(this.tabControlForInfoM.TabPages[e.Index].Text, this.Font, SystemBrushes.ControlText, origRect.X + 18, origRect.Y + 8);
}
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
/*********************************************************************************************
* 函数功能:处理tabcontrol鼠标按下事件
* 函数说明:计算位置 实现关闭tabpage 关闭操作
* *****************************************************************************************/
private void tabControlForInfoM_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
{
if (e.Button == MouseButtons.Left)
{
int x = e.X, y = e.Y;
//计算关闭区域
Rectangle myTabRect = tabControlForInfoM.GetTabRect(tabControlForInfoM.SelectedIndex);
myTabRect.Offset(myTabRect.Width - (CLOSE_SIZE + 5), 5);
myTabRect.Width = CLOSE_SIZE;
myTabRect.Height = CLOSE_SIZE;
如果鼠标在区域内就关闭选项卡
if (x > myTabRect.Left && x < myTabRect.Right && y > myTabRect.Top && y < myTabRect.Bottom)
{
tabControlForInfoM.TabPages.Remove(tabControlForInfoM.SelectedTab);
}
}
if (tabControlForInfoM.TabPages.Count == 0)
{
tabControlForInfoM.Visible = false;
webBrowserForFirm.Visible = true;
}
}