-
Notifications
You must be signed in to change notification settings - Fork 4
/
MdiTab.cs
228 lines (191 loc) · 5.76 KB
/
MdiTab.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
namespace Menees.Windows.Forms
{
#region Using Directives
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
#endregion
/// <summary>
/// A single tab in an <see cref="MdiTabStrip"/>.
/// </summary>
public sealed class MdiTab : ToolStripButton
{
#region Private Data Members
private const int CloseImagePixelSpace = 10;
private const int CloseButtonSpace = 14;
#endregion
#region Constructors
internal MdiTab(Form form)
: base(form.Text)
{
this.ImageScaling = ToolStripItemImageScaling.None;
this.ImageAlign = ContentAlignment.MiddleLeft;
this.TextAlign = ContentAlignment.MiddleLeft;
this.CheckOnClick = true;
this.AutoToolTip = false;
this.AssociatedForm = form;
this.Padding = new Padding(0, 0, CloseButtonSpace, 0);
}
#endregion
#region Internal Events
internal event EventHandler? CloseClicked;
#endregion
#region Public Properties
/// <summary>
/// Gets the form associated with the current tab.
/// </summary>
public Form? AssociatedForm
{
get;
internal set;
}
#endregion
#region Protected Methods
/// <summary>
/// Overrides <see cref="ToolStripButton.OnPaint"/>.
/// </summary>
protected override void OnPaint(PaintEventArgs e)
{
base.OnPaint(e);
// Draw the Close "button" if necessary.
Rectangle closeRect = this.GetCloseButtonRect();
bool clipIncludesCloseRect = e.ClipRectangle.IntersectsWith(closeRect);
if (clipIncludesCloseRect && (this.Selected || this.Checked))
{
Graphics graph = e.Graphics;
Point mousePositionInStrip = this.Owner.PointToClient(Cursor.Position);
Rectangle bounds = this.Bounds;
Point mousePosition = new(mousePositionInStrip.X - bounds.X, mousePositionInStrip.Y - bounds.Y);
Bitmap closeImage = Properties.Resources.CloseGray;
bool mouseOnClose = closeRect.Contains(mousePosition);
if (mouseOnClose)
{
closeImage = Properties.Resources.CloseBlack;
this.GetCloseButtonColors(out Color closeBackColor, out Color closeBorderColor);
using (Brush backBrush = new SolidBrush(closeBackColor))
{
graph.FillRectangle(backBrush, closeRect);
}
using (Pen borderPen = new(closeBorderColor))
{
graph.DrawRectangle(borderPen, closeRect.X, closeRect.Y, closeRect.Width - 1, closeRect.Height - 1);
}
}
const int Offset = (CloseButtonSpace - CloseImagePixelSpace) / 2;
Point pt = closeRect.Location;
pt.Offset(Offset, Offset);
graph.DrawImage(closeImage, pt);
}
}
/// <summary>
/// Overrides <see cref="ToolStripItem.OnMouseDown"/>.
/// </summary>
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
if (e.Button == MouseButtons.Left)
{
bool clickedClose = false;
if (this.CloseClicked != null)
{
Rectangle closeRect = this.GetCloseButtonRect();
if (closeRect.Contains(e.Location))
{
this.CloseClicked(this, EventArgs.Empty);
clickedClose = true;
}
}
if (!clickedClose)
{
if (this.Owner is MdiTabStrip owner && owner.AllowDrop && !this.IsOnOverflow)
{
// Perform the click to activate the tab. Otherwise, the click
// is lost when we capture the mouse to begin the drag.
this.PerformClick();
// This will begin the drag after a very short delay.
owner.BeginDragTimer(this);
}
}
}
else if (e.Button.HasFlag(MouseButtons.Right) || e.Button.HasFlag(MouseButtons.Middle))
{
// Select the tab when the right or middle mouse buttons have been clicked on it.
// Visual Studio makes a middle-click close the tab, but I don't like that behavior.
this.PerformClick();
}
}
/// <summary>
/// Overrides <see cref="ToolStripItem.OnMouseUp"/>.
/// </summary>
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp(e);
if (this.Owner is MdiTabStrip owner)
{
owner.EndDragTimer(false);
}
}
/// <summary>
/// Overrides <see cref="ToolStripItem.OnMouseEnter"/>.
/// </summary>
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter(e);
Rectangle closeRect = this.GetCloseButtonRect();
this.Invalidate(closeRect);
}
/// <summary>
/// Overrides <see cref="ToolStripItem.OnMouseMove"/>.
/// </summary>
protected override void OnMouseMove(MouseEventArgs mea)
{
base.OnMouseMove(mea);
Rectangle closeRect = this.GetCloseButtonRect();
this.Invalidate(closeRect);
}
/// <summary>
/// Overrides <see cref="ToolStripItem.OnMouseLeave"/>.
/// </summary>
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave(e);
Rectangle closeRect = this.GetCloseButtonRect();
this.Invalidate(closeRect);
}
#endregion
#region Private Methods
private Rectangle GetCloseButtonRect()
{
Rectangle bounds = this.Bounds;
int x = bounds.Width - CloseButtonSpace - 2;
int y = (bounds.Height - CloseButtonSpace) / 2;
Rectangle result = new(x, y, CloseButtonSpace, CloseButtonSpace);
return result;
}
private void GetCloseButtonColors(out Color closeBackColor, out Color closeBorderColor)
{
// Set the default colors.
closeBackColor = MdiTabStripColorTable.SelectedTab;
closeBorderColor = MdiTabStripColorTable.SelectedTabBorder;
// Try to use the colors from the renderer's color table (if we can find one).
ToolStrip tabStrip = this.Owner;
if (tabStrip != null)
{
if (tabStrip.Renderer is ToolStripProfessionalRenderer renderer)
{
ProfessionalColorTable colorTable = renderer.ColorTable;
if (colorTable != null)
{
closeBackColor = colorTable.ButtonCheckedGradientBegin;
closeBorderColor = colorTable.ButtonSelectedBorder;
}
}
}
}
#endregion
}
}