TabControl美化扩展----------WinForm控件开发系列
该控件是继承于 TabControl 基类开发的。主要重写 OnPaint 进行美化、添加图标、添加关闭按钮。
Tab 的左右切换按钮没有重写。
1 protected override void OnPaint(PaintEventArgs e) 2 { 3 base.OnPaint(e); 4 5 Graphics g = e.Graphics; 6 e.Graphics.SmoothingMode = SmoothingMode.AntiAlias; 7 8 #region 绘制所有Tab选项 9 10 SolidBrush back_normal_sb = new SolidBrush(this.TabBackNormalColor); 11 SolidBrush back_selected_sb = new SolidBrush(this.TabBackSelectedColor); 12 SolidBrush text_normal_sb = new SolidBrush(this.TabTextNormalColor); 13 SolidBrush text_selected_sb = new SolidBrush(this.TabTextSelectedColor); 14 StringFormat text_sf = new StringFormat() { Alignment = this.TabTextAlignment, LineAlignment = StringAlignment.Center, Trimming = StringTrimming.EllipsisCharacter }; 15 Pen close_pen = new Pen(this.TabCloseBackColor, 2) { StartCap = LineCap.Round, EndCap = LineCap.Round }; 16 17 Rectangle tab_rect = this.GetTabRectangle(); 18 Region client_region = null; 19 Region tabitem_region = null; 20 if (this.Alignment == TabAlignment.Top || this.Alignment == TabAlignment.Bottom) 21 { 22 client_region = g.Clip.Clone(); 23 tabitem_region = new Region(tab_rect); 24 g.Clip = tabitem_region; 25 26 } 27 for (int i = 0; i < this.TabCount; i++) 28 { 29 Rectangle rect = this.GetTabRect(i); 30 GraphicsPath path = ControlCommom.TransformCircular(rect, this.TabRadiusLeftTop, this.TabRadiusRightTop, this.TabRadiusRightBottom, this.TabRadiusLeftBottom); 31 32 #region 绘制Tab选项背景颜色 33 g.FillPath((i == this.SelectedIndex) ? back_selected_sb : back_normal_sb, path); 34 #endregion 35 36 #region 绘制Tab选项图片 37 if (this.ImageList != null && this.ImageList.Images.Count > 0) 38 { 39 Image img = null; 40 if (this.TabPages[i].ImageIndex > -1) 41 { 42 img = this.ImageList.Images[this.TabPages[i].ImageIndex]; 43 } 44 else if (this.TabPages[i].ImageKey.Trim().Length > 0) 45 { 46 img = this.ImageList.Images[this.TabPages[i].ImageKey]; 47 } 48 if (img != null) 49 { 50 g.DrawImage(img, rect.X + this.tabImageMarginLeft, rect.Y + (rect.Height - this.TabImageSize.Height) / 2, this.TabImageSize.Width, this.TabImageSize.Height); 51 } 52 } 53 #endregion 54 55 #region 绘制Tab选项文本 56 if (this.ImageList != null && ((this.TabPages[i].ImageIndex > -1) || (this.TabPages[i].ImageKey.Trim().Length > 0))) 57 { 58 rect = new Rectangle(rect.Left + this.TabImageSize.Width + this.tabImageMarginLeft * 2, rect.Top, rect.Width - this.TabImageSize.Width - this.tabImageMarginLeft * 2, rect.Height); 59 } 60 61 if (this.TextVertical) 62 { 63 string text = this.TabPages[i].Text; 64 float sum = 0; 65 SizeF text_size = g.MeasureString(text, this.Font, new PointF(), text_sf); 66 for (int j = 0; j < text.Length; j++) 67 { 68 RectangleF char_rect = new RectangleF(this.Padding.X + rect.X, this.Padding.Y + rect.Y + sum, text_size.Width, text_size.Height + 1); 69 g.DrawString(text.Substring(j, 1), this.Font, (i == this.SelectedIndex) ? text_selected_sb : text_normal_sb, char_rect, text_sf); 70 sum += text_size.Height + 1; 71 } 72 } 73 else 74 { 75 g.DrawString(this.TabPages[i].Text, this.Font, (i == this.SelectedIndex) ? text_selected_sb : text_normal_sb, rect, text_sf); 76 } 77 #endregion 78 79 #region 绘制关闭按钮 80 if (this.TabCloseShow) 81 { 82 RectangleF close_rect = this.GetTabCloseRectangle(i); 83 g.DrawLine(close_pen, new PointF(close_rect.X, close_rect.Y), new PointF(close_rect.Right, close_rect.Bottom)); 84 g.DrawLine(close_pen, new PointF(close_rect.Right, close_rect.Y), new PointF(close_rect.Left, close_rect.Bottom)); 85 } 86 #endregion 87 } 88 89 if (tabitem_region != null) 90 { 91 g.Clip = client_region; 92 tabitem_region.Dispose(); 93 } 94 95 if (back_normal_sb != null) 96 back_normal_sb.Dispose(); 97 if (back_selected_sb != null) 98 back_selected_sb.Dispose(); 99 if (text_normal_sb != null) 100 text_normal_sb.Dispose(); 101 if (text_selected_sb != null) 102 text_selected_sb.Dispose(); 103 if (text_sf != null) 104 text_sf.Dispose(); 105 if (close_pen != null) 106 close_pen.Dispose(); 107 #endregion 108 109 #region 设置TabPage内容页边框色 110 if (this.TabCount > 0) 111 { 112 Pen border_pen = new Pen(this.TabPageBorderColor, 1); 113 Rectangle borderRect = this.TabPages[0].Bounds; 114 borderRect.Inflate(1, 1); 115 g.DrawRectangle(border_pen, borderRect); 116 border_pen.Dispose(); 117 } 118 #endregion 119 }