-
Notifications
You must be signed in to change notification settings - Fork 209
/
Copy pathTabControlEx.cs
58 lines (54 loc) · 1.53 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
using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Modbus.Common.Properties;
namespace Modbus.Common
{
public delegate bool PreRemoveTab(int indx);
public class TabControlEx : TabControl
{
public TabControlEx()
: base()
{
PreRemoveTabPage = null;
this.DrawMode = TabDrawMode.OwnerDrawFixed;
}
public PreRemoveTab PreRemoveTabPage;
protected override void OnDrawItem(DrawItemEventArgs e)
{
var r = GetTabRect(e.Index);
r.Offset(2, 2);
r.Width = 10;
r.Height = 10;
e.Graphics.DrawIcon(Resources.close, r);
e.Graphics.DrawString(TabPages[e.Index].Text, e.Font, Brushes.Black, new PointF(r.X + 10, r.Y));
e.DrawFocusRectangle();
}
protected override void OnMouseClick(MouseEventArgs e)
{
Point p = e.Location;
for (int i = 0; i < TabCount; i++)
{
Rectangle r = GetTabRect(i);
r.Width = 10;
if (r.Contains(p))
{
CloseTab(i);
}
}
}
private void CloseTab(int i)
{
if (PreRemoveTabPage != null)
{
bool closeIt = PreRemoveTabPage(i);
if (!closeIt)
return;
}
TabPages.Remove(TabPages[i]);
}
}
}