tabcontrol在切换页面的时候经常用到
这里讲下如何让tabcontrol更好看
ref:http://blog.****.net/conmajia/article/details/7596718
http://wenku.baidu.com/link?url=y4BdtX3mOer4Hdin019jJpXJLi-2_ehmEo7i08cxEp1OR_3gb5CqaHrnNEB2iLQyNDqpkNtnuREmn4GWpur081mIPuNH-1184wLkFzsVuEq
一。 创建一个类,加引用,using什么的,TabControlEx的基类为System.Windows.Forms.TabControl
二。加代码
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; using System.Drawing.Drawing2D; namespace MyTabControl { public class TabControlEx : System.Windows.Forms.TabControl { Image backImage; public TabControlEx() { base.SetStyle( ControlStyles.UserPaint | ControlStyles.OptimizedDoubleBuffer | ControlStyles.ResizeRedraw | ControlStyles.SupportsTransparentBackColor, true); base.Update(); this.SizeMode = TabSizeMode.Fixed; , ); backImage = new Bitmap(this.GetType(), "MyTabControl.bmp"); } Form oldman; protected override void OnParentChanged(EventArgs e) { if (oldman == null) oldman = this.FindForm(); oldman.Text = ].Text; } protected override void OnSelected(TabControlEventArgs e) { Parent.Text = e.TabPage.Text; } protected override void OnPaint(PaintEventArgs e) { e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias; ; i < this.TabCount; i++) { //e.Graphics.DrawRectangle(Pens.Red, this.GetTabRect(i)); if (this.SelectedIndex == i) { e.Graphics.DrawImage(backImage, this.GetTabRect(i)); } Rectangle bounds = this.GetTabRect(i); PointF textPoint = new PointF(); SizeF textSize = TextRenderer.MeasureText(this.TabPages[i].Text, this.Font); textPoint.X = bounds.X + (bounds.Width - textSize.Width) / ; textPoint.Y = bounds.Bottom - textSize.Height - this.Padding.Y; e.Graphics.DrawString( this.TabPages[i].Text, this.Font, SystemBrushes.ControlLightLight, textPoint.X, textPoint.Y); textPoint.Y--; e.Graphics.DrawString( this.TabPages[i].Text, this.Font, SystemBrushes.ControlText, textPoint.X, textPoint.Y); if (this.ImageList != null) { int index = this.TabPages[i].ImageIndex; string key = this.TabPages[i].ImageKey; Image icon = , ); ) { icon = this.ImageList.Images[index]; } if (!string.IsNullOrEmpty(key)) { icon = this.ImageList.Images[key]; } e.Graphics.DrawImage( icon, bounds.X + (bounds.Width - icon.Width) / , bounds.Top + this.Padding.Y); } } } } }
三。加imagelist,并且为tabcontrol里的tabpage设置imageindex和imageKey
四。将此类加入工具箱中,注意将该项目的属性里的调试改为外部程序调试