Index: DraggableTabControl.cs
===================================================================
--- DraggableTabControl.cs (revision 24)
+++ DraggableTabControl.cs (working copy)
@@ -12,20 +12,40 @@
///
public class DraggableTabControl : System.Windows.Forms.TabControl
{
- ///
+ ///
/// Required designer variable.
- ///
+ ///
private System.ComponentModel.Container components = null;
+ public event CancelEventHandler TabClosingEventHandler;
+ private TabPage closingTab;
+ public TabPage ClosingTab
+ {
+ get { return closingTab; }
+ }
+ protected void OnTabClosing()
+ {
+ CancelEventArgs e = new CancelEventArgs();
+ if(TabClosingEventHandler != null)
+ try {
+ TabClosingEventHandler.BeginInvoke(this, e, null, null);
+ } catch(Exception) {
+ throw;
+ }
+ if(!e.Cancel)
+ CloseTab();
+ }
- public DraggableTabControl()
+ public DraggableTabControl() : base()
{
// This call is required by the Windows.Forms Form Designer.
InitializeComponent();
+ DrawMode = TabDrawMode.OwnerDrawFixed;
+ Padding = new Point(17,3);
}
- ///
+ ///
/// Clean up any resources being used.
- ///
+ ///
protected override void Dispose(bool disposing)
{
if(disposing)
@@ -39,10 +59,10 @@
}
#region Component Designer generated code
- ///
+ ///
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
- ///
+ ///
private void InitializeComponent()
{
}
@@ -104,7 +124,6 @@
e.Effect = DragDropEffects.None;
}
}
-
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown(e);
@@ -117,12 +136,37 @@
DoDragDrop(tp, DragDropEffects.All);
}
}
+ protected override void OnDrawItem(DrawItemEventArgs e)
+ {
+ e.Graphics.DrawLine(new Pen(Brushes.Black, 2f), new PointF(e.Bounds.Right-15, e.Bounds.Y-4), new PointF(e.Bounds.Right-15, e.Bounds.Bottom-4));
+ e.Graphics.DrawLine(new Pen(Brushes.Black, 2f), new PointF(e.Bounds.Right-15, e.Bounds.Bottom-4), new PointF(e.Bounds.Right-15, e.Bounds.Y-4));
+ e.Graphics.DrawString(TabPages[e.Index].Text, Font, Brushes.Black, new PointF(e.Bounds.X+4, e.Bounds.Y+2));
+ base.OnDrawItem(e);
+ }
+ protected override void OnMouseClick(MouseEventArgs e)
+ {
+ for(int i = 0; i < TabPages.Count; i++)
+ {
+ Rectangle r = GetTabRect(i);
+ if(r.Contains(e.Location))
+ {
+ closingTab = TabPages[i];
+ OnTabClosing();
+ closingTab = null;
+ }
+ }
+ base.OnMouseClick(e);
+ }
- ///
+ private void CloseTab()
+ {
+ TabPages.Remove(ClosingTab);
+ }
+ ///
/// Finds the TabPage whose tab is contains the given point.
- ///
- /// The point (given in client coordinates) to look for a TabPage.
- /// The TabPage whose tab is at the given point (null if there isn't one).
+ ///
+ /// The point (given in client coordinates) to look for a TabPage.
+ /// The TabPage whose tab is at the given point (null if there isn't one).
private TabPage GetTabPageByTab(Point pt)
{
TabPage tp =
@@ -142,11 +186,11 @@
return tp;
}
- ///
+ ///
/// Loops over all the TabPages to find the index of the given TabPage.
- ///
- /// The TabPage we want the index for.
- /// The index of the given TabPage(-1 if it isn't found.)
+ ///
+ /// The TabPage we want the index for.
+ /// The index of the given TabPage(-1 if it isn't found.)
private int FindIndex(TabPage page)
{
for(int i = 0; i < TabPages.Count; i++)