001.
using
System;
002.
using
System.Collections.Generic;
003.
using
System.Linq;
004.
using
System.Windows.Forms;
005.
using
System.ComponentModel.Design;
006.
using
System.ComponentModel;
007.
using
System.Drawing;
008.
using
System.Drawing.Drawing2D;
009.
010.
namespace
TORServices
011.
{
012.
#region _SidTabControl
013.
public
class
cntlSidTabControl : System.Windows.Forms.TabControl
014.
{
015.
public
void
AddNewForm(Form form)
016.
{
017.
for
(
int
i = 0; i <
this
.TabCount;i++ )
018.
{
019.
if
(
this
.TabPages[i].Text == form.Text)
020.
{
021.
this
.TabIndex = i;
022.
return
;
023.
}
024.
}
025.
TabPage tab =
new
TabPage();
026.
this
.Controls.Add(tab);
027.
form.TopLevel =
false
;
028.
form.Anchor = (AnchorStyles.Bottom | AnchorStyles.Top | AnchorStyles.Right | AnchorStyles.Left);
029.
form.Dock = DockStyle.Fill;
030.
form.FormBorderStyle = System.Windows.Forms.FormBorderStyle.None;
031.
form.Parent = tab;
032.
tab.Text = form.Text;
033.
try
{ form.Show(); }
034.
catch
{ }
035.
036.
this
.SelectedTab = tab;
037.
038.
}
039.
public
cntlSidTabControl()
040.
{
041.
042.
SetStyle(ControlStyles.DoubleBuffer,
true
);
043.
044.
TabStop =
false
;
045.
046.
DrawMode = TabDrawMode.OwnerDrawFixed;
047.
048.
_closeButtonBrush =
new
SolidBrush(_closeButtonColor);
049.
050.
ItemSize =
new
Size(ItemSize.Width, 24);
051.
052.
053.
054.
Padding =
new
Point(16, 0);
055.
056.
}
057.
058.
protected
override
void
Dispose(
bool
disposing)
059.
{
060.
061.
if
(disposing)
062.
{
063.
064.
_stringFormat.Dispose();
065.
066.
_closeButtonBrush.Dispose();
067.
068.
}
069.
070.
base
.Dispose(disposing);
071.
072.
}
073.
074.
public
delegate
void
TabClosedDelegate(
object
sender, ClosedEventArgs e);
075.
076.
public
delegate
void
TabClosingDelegate(
object
sender, ClosingEventArgs e);
077.
078.
public
event
TabClosedDelegate TabClosed;
079.
080.
public
event
TabClosingDelegate TabClosing;
081.
082.
private
int
_buttonWidth = 16;
083.
084.
[DefaultValue(16), Category(
"Action Buttons"
)]
085.
086.
public
int
ButtonWidth
087.
{
088.
089.
get
{
return
_buttonWidth; }
090.
091.
set
{ _buttonWidth = value; }
092.
093.
}
094.
095.
private
int
_crossOffset = 3;
096.
097.
[DefaultValue(3), Category(
"Action Buttons"
)]
098.
099.
public
int
CrossOffset
100.
{
101.
102.
get
{
return
_crossOffset; }
103.
104.
set
{ _crossOffset = value; }
105.
106.
}
107.
108.
private
readonly
StringFormat _stringFormat =
new
StringFormat
109.
110.
{
111.
112.
Alignment = StringAlignment.Near,
113.
114.
LineAlignment = StringAlignment.Center
115.
116.
};
117.
118.
private
Color _closeButtonColor = Color.Red;
119.
120.
private
Brush _closeButtonBrush;
121.
122.
[Category(
"Action Buttons"
)]
123.
124.
public
Color CloseButtonColor
125.
{
126.
127.
get
{
return
_closeButtonColor; }
128.
129.
set
130.
{
131.
132.
_closeButtonBrush.Dispose();
133.
134.
_closeButtonColor = value;
135.
136.
_closeButtonBrush =
new
SolidBrush(_closeButtonColor);
137.
138.
Invalidate();
139.
140.
}
141.
142.
}
143.
144.
protected
override
void
OnDrawItem(DrawItemEventArgs e)
145.
{
146.
147.
if
(e.Bounds != RectangleF.Empty)
148.
{
149.
150.
e.Graphics.SmoothingMode = SmoothingMode.AntiAlias;
151.
152.
for
(
int
nIndex = 0; nIndex < TabCount; nIndex++)
153.
{
154.
155.
Rectangle tabArea = GetTabRect(nIndex);
156.
157.
Rectangle closeBtnRect = GetCloseBtnRect(tabArea);
158.
159.
if
(nIndex != SelectedIndex)
160.
{
161.
162.
e.Graphics.DrawRectangle(Pens.DarkGray, closeBtnRect);
163.
164.
DrawCross(e, closeBtnRect, Color.DarkGray);
165.
166.
}
167.
168.
else
169.
{
170.
171.
172.
173.
e.Graphics.FillRectangle(_closeButtonBrush, closeBtnRect);
174.
175.
e.Graphics.DrawRectangle(Pens.White, closeBtnRect);
176.
177.
DrawCross(e, closeBtnRect, Color.White);
178.
179.
}
180.
181.
string
str = TabPages[nIndex].Text;
182.
183.
e.Graphics.DrawString(str, Font,
new
SolidBrush(TabPages[nIndex].ForeColor), tabArea, _stringFormat);
184.
185.
}
186.
187.
}
188.
189.
}
190.
191.
private
void
DrawCross(DrawItemEventArgs e, Rectangle btnRect, Color color)
192.
{
193.
194.
using
(Pen pen =
new
Pen(color, 2))
195.
{
196.
197.
float
x1 = btnRect.X + CrossOffset;
198.
199.
float
x2 = btnRect.Right - CrossOffset;
200.
201.
float
y1 = btnRect.Y + CrossOffset;
202.
203.
float
y2 = btnRect.Bottom - CrossOffset;
204.
205.
e.Graphics.DrawLine(pen, x1, y1, x2, y2);
206.
207.
e.Graphics.DrawLine(pen, x1, y2, x2, y1);
208.
209.
}
210.
211.
}
212.
213.
private
Rectangle GetCloseBtnRect(Rectangle tabRect)
214.
{
215.
216.
Rectangle rect =
new
Rectangle(tabRect.X + tabRect.Width - ButtonWidth - 4, (tabRect.Height - ButtonWidth) / 2, ButtonWidth, ButtonWidth);
217.
218.
return
rect;
219.
220.
}
221.
222.
protected
override
void
OnMouseDown(MouseEventArgs e)
223.
{
224.
225.
if
(!DesignMode)
226.
{
227.
228.
Rectangle rect = GetTabRect(SelectedIndex);
229.
230.
rect = GetCloseBtnRect(rect);
231.
232.
Point pt =
new
Point(e.X, e.Y);
233.
234.
if
(rect.Contains(pt))
235.
{
236.
237.
CloseTab(SelectedTab);
238.
239.
}
240.
241.
}
242.
243.
}
244.
245.
public
void
CloseTab(
int
tabindex)
246.
{
247.
248.
CloseTab(TabPages[tabindex]);
249.
250.
}
251.
252.
public
void
CloseTab(TabPage tp)
253.
{
254.
255.
ClosingEventArgs args =
new
ClosingEventArgs(TabPages.IndexOf(tp));
256.
257.
OnTabClosing(args);
258.
259.
260.
261.
if
(!args.Cancel)
262.
{
263.
264.
265.
266.
TabPages.Remove(tp);
267.
268.
OnTabClosed(
new
ClosedEventArgs(tp));
269.
270.
tp.Dispose();
271.
272.
}
273.
274.
}
275.
276.
protected
void
OnTabClosed(ClosedEventArgs e)
277.
{
278.
279.
if
(TabClosed !=
null
)
280.
{
281.
282.
TabClosed(
this
, e);
283.
284.
}
285.
286.
}
287.
288.
protected
void
OnTabClosing(ClosingEventArgs e)
289.
{
290.
291.
if
(TabClosing !=
null
)
292.
293.
TabClosing(
this
, e);
294.
295.
}
296.
297.
}
298.
299.
300.
301.
public
class
ClosingEventArgs
302.
{
303.
304.
private
readonly
int
_nTabIndex = -1;
305.
306.
public
ClosingEventArgs(
int
nTabIndex)
307.
{
308.
309.
_nTabIndex = nTabIndex;
310.
311.
Cancel =
false
;
312.
313.
}
314.
315.
public
bool
Cancel {
get
;
set
; }
316.
317.
/// <summary>
318.
319.
/// Get/Set the tab index value where the close button is clicked
320.
321.
/// </summary>
322.
323.
public
int
TabIndex
324.
{
325.
326.
get
327.
{
328.
329.
return
_nTabIndex;
330.
331.
}
332.
333.
}
334.
335.
}
336.
337.
public
class
ClosedEventArgs : EventArgs
338.
{
339.
340.
private
readonly
TabPage _tab;
341.
342.
public
ClosedEventArgs(TabPage tab)
343.
{
344.
345.
_tab = tab;
346.
347.
}
348.
349.
/// <summary>
350.
351.
/// Get/Set the tab index value where the close button is clicked
352.
353.
/// </summary>
354.
355.
public
TabPage Tab
356.
{
357.
358.
get
359.
{
360.
361.
return
_tab;
362.
363.
}
364.
365.
}
366.
367.
}
368.
369.
#endregion
370.
}