C#控件:TabControl

tabcontrol在切换页面的时候经常用到

这里讲下如何让tabcontrol更好看

ref:http://blog.csdn.net/conmajia/article/details/7596718

http://wenku.baidu.com/link?url=y4BdtX3mOer4Hdin019jJpXJLi-2_ehmEo7i08cxEp1OR_3gb5CqaHrnNEB2iLQyNDqpkNtnuREmn4GWpur081mIPuNH-1184wLkFzsVuEq

一。 创建一个类,加引用,using什么的,TabControlEx的基类为System.Windows.Forms.TabControl

二。加代码

 1 using System;

 2 using System.Collections.Generic;

 3 using System.Linq;

 4 using System.Text;

 5 using System.Windows.Forms;

 6 using System.Drawing;

 7 using System.Drawing.Drawing2D;

 8 

 9 namespace MyTabControl

10 {

11     public class TabControlEx : System.Windows.Forms.TabControl

12     {

13         Image backImage;

14         public TabControlEx()

15         {

16             base.SetStyle(

17                 ControlStyles.UserPaint |

18                 ControlStyles.OptimizedDoubleBuffer |

19                 ControlStyles.ResizeRedraw |

20                 ControlStyles.SupportsTransparentBackColor,

21                 true);

22             base.Update();

23             this.SizeMode = TabSizeMode.Fixed;

24             this.ItemSize = new Size(44, 55);

25             backImage = new Bitmap(this.GetType(), "MyTabControl.bmp");

26         }

27         Form oldman;

28         protected override void OnParentChanged(EventArgs e)

29         {

30             if (oldman == null) oldman = this.FindForm();

31             oldman.Text = this.TabPages[0].Text;

32         }

33         protected override void OnSelected(TabControlEventArgs e)

34         {

35             Parent.Text = e.TabPage.Text;

36         }

37         protected override void OnPaint(PaintEventArgs e)

38         {

39             e.Graphics.TextRenderingHint = System.Drawing.Text.TextRenderingHint.AntiAlias;

40             for (int i = 0; i < this.TabCount; i++)

41             {

42                 //e.Graphics.DrawRectangle(Pens.Red, this.GetTabRect(i));

43                 if (this.SelectedIndex == i)

44                 {

45                     e.Graphics.DrawImage(backImage, this.GetTabRect(i));

46                 }

47                 Rectangle bounds = this.GetTabRect(i);

48                 PointF textPoint = new PointF();

49                 SizeF textSize = TextRenderer.MeasureText(this.TabPages[i].Text, this.Font);

50                 textPoint.X = bounds.X + (bounds.Width - textSize.Width) / 2;

51                 textPoint.Y = bounds.Bottom - textSize.Height - this.Padding.Y;

52                 e.Graphics.DrawString(

53                     this.TabPages[i].Text,

54                     this.Font,

55                     SystemBrushes.ControlLightLight,

56                     textPoint.X,

57                     textPoint.Y);

58                 textPoint.Y--;

59                 e.Graphics.DrawString(

60                     this.TabPages[i].Text,

61                     this.Font,

62                     SystemBrushes.ControlText,

63                     textPoint.X,

64                     textPoint.Y);

65                 if (this.ImageList != null)

66                 {

67                     int index = this.TabPages[i].ImageIndex;

68                     string key = this.TabPages[i].ImageKey;

69                     Image icon = new Bitmap(1, 1);

70                     if (index > -1)

71                     {

72                         icon = this.ImageList.Images[index];

73                     }

74                     if (!string.IsNullOrEmpty(key))

75                     {

76                         icon = this.ImageList.Images[key];

77                     }

78                     e.Graphics.DrawImage(

79                         icon,

80                         bounds.X + (bounds.Width - icon.Width) / 2,

81                         bounds.Top + this.Padding.Y);

82                 }

83             }

84         }

85     }

86 }
View Code

 

三。加imagelist,并且为tabcontrol里的tabpage设置imageindex和imageKey

四。将此类加入工具箱中,注意将该项目的属性里的调试改为外部程序调试

你可能感兴趣的:(tab)