TabPage的源代码

TabPage的源文件,需要改成MenuEx的样式。我没多少时间改这个

using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Web.UI;

namespace MyThread
{
 public delegate void OnTabChange(object sender, int oldIndex, int newIndex);

 /// <summary>
 /// Summary description for MyTabPage.
 /// </summary>
 public class MyTabPage : System.Windows.Forms.UserControl
 {
  private System.Windows.Forms.Panel pnlControl;
  private System.Windows.Forms.Panel pnlMemo;

  private Color _myCmdBackColor = SystemColors.ControlLight;
  private Color _myCmdActiveColor = SystemColors.Control;

  private ArrayList _MyPages = new ArrayList();
  private MyTabColumns _MyTabs = new MyTabColumns();
  private ImageList _myImages = new ImageList();
  private int selIndex = 0;  // default is First Page
  /// <summary>
  /// Required designer variable.
  /// </summary>
  private System.ComponentModel.Container components = null;

  public MyTabPage()
  {
   this.SetStyle(ControlStyles.DoubleBuffer, true);

   // This call is required by the Windows.Forms Form Designer.
   InitializeComponent();

   // TODO: Add any initialization after the InitializeComponent call

  }

  /// <summary>
  /// Clean up any resources being used.
  /// </summary>
  protected override void Dispose( bool disposing )
  {
   if( disposing )
   {
    if(components != null)
    {
     components.Dispose();
    }
   }
   base.Dispose( disposing );
  }

  #region Component Designer generated code
  /// <summary>
  /// Required method for Designer support - do not modify
  /// the contents of this method with the code editor.
  /// </summary>
  private void InitializeComponent()
  {
   this.pnlControl = new System.Windows.Forms.Panel();
   this.pnlMemo = new System.Windows.Forms.Panel();
   this.SuspendLayout();
   //
   // pnlControl
   //
   this.pnlControl.BackColor = System.Drawing.SystemColors.ControlLight;
   this.pnlControl.Dock = System.Windows.Forms.DockStyle.Bottom;
   this.pnlControl.Location = new System.Drawing.Point(0, 319);
   this.pnlControl.Name = "pnlControl";
   this.pnlControl.Size = new System.Drawing.Size(400, 25);
   this.pnlControl.TabIndex = 0;
   this.pnlControl.Resize += new System.EventHandler(this.pnlControl_Resize);
   this.pnlControl.Paint += new System.Windows.Forms.PaintEventHandler(this.pnlControl_Paint);
   this.pnlControl.MouseDown += new System.Windows.Forms.MouseEventHandler(this.pnlControl_MouseDown);
   //
   // pnlMemo
   //
   this.pnlMemo.Dock = System.Windows.Forms.DockStyle.Fill;
   this.pnlMemo.Location = new System.Drawing.Point(0, 0);
   this.pnlMemo.Name = "pnlMemo";
   this.pnlMemo.Size = new System.Drawing.Size(400, 319);
   this.pnlMemo.TabIndex = 1;
   //
   // MyTabPage
   //
   this.Controls.Add(this.pnlMemo);
   this.Controls.Add(this.pnlControl);
   this.Name = "MyTabPage";
   this.Size = new System.Drawing.Size(400, 344);
   this.Resize += new System.EventHandler(this.MyTabPage_Resize);
   this.ResumeLayout(false);

  }
  #endregion

  private void pnlControl_Paint(object sender, System.Windows.Forms.PaintEventArgs e)
  {
   if (!e.Graphics.IsClipEmpty)
   {
    Rectangle rect = this.pnlControl.ClientRectangle;
    e.Graphics.Clear(_myCmdBackColor);

    e.Graphics.DrawLine(Pens.Black,
     new Point(rect.X, rect.Y + 1),
     new Point(rect.Right, rect.Y + 1));
    Pen p2 = new Pen(SystemColors.Control);
    e.Graphics.DrawLine(p2,
     new Point(rect.X ,rect.Y),
     new Point(rect.Right, rect.Y));

    // draw button
    if (this._MyTabs.Count != 0)
    {
     for (int i=0, n=_MyTabs.Count; i<n; i++)
     {
      // get earch Tab
      MyTabColumn tab = (MyTabColumn)this._MyTabs[i];
      Rectangle myRect = this.MeasureRange(e.Graphics, i);
      if (i == selIndex)
       DrawTab1(e.Graphics, myRect, tab);
      else if (i == selIndex - 1)
       DrawTab3(e.Graphics, myRect, tab);
      else
       DrawTab2(e.Graphics, myRect, tab);
     }
    }
    // draw close Button
    DrawClose(e.Graphics);
   }
   
  }
  private Rectangle MeasureRange(Graphics g, int pos)
  {
   Rectangle myClientRect;
   Rectangle rect = this.pnlControl.ClientRectangle;
   int width = 5;
   // measure Client Rect
   for (int i = 0; i< pos; i++)
   {
    MyTabColumn col = (MyTabColumn)this._MyTabs[i];
    width += col.Width + 2;
   }
   // coculate size
   MyTabColumn myCol = (MyTabColumn)this._MyTabs[pos];
   myClientRect = new Rectangle(
    rect.X + width,
    rect.Y + 1,
    myCol.Width,
    20);

   return myClientRect;
  }
  private void DrawTab1(Graphics g, Rectangle colRect, MyTabColumn tab)
  {
   // draw tab
   Brush b = new SolidBrush(_myCmdActiveColor);
   g.FillRectangle(b, colRect);
   // draw border
   Pen p = new Pen(Color.White);
   Pen p2 = new Pen(Color.Black);
   g.DrawLine(p, colRect.Location,
    new Point(colRect.X, colRect.Bottom));
   g.DrawLine(p2, new Point(colRect.X + 1, colRect.Bottom),
    new Point(colRect.Right, colRect.Bottom));
   g.DrawLine(p2, new Point(colRect.Right, colRect.Bottom),
    new Point(colRect.Right, colRect.Y));

   // Draw Image
   if (this._myImages != null &&
    tab.Icon >= 0 &&
    tab.Icon < _myImages.Images.Count)
   {
    Image myIcon = _myImages.Images[tab.Icon];
    g.DrawImage(myIcon,
     new Point(colRect.X + 4, colRect.Y + 2));
   }

   // Draw Text
   Brush b2 = new SolidBrush(pnlControl.ForeColor);
   Font f = pnlControl.Font;
   int iconWidth = 0;
   if (this._myImages != null &&
    tab.Icon >= 0 &&
    tab.Icon < _myImages.Images.Count)
   {
    Image myIcon = _myImages.Images[tab.Icon];
    iconWidth += myIcon.Width;
   }
   Point posi = new Point(colRect.X + iconWidth + 6, colRect.Y + 4);
   g.DrawString(tab.Name, f, b2, posi);
  }
  private void DrawTab2(Graphics g, Rectangle colRect, MyTabColumn tab)
  {
   // draw tab
   Brush b = new SolidBrush(_myCmdBackColor);
   g.FillRectangle(b, colRect);
   // draw border
   Pen p = new Pen(Color.Gray);
   Pen p2 = new Pen(Color.Black);
   g.DrawLine(p2, new Point(colRect.X, colRect.Y),
    new Point(colRect.Right, colRect.Y));
   g.DrawLine(p, new Point(colRect.Right, colRect.Bottom - 4),
    new Point(colRect.Right, colRect.Y + 2));

   // Draw Image
   if (this._myImages != null &&
    tab.Icon >= 0 &&
    tab.Icon < _myImages.Images.Count)
   {
    Image myIcon = _myImages.Images[tab.Icon];
    g.DrawImage(myIcon,
     new Point(colRect.X + 4, colRect.Y + 2));
   }
   // Draw Text
   Brush b2 = new SolidBrush(Color.Gray);
   Font f = pnlControl.Font;
   int iconWidth = 0;
   if (this._myImages != null &&
    tab.Icon >= 0 &&
    tab.Icon < _myImages.Images.Count)
   {
    Image myIcon = _myImages.Images[tab.Icon];
    iconWidth += myIcon.Width;
   }
   Point posi = new Point(colRect.X + iconWidth + 6, colRect.Y + 4);
   g.DrawString(tab.Name, f, b2, posi);
  }
  private void DrawTab3(Graphics g, Rectangle colRect, MyTabColumn tab)
  {
   // draw tab
   Brush b = new SolidBrush(_myCmdBackColor);
   g.FillRectangle(b, colRect);
   // draw border
   //Pen p = new Pen(Color.Gray);
   Pen p2 = new Pen(Color.Black);
   g.DrawLine(p2, new Point(colRect.X, colRect.Y),
    new Point(colRect.Right, colRect.Y));
   //g.DrawLine(p, new Point(colRect.Right, colRect.Bottom - 4),
   // new Point(colRect.Right, colRect.Y + 2));

   // Draw Image
   if (this._myImages != null &&
    tab.Icon >= 0 &&
    tab.Icon < _myImages.Images.Count)
   {
    Image myIcon = _myImages.Images[tab.Icon];
    g.DrawImage(myIcon,
     new Point(colRect.X + 4, colRect.Y + 2));
   }
   // Draw Text
   Brush b2 = new SolidBrush(Color.Gray);
   Font f = pnlControl.Font;
   int iconWidth = 0;
   if (this._myImages != null &&
    tab.Icon >= 0 &&
    tab.Icon < _myImages.Images.Count)
   {
    Image myIcon = _myImages.Images[tab.Icon];
    iconWidth += myIcon.Width;
   }
   Point posi = new Point(colRect.X + iconWidth + 6, colRect.Y + 4);
   g.DrawString(tab.Name, f, b2, posi);
  }
  private void DrawClose(Graphics g)
  {
   // draw border
   Rectangle rect = pnlControl.ClientRectangle;
   Rectangle closeRect = new Rectangle(
    rect.Right - 16, 9,
    6, 6);
   
   //closeRect.Inflate(1,1);
   //g.DrawRectangle(Pens.Black, closeRect);
   //closeRect.Inflate(-2, -2);
   g.DrawLine(new Pen(Color.Gray, 1),
    new Point(closeRect.X, closeRect.Y),
    new Point(closeRect.Right, closeRect.Bottom));
   g.DrawLine(new Pen(Color.Gray, 1),
    new Point(closeRect.Right, closeRect.Y),
    new Point(closeRect.X, closeRect.Bottom));
  }

  /// <summary>
  ///  switch Tabs
  /// </summary>
  /// <param name="pos"></param>
  private void switchIndex(int pos)
  {
   if (_MyPages.Count > 0 && pos < _MyPages.Count)
   {
    for (int i=0, n=_MyPages.Count; i<n; i++)
    {
     System.Windows.Forms.Control ctl =
      (System.Windows.Forms.Control)_MyPages[i];
     ctl.Visible = false;
    }

    // switch pos
    System.Windows.Forms.Control ctl2 =
     (System.Windows.Forms.Control)_MyPages[pos];
    ctl2.Location = pnlMemo.Location;
    ctl2.Size = pnlMemo.Size;
    ctl2.Dock = DockStyle.Fill;
    ctl2.Visible = true;
    this.pnlMemo.Controls.Add(ctl2);
   }
  }

  private void pnlControl_Resize(object sender, System.EventArgs e)
  {
   //this.Invalidate();
  }

  private void MyTabPage_Resize(object sender, System.EventArgs e)
  {
   this.Invalidate(true);
  }

  private void pnlControl_MouseDown(object sender, System.Windows.Forms.MouseEventArgs e)
  {
   this.Cursor = Cursors.Default;
   
   Graphics g = this.pnlControl.CreateGraphics();
   //Rectangle rect = this.pnlControl.ClientRectangle;

   for (int i=0, n=this._MyTabs.Count; i<n; i++)
   {
    Rectangle subRect = this.MeasureRange(g, i);
    if (subRect.Contains(e.X, e.Y))
    {
     // clicked this tab
     this.OnTabChanged(sender, this.selIndex, i);
     this.selIndex = i;
     this.switchIndex(this.selIndex);
     this.pnlControl.Invalidate(true);
    }
   }
  }

  #region Public Method
  public event OnTabChange OnTabChanged;
  #endregion
  
  #region Public Property
  [Category("Data")]
  [PersistenceMode(PersistenceMode.InnerProperty),
  DesignerSerializationVisibility(DesignerSerializationVisibility.Content) ]
  public MyTabColumns MyTabs
  {
   get {return this._MyTabs; }
   set {this._MyTabs = value; }
  }
  [Browsable(false)]
  public ArrayList MyPages
  {
   get {return this._MyPages; }
   set
   {
    this._MyPages = value;
    switchIndex(0);
   }
  }
  [Category("Data")]
  public ImageList MyImages
  {
   get {return this._myImages; }
   set {this._myImages = value; }
  }
  [Category("Appearance")]
  public Color MyCmdBackColor
  {
   get {return this._myCmdBackColor; }
   set
   {
    this._myCmdBackColor = value;
    this.Invalidate(true); }
  }
  [Category("Appearance")]
  public Color MyCmdActiveColor
  {
   get {return this._myCmdActiveColor; }
   set
   {
    this._myCmdActiveColor = value;
    this.Invalidate(true); }
  }
  [Browsable(false)]
  public int SelectIndex
  {
   get {return this.selIndex; }
  }
  #endregion
 }

 /// <summary>
 ///  Tabs Data Module
 ///   . Name
 ///   . Width
 ///   . ICON
 /// </summary>
 public class MyTabColumn : IStateManager
 {
  private string _name;
  private int _width;
  private int _icon;

  public MyTabColumn()
  {
  }

  public MyTabColumn(string name, int width, int icon)
  {
   this._name = name;
   this._width = width;
   this._icon = icon;
  }

  public string Name
  {
   get {return this._name; }
   set {this._name = value; }
  }

  public int Width
  {
   get {return this._width; }
   set {this._width = value; }
  }

  public int Icon
  {
   get {return this._icon; }
   set {this._icon = value; }
  }


  #region IStateManager member

  void IStateManager.TrackViewState()
  {
   //base.TrackViewState();
  }

  bool IStateManager.IsTrackingViewState
  {
   get
   {
    return true;
    //return base.IsTrackingViewState;
   }
  }

  object IStateManager.SaveViewState()
  {
   return this;
   //return base.SaveViewState();
  }

  void IStateManager.LoadViewState(object state)
  {
   //base.LoadViewState(state);
  }

  #endregion
 }

 /// <summary>
 ///  My Tab Collection
 /// </summary>
 public class MyTabColumns : CollectionBase, IStateManager
 {
  private bool marked;

  public MyTabColumns() : base()
  {
   //
   // TODO:
   //
  }

  private void Initialize()
  {
   marked = false;
  }

  public MyTabColumn this[int index]
  {
   get
   {
    return (MyTabColumn)base.List[index];
   }
   set
   {
    List[index] = value;
   }
  }

  public void Add(MyTabColumn aItem)
  {
   base.List.Add(aItem);
  }

  public void Remove(int index)
  {
   if(index < base.Count - 1 && index > 0 )
   {
    base.List.RemoveAt(index);
   }
  }
  #region IStateManager member

  void IStateManager.TrackViewState()
  {
   for(int i = 0 ; i < base.List.Count; i ++)
   {
    ((IStateManager)base.List[i]).TrackViewState();
   }
  }

  bool IStateManager.IsTrackingViewState
  {
   get
   {
    return marked;
   }
  }

  object IStateManager.SaveViewState()
  {
   object[] iState = new object[base.List.Count];
   for(int i = 0 ; i < base.List.Count; i ++)
   {
    iState[i] = ((IStateManager)base.List[i]).SaveViewState();
   }
   return iState;
  }

  void IStateManager.LoadViewState(object state)
  {
   if(state != null)
   {
    object[] viewState = (object[])state;
    for(int i = 0 ; i < viewState.Length ; i ++)
    {
     ((IStateManager)List[i]).LoadViewState(viewState[i]);
    }
   }
  }
  #endregion
 }
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值