自定义tabcontrol实现

c# tabcontrol自定义实现

具体代码如下所示:

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace ShengXinSolution.Client.LablePrintSystemV2
{
    class TabControlAnjun : TabControl
    {
        Image backImage;
        public TabControlAnjun() {
            this.SizeMode = TabSizeMode.Fixed;
            this.ItemSize = new Size(44,55);
            backImage = new Bitmap(this.GetType(), "test.png");
            base.SetStyle(
                ControlStyles.UserPaint |
                ControlStyles.OptimizedDoubleBuffer |
                ControlStyles.AllPaintingInWmPaint |
                ControlStyles.ResizeRedraw |
                ControlStyles.SupportsTransparentBackColor,
                true);
            base.UpdateStyles();
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            for (int i = 0; i < this.TabCount; i++) {
                e.Graphics.DrawRectangle(Pens.Red,this.GetTabRect(i));
                if (this.SelectedIndex == i) {
                    e.Graphics.DrawImage(backImage, this.GetTabRect(i));
                }
                //Calculate text position
                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);

                //绘制正常文字
                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 = 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);
                }
            }
        }
    }
}
分享一句最近比较喜欢的一句话

如果才华撑不起你的梦想,那就安静下来读书吧!

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值