📄 gtabcontrol.cs
字号:
using System;
using System.Collections;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Windows.Forms;
using System.Runtime.InteropServices;using System.Drawing.Drawing2D;
namespace gowk.controls
{
public class GTabControl :GPanel
{GTabPageCollection _GTabPages;
Size _ItemSize=new Size(32,32);
GAlignment _Alignment=GAlignment.Left;
int _SelectedIndex=0;
GPanel _MainPanle;
GImage _ItemBackGround;
public GImage ItemBackGround
{
get{return this._ItemBackGround;}
set{this._ItemBackGround=value;}
}
public event System.EventHandler SelectedIndexChanged;
public GTabControl()
{
this.SetStyle(ControlStyles.DoubleBuffer|ControlStyles.UserPaint|ControlStyles.AllPaintingInWmPaint,true);
this._GTabPages=new GTabPageCollection(this);
this._MainPanle=new GPanel();
this._ItemBackGround=new GImage();
this._MainPanle.IsRound=false;
this._MainPanle.BorderColor.NormalColor=Color.Transparent;
this._MainPanle.BorderColor.ActiveColor=Color.Transparent;
this._MainPanle.BorderColor.PressedColor=Color.Transparent;
this._MainPanle.BackColor=Color.Transparent;
this.Controls.Add(this._MainPanle);
this.SettingAll();
this.IsRound=true;
}
protected override void OnSizeChanged(EventArgs e)
{
base.OnSizeChanged (e);
this.SettingAll();
}
public void SettingAll()
{
this._MainPanle.SuspendLayout();
switch(this._Alignment)
{
case GAlignment.Top:
this._MainPanle.Location=new Point(0,this._ItemSize.Height);
this._MainPanle.Width=this.Width;
this._MainPanle.Height=this.Height-this._ItemSize.Height;
break;
case GAlignment.Right:
this._MainPanle.Location=new Point(0,0);
this._MainPanle.Width=this.Width-this._ItemSize.Width;
this._MainPanle.Height=this.Height;
break;
case GAlignment.Bottom:
this._MainPanle.Location=new Point(0,0);
this._MainPanle.Width=this.Width;
this._MainPanle.Height=this.Height-this._ItemSize.Height;
break;
default:
this._MainPanle.Location=new Point(this._ItemSize.Width,0);
this._MainPanle.Width=this.Width-this._ItemSize.Width;
this._MainPanle.Height=this.Height;
break;
}
this._MainPanle.ResumeLayout();
}
// [System.ComponentModel.DesignerSerializationVisibility(System.ComponentModel.DesignerSerializationVisibility.Hidden),
// System.ComponentModel.Editor(typeof(GTabPageColllectionEditor),typeof(System.Drawing.Design.UITypeEditor))]
public GTabPageCollection GTabPages
{
get{return this._GTabPages;}
}
public Size ItemSize
{
get{return this._ItemSize;}set{this._ItemSize=value;this.SettingAll();}
}
public GAlignment Alignment
{
get{return this._Alignment;}
set
{
if(this._Alignment!=value)
{
this._Alignment=value;
this.SettingAll();
this.Invalidate();
}
}
}
public Rectangle GetTabRect(int index)
{
Rectangle rect=new Rectangle(0,0,this._ItemSize.Width,this._ItemSize.Height);
switch(this._Alignment)
{
case GAlignment.Top:
rect.X=this.ItemSize.Width*index;
break;
case GAlignment.Right:
rect.Y=this.ItemSize.Height*index;
rect.X=this.Width-this._ItemSize.Width;
break;
case GAlignment.Bottom:
rect.X=this.ItemSize.Width*index;
rect.Y=this.Height-this._ItemSize.Height;
break;
default:
rect.Y=this.ItemSize.Height*index;
break;
}
return rect;
}
private void OnDrawItem(Graphics g,int index)
{
GTabPage page=(GTabPage)this.GTabPages[index];
Rectangle rect=this.GetTabRect(index);
State state=State.Normal;
Image img=null;
Image bimg=null;
//get the state
if(this.SelectedIndex==index)
{
// state==State.Selected;
state=State.Pressed;
}
else if(rect.Contains(this.PointToClient(Control.MousePosition)))
{
state=this.Capture?State.Pressed:State.Actived;
}
//get the image and draw it
img=page.GImage.GetImageByState(state);
bimg=this._ItemBackGround.GetImageByState(state);
if(bimg!=null)
{
g.DrawImage(bimg,rect);
}
if(img!=null)
{
g.DrawImage(img,rect.X+5,rect.Y+5);
}
else
{
StringFormat sf=new StringFormat();
sf.Trimming=StringTrimming.EllipsisCharacter;
sf.LineAlignment=StringAlignment.Far;
sf.Alignment=StringAlignment.Center;
g.DrawString(page.Text,this.Font,new SolidBrush(this.ForeColor),rect,sf);
}
}
public int SelectedIndex
{
get{return this._SelectedIndex;}
set
{
if((this._SelectedIndex!=value) && (value>-1) && (value<this.GTabPages.Count))
{
this.GTabPages[this._SelectedIndex].Hide();
this.GTabPages[value].Show();
this._SelectedIndex=value;
if(this.SelectedIndexChanged!=null)this.SelectedIndexChanged(this,new EventArgs());
// this.Invalidate();
}
}
}
protected override void OnPaint(PaintEventArgs e)
{
// System.Diagnostics.Trace.WriteLine("onpaint");
Graphics g=e.Graphics;
for(int i=0;i<this.GTabPages.Count;i++)
{
this.OnDrawItem(g,i);
// g.DrawRectangle(new Pen(Color.White),this.GetTabRect(i));
}
base.OnPaint (e);
}
protected override void OnMouseMove(MouseEventArgs e)
{
base.OnMouseMove (e);
this.Invalidate();
}
protected override void OnClick(EventArgs e)
{
base.OnClick (e);
for(int i=0;i<this.GTabPages.Count;i++)
{
Rectangle rect=this.GetTabRect(i);
Point p=this.PointToClient(Control.MousePosition);
if(rect.Contains(p))
{
this.SelectedIndex=i;
this.Invalidate();
break;
}
}
this.Invalidate();
}
protected override void OnMouseEnter(EventArgs e)
{
base.OnMouseEnter (e);
this.Invalidate();
}
protected override void OnMouseDown(MouseEventArgs e)
{
base.OnMouseDown (e);
this.Invalidate();
}
protected override void OnMouseUp(MouseEventArgs e)
{
base.OnMouseUp (e);
this.Invalidate();
}
protected override void OnMouseLeave(EventArgs e)
{
base.OnMouseLeave (e);
this.Invalidate();
}
public class GTabPageCollection:System.Collections.ICollection,System.Collections.IList,System.Collections.IEnumerable
{
GTabControl c;
public GTabPageCollection(GTabControl owner)
{
this.c=owner;
}
#region ICollection 成员
public bool IsSynchronized
{
get
{
return false;
}
}
public int Count
{
get
{
return this.c._MainPanle.Controls.Count;
}
}
public void CopyTo(Array array, int index)
{
this.c._MainPanle.Controls.CopyTo(array,index);
}
public object SyncRoot
{
get
{return this.c._MainPanle.Controls;
}
}
#endregion
#region IEnumerable 成员
public IEnumerator GetEnumerator()
{
try
{
return this.c._MainPanle.Controls.GetEnumerator();
}
catch(System.Exception ex)
{
MessageBox.Show(ex.StackTrace);
return null;
}
}
#endregion
#region IList 成员
public bool IsReadOnly
{
get
{
// TODO: 添加 GTabPageCollection.IsReadOnly getter 实现
return false;
}
}
object System.Collections.IList.this[int index]
{
get
{
// TODO: 添加 GTabPageCollection.this getter 实现
return this.c._MainPanle.Controls[index];
}
set
{
// TODO: 添加 GTabPageCollection.this setter 实现
}
}
public void RemoveAt(int index)
{
this.c._MainPanle.Controls.RemoveAt(index);
}
public void Insert(int index, object value)
{
}
public void Remove(object value)
{
this.c._MainPanle.Controls.Remove((GTabControl)value);
}
public bool Contains(object value)
{
// TODO: 添加 GTabPageCollection.Contains 实现
return this.IndexOf(value)!=-1;
}
public void Clear()
{
this.c._MainPanle.Controls.Clear();
}
public int IndexOf(object value)
{
// TODO: 添加 GTabPageCollection.IndexOf 实现
return this.c._MainPanle.Controls.IndexOf((GTabPage)value);
}
public int Add(object value)
{
return this.Add((gowk.controls.GTabPage)value);
}
public bool IsFixedSize
{
get
{
// TODO: 添加 GTabPageCollection.IsFixedSize getter 实现
return false;
}
}
#endregion
#region new member
public GTabPage this[int index]
{
get
{
// TODO: 添加 GTabPageCollection.this getter 实现
return (GTabPage)this.c._MainPanle.Controls[index];
}
set
{
// TODO: 添加 GTabPageCollection.this setter 实现
}
}
public void Insert(int index, GTabPage value)
{
}
public void Remove(GTabPage value)
{
this.c._MainPanle.Controls.Remove(value);
}
public bool Contains(GTabPage value)
{
// TODO: 添加 GTabPageCollection.Contains 实现
return this.IndexOf(value)!=-1;
}
public int IndexOf(GTabPage value)
{
// TODO: 添加 GTabPageCollection.IndexOf 实现
return this.c._MainPanle.Controls.IndexOf(value);
}
public int Add(GTabPage value)
{
if(this.Count!=0)
value.Hide();
this.c._MainPanle.Controls.Add(value);
return this.c._MainPanle.Controls.Count-1;
}
public void AddRange(GTabPage[] pages)
{
if (pages == null)
{
throw new ArgumentNullException("pages");
}
GTabPage[] pageArray1 = pages;
for (int num1 = 0; num1 < pageArray1.Length; num1++)
{
GTabPage page1 = pageArray1[num1];
this.Add(page1);
}
}
#endregion
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -