-
Notifications
You must be signed in to change notification settings - Fork 0
/
TabControlEx.cs
75 lines (61 loc) · 2.36 KB
/
TabControlEx.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
using System.Windows.Forms;
using System.Drawing;
class TabControlEx : TabControl
{
public string pathCloseImg = "\\images\\close.png";
public string onCloseMsg = "Would you like to Close this Tab?";
public TabControlEx()
{
Setup();
}
public void Setup()
{
Padding = new Point(25, 3);
DrawMode = TabDrawMode.OwnerDrawFixed;
}
protected override void OnDrawItem(DrawItemEventArgs e)
{
RectangleF tabFill = (RectangleF)GetTabRect(e.Index);
if (e.Index == SelectedIndex)
{
Brush textBrush = new SolidBrush(Color.Black);
Brush fillBrush = new SolidBrush(Color.White);
e.Graphics.FillRectangle(fillBrush, tabFill);
e.Graphics.DrawString(TabPages[e.Index].Text, Font, textBrush, new Point(e.Bounds.X + 5, e.Bounds.Y + 5));
int offset = (e.Bounds.Height - 16) / 2;
e.Graphics.DrawImage(Image.FromFile(pathCloseImg), e.Bounds.X + e.Bounds.Width - 20, e.Bounds.Y + offset);
textBrush.Dispose();
fillBrush.Dispose();
}
else
{
Brush textBrush = new SolidBrush(Color.White);
Brush fillBrush = new SolidBrush(Color.DimGray);
e.Graphics.FillRectangle(fillBrush, tabFill);
fillBrush.Dispose();
e.Graphics.DrawString(TabPages[e.Index].Text, Font, textBrush, new Point(e.Bounds.X + 5, e.Bounds.Y + 3));
int offset = (e.Bounds.Height - 16) / 2;
e.Graphics.DrawImage(Image.FromFile(pathCloseImg), e.Bounds.X + e.Bounds.Width - 20, e.Bounds.Y + offset + 2);
textBrush.Dispose();
fillBrush.Dispose();
}
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
for (int i = 0; i < TabPages.Count; i++)
{
Rectangle bounds = GetTabRect(i);
int offset = (bounds.Height - 16) / 2;
Rectangle closeButton = new Rectangle(bounds.X + bounds.Width - 20, bounds.Y + offset, 14, 16);
if (closeButton.Contains(e.Location))
{
if (MessageBox.Show(onCloseMsg, "Confirm", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
{
TabPages.RemoveAt(i);
break;
}
}
}
}
}