我创建了一个扩展选项卡控件。在该选项卡控件中我想放置关闭按钮(就像Chrome浏览器选项卡一样)。怎么会发生?请提出任何建议。以下代码用于创建选项卡控件
public class ExtendedTabControl : TabControl
{
public ExtendedTabControl()
{
this.DrawMode = TabDrawMode.OwnerDrawFixed;
SetStyle(ControlStyles.AllPaintingInWmPaint | ControlStyles.DoubleBuffer | ControlStyles.ResizeRedraw |
ControlStyles.UserPaint | ControlStyles.SupportsTransparentBackColor, true);
this.Invalidate();
BorderWidth =1;
}
public Color ActiveBorderColor { get; set; }
public Color InactiveBorderColor { get; set; }
public Color ActiveTabColor { get; set; }
public Color InActiveTabColor { get; set; }
public float BorderWidth { get; set; }
protected override void OnSelectedIndexChanged(EventArgs e)
{
base.OnSelectedIndexChanged(e);
//TabPages[this.SelectedIndex]
this.Invalidate();
}
protected override void OnPaintBackground(PaintEventArgs e)
{
base.OnPaintBackground(e);
for (int i = 0; i < this.TabCount; i++)
{
if (i != this.SelectedIndex)
{
DrawTab(e.Graphics, i, false);
}
}
//TabPages[this.SelectedIndex].BackColor = ActiveTabColor;
DrawTab(e.Graphics, this.SelectedIndex, true);
}
protected override void OnPaint(PaintEventArgs e)
{
//base.OnPaint(e);
}
private void DrawTab(Graphics g, int i, bool isSelected)
{
if (i < 0)
{
return;
}
Rectangle rectangle = GetTabRect(i);
if (isSelected)
{
rectangle.Height += 2;
}
GraphicsPath path = new GraphicsPath();
Point p1 = new Point(rectangle.X + 20, rectangle.Y);
Point p2 = new Point(rectangle.X, rectangle.Y + rectangle.Size.Height);
path.AddLine(p1, p2);
Point p3 = new Point(rectangle.X + rectangle.Width, rectangle.Y);
path.AddLine(p1, p3);
Point p4 = new Point(rectangle.Left + rectangle.Width + 24, rectangle.Top + rectangle.Height);
path.AddLine(p3, p4);
path.AddLine(p4, p2);
if (isSelected)
{
using (Brush brush = new SolidBrush(ActiveTabColor))
{
g.FillPath(brush, path);
}
using (Pen pen = new Pen(ActiveBorderColor, BorderWidth))
{
DrawTabBorder(g, pen);
}
}
else
{
using (Brush brushI = new SolidBrush(InActiveTabColor))
{
g.FillPath(brushI, path);
}
using (Pen pen = new Pen(InactiveBorderColor, BorderWidth))
{
DrawTabBorder(g, pen);
}
}
}
private void DrawTabBorder(Graphics g, Pen pen)
{
if (this.SelectedIndex < 0)
{
return;
}
Rectangle selectedRectangel = GetTabRect(this.SelectedIndex);
#region coment
//selectedRectangel.Width -= 6;
//selectedRectangel.X += 1;
//Point pRight = new Point(this.SelectedTab.Bounds.Location.X + this.SelectedTab.Width + 1,
//this.SelectedTab.Bounds.Location.Y - 1);
//Point pRightBottam = new Point(this.SelectedTab.Bounds.Location.X + this.SelectedTab.Width + 1,
//this.SelectedTab.Bounds.Location.Y + this.SelectedTab.Bounds.Height + 1);
//Point pLeftTabStart = new Point(selectedRectangel.X, pLeft.Y);
//Point pLeftTabStop = new Point(selectedRectangel.X + selectedRectangel.Width + selectedRectangel.Height + 1 ,
#endregion
Point pLeft = new Point(this.SelectedTab.Bounds.Location.X, this.SelectedTab.Bounds.Location.Y);
Point pLeftBottam = new Point(this.SelectedTab.Bounds.Location.X - 1,
this.SelectedTab.Bounds.Location.Y + this.SelectedTab.Bounds.Height + 1);
Point pt1 = selectedRectangel.Location;
pt1.X += 20;
Point pt2 = new Point(selectedRectangel.Location.X + selectedRectangel.Width + 1,
selectedRectangel.Location.Y + 1);
Point pt3 = new Point(selectedRectangel.X, selectedRectangel.Y + selectedRectangel.Height);
Point pt4 = new Point(selectedRectangel.Left+selectedRectangel.Width+24,
selectedRectangel.Top+selectedRectangel.Height);
Point pt6 = new Point(this.SelectedTab.Bounds.Location.X + this.SelectedTab.Width + 1,
this.SelectedTab.Bounds.Location.Y - 1);
Point pt7 = new Point(pLeftBottam.X+this.SelectedTab.Bounds.Width,pLeftBottam.Y);
Point pt8 = pLeftBottam;
GraphicsPath pathTab = new GraphicsPath();
pathTab.AddLine(pt3, pt1);
pathTab.AddLine(pt1, pt2);
pathTab.AddLine(pt2, pt4);
pathTab.AddLine(pt4, pt6);
pathTab.AddLine(pt6, pt7);
pathTab.AddLine(pt7, pt8);
pathTab.AddLine(pt8, pLeft);
pathTab.AddLine(pLeft, pt3);
g.SmoothingMode = SmoothingMode.AntiAlias;
pathTab.Widen(pen);
g.DrawPath(pen, pathTab);
}
protected override void OnSelected(TabControlEventArgs e)
{
TabPage page = this.SelectedTab;
page.BackColor = ActiveTabColor;
}
}