开发者论坛

 找回密码
 注册 (请使用非IE浏览器)
查看: 2328|回复: 1

[源码] tabcontrol控件自定义,选项卡可以换图片,自己画线

[复制链接]

0

精华

0

贡献

0

赞扬

帖子
13
软币
110
在线时间
10 小时
注册时间
2017-5-12
发表于 2017-5-15 11:05:08 | 显示全部楼层 |阅读模式
public partial class TabControlEx : TabControl    {        Image backGroundImage;        public TabControlEx()        {            InitializeComponent();            //重画TabControl            base.SetStyle(                    ControlStyles.UserPaint |                    ControlStyles.OptimizedDoubleBuffer |                    ControlStyles.AllPaintingInWmPaint |                    ControlStyles.ResizeRedraw |                    ControlStyles.SupportsTransparentBackColor,                    true);            base.UpdateStyles();            this.Alignment = TabAlignment.Bottom;            this.Size = new Size(600,375);            this.SizeMode = TabSizeMode.Fixed;  // 大小模式为固定              //选项卡的大小            this.ItemSize = new Size(70, 20);   // 设定每个标签的尺寸              //背景图片            backGroundImage = new Bitmap(this.GetType(), "backgroundimage.png");        }        protected override void OnPaint(PaintEventArgs e)        {            for (int i = 0; i < this.TabCount; i++)            {                                               //画背景                e.Graphics.DrawImage(backGroundImage, this.GetTabRect(i));                if (this.SelectedIndex == i)                {                    //画线的位置及大小                    Rectangle rect = new Rectangle(this.GetTabRect(i).Location.X+4, this.GetTabRect(i).Location.Y + 1, 63, 17);                    e.Graphics.DrawRectangle(Pens.LightSlateGray, rect); //内框                                        //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);                // 注意要加上每个标签的左偏移量X                  textPoint.X                    = bounds.X + (bounds.Width - textSize.Width) / 2;                textPoint.Y                    = bounds.Bottom - textSize.Height - this.Padding.Y;                // Draw highlights                  e.Graphics.DrawString(                    this.TabPages[i].Text,                    this.Font,                    SystemBrushes.ControlLightLight,    // 高光颜色                      textPoint.X,                    textPoint.Y-2);                // 绘制正常文字                  textPoint.Y--;                e.Graphics.DrawString(                    this.TabPages[i].Text,                    this.Font,                    SystemBrushes.ControlText,    // 正常颜色                      textPoint.X,                    textPoint.Y-2);                if (this.ImageList != null)                {                    int index = this.TabPages[i].ImageIndex;                    string key = this.TabPages[i].ImageKey;                    Image icon = new Bitmap(1, 1);                    if (index > -1)                    {                        icon = this.ImageList.Images[index];                    }                    if (!string.IsNullOrEmpty(key))                    {                        icon = this.ImageList.Images[key];                    }                    e.Graphics.DrawImage(                        icon,                        bounds.X + (bounds.Width - icon.Width) / 2,                        bounds.Top + this.Padding.Y);                }            }        }    }
回复

使用道具 举报

0

精华

0

贡献

0

赞扬

帖子
13
软币
110
在线时间
10 小时
注册时间
2017-5-12
 楼主| 发表于 2017-5-15 11:05:48 | 显示全部楼层
自定义,falsh控件不可以用了,只能这样了
回复

使用道具 举报

Archiver|手机版|小黑屋|开发者网 ( 苏ICP备08004430号-2 )
版权所有:南京韵文教育信息咨询有限公司

GMT+8, 2024-4-26 03:57

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表