日期:2014-05-20 浏览次数:21047 次
/// <summary> /// 重写标签控件 /// </summary> public class TabControlFx : TabControl { #region 属性 private int Round = 8; // 圆角弧度 #region 控件边框颜色 private Color colorBorder = Color.FromArgb(78, 160, 209); /// <summary> /// 控件边框颜色 /// </summary> [Category("颜色设置")] [Description("控件边框颜色")] public Color ColorBorder { get { return this.colorBorder; } set { this.colorBorder = value; this.penBorder.Dispose(); this.penBorder = new Pen(colorBorder); } } private Pen penBorder; #endregion #region 控件背景色 private Color colorControlBack = Color.White; /// <summary> /// 控件背景色 /// </summary> [Category("颜色设置")] [Description("控件背景色")] public Color ControlBack { get { return this.colorControlBack; } set { this.colorControlBack = value; this.brushControlBack.Dispose(); this.brushControlBack = new SolidBrush(colorControlBack); } } private Brush brushControlBack; #endregion #region 标签未被选中时背景色 private Color colorTabNormal = Color.LightGray; /// <summary> /// 标签未被选中时背景色 /// </summary> [Category("颜色设置")] [Description("标签未被选中时背景色")] public Color ColorTabNormal { get { return this.colorTabNormal; } set { this.colorTabNormal = value; this.brushTabNormal.Dispose(); this.brushTabNormal = new SolidBrush(colorTabNormal); } } private Brush brushTabNormal; #endregion #region 标签被选中时背景色 private Color colorTabActive = Color.White; /// <summary> /// 标签被选中时背景色 /// </summary> [Category("颜色设置")] [Description("标签被选中时背景色")] public Color ColorTabActive { get { return this.colorTabActive; } set { this.colorTabActive = value; this.brushTabActive.Dispose(); this.brushTabActive = new SolidBrush(colorTabActive); } } private Brush brushTabActive; #endregion #region 标签集合 /// <summary> /// 标签集合 /// </summary> [Editor(typeof(TabpageExCollectionEditor), typeof(UITypeEditor))] public new TabPageCollection TabPages { get { return base.TabPages; } } #endregion #region 标签停靠位置 /// <summary> /// 标签停靠位置 现在只有 Top 和Bottom两种 /// </summary> public new TabAlignment Alignment { get { return base.Alignment; } set { if (value != TabAlignment.Top) throw new Exception("只支持标签在顶部显示"); base.Alignment = value; } } #endregion #region 设置外观模式 /// <summary> /// 设置外观模式只能是正常模式 /// </summary> public new TabAppearance Appearance { get { return base.Appearance; } set { base.Appearance = TabAppearance.Normal; } } #endregion #endregion public TabControlFx() { this.brushControlBack = new SolidBrush(colorControlBack); this.penBorder = new Pen(colorBorder); this.brushTabNormal = new SolidBrush(colorTabNormal); this.brushTabActive = new SolidBrush(colorTabActive); // 由用户绘制控件 this.SetStyle(ControlStyles.UserPaint, true); // 防止闪烁 this.SetStyle(ControlStyles.AllPaintingInWmPaint, true); this.SetStyle(ControlStyles.OptimizedDoubleBuffer, true); // 启动缓冲 this.SetStyle(ControlStyles.DoubleBuffer, true); // 调整空间大小时重绘 this.SetStyle(ControlStyles.ResizeRedraw, true); } /// <summary> /// 重绘标签控件 /// </summary> /// <param name="e"></param> protected override void OnPaint(PaintEventArgs e) { base.OnPaint(e); if (!this.Visible) { return; } // 控件区域 Rectangle ControlArea = this.ClientRectangle; // 标签页区域 Rectangle TabsArea = this.DisplayRectangle; // 绘制控件背景 e.Graphics.FillRectangle(brushControlBack, ControlArea); // 绘制控件边框 int nDelta = SystemInformation.Border3DSize.Width; // 扩大标签范围 排除3D边框造成的缝隙 TabsArea.Inflate(nDelta, nDelta); // 绘制控件边框 e.Graphics.DrawRectangle(penBorder, TabsArea); // 绘制标签 Rectangle rectangle = new Rectangle(TabsArea.Left, ControlArea.Top, TabsArea.Width, ControlArea.Height); e.Graphics.SetClip(rectangle); for (int i = 0; i < this.TabCount; i++) { DrawTab(e.Graphics, this.TabPages[i], i); } } /// <summary> /// 绘制标签 /// </summary> /// <param name="g"></param> /// <param name="tabPage"></param> /// <param name="nIndex"></param> internal void DrawTab(Graphics g, TabPage tabPage, int nIndex) { Rectangle recBounds = this.GetTabRect(nIndex); RectangleF tabTextArea = (RectangleF)this.GetTabRect(nIndex); using (GraphicsPath FormTopAreaPath = new GraphicsPath()) { if (this.Alignment == TabAlignment.Top) { // 绘制窗体上部两个圆角 FormTopAreaPath.AddArc(recBounds.Left, recBounds.Top, Round, Round, 180, 90); FormTopAreaPath.AddArc(recBounds.Right - Round, recBounds.Top, Round, Round, 270, 90); FormTopAreaPath.AddLine(recBounds.Right, recBounds.Top + Round, recBounds.Right, recBounds.Bottom - Round); FormTopAreaPath.AddLine(recBounds.Right, recBounds.Bottom, recBounds.Left, recBounds.Bottom); FormTopAreaPath.AddLine(recBounds.Left, recBounds.Top + Round, recBounds.Left, recBounds.Bottom - Round); FormTopAreaPath.CloseAllFigures(); // 绘制标签背景色 if (this.SelectedIndex == nIndex) { using (Brush brush = new SolidBrush(Color.White)) { g.FillPath(brush, FormTopAreaPath); } } else { // 填充背景图 using (TextureBrush textureBrush = new TextureBrush(Resources.TabWhite)) { textureBrush.TranslateTransform(4, 0); g.FillPath(textureBrush, FormTopAreaPath); } } // 绘制标签边框 g.DrawPath(penBorder, FormTopAreaPath); } // 是选中标签和标签页联通 if (this.SelectedIndex == nIndex) { switch (this.Alignment) { case TabAlignment.Top: g.FillRectangle(brushTabActive, recBounds.Left + 1, recBounds.Bottom, recBounds.Width - 1, 2); break; case TabAlignment.Bottom: g.FillRectangle(brushTabActive, recBounds.Left + 1, recBounds.Top - 2, recBounds.Width - 1, 3); break; } } // 重绘字体 StringFormat stringFormat = new StringFormat(); stringFormat.Alignment = StringAlignment.Center; stringFormat.LineAlignment = StringAlignment.Center; g.DrawString(tabPage.Text, this.Font, new SolidBrush(tabPage.ForeColor), tabTextArea, stringFormat); } } #region 提供用户编辑界面 internal class TabpageExCollectionEditor : CollectionEditor { public TabpageExCollectionEditor(System.Type type) : base(type) { } protected override Type CreateCollectionItemType() { return typeof(TabPage); } } #endregion