Winform 中tabcontrol 美化

本文介绍如何通过自定义绘制事件来美化TabControl控件,包括设置背景图片、调整标题样式等,适用于希望提升界面美观度的Windows Forms应用程序。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

需要对tabcontrol按照美工出的图进行美化

对tabpage页进行标题设置,首先对整个tabcontrol的DrawMode设置为OwnerDrawFixed,由于需要对标题宽度有要求,设置sizemode为fixed,可以对itemsize调整,设置标题的宽和高,然后进行写事件就能实现

 private void tb_DrawItem(object sender, DrawItemEventArgs e)
        {
            StringFormat sf = new StringFormat();

            #region 头背景
            sf.LineAlignment = StringAlignment.Center;
            sf.Alignment = StringAlignment.Center;
            Rectangle rec = tb.ClientRectangle;
            //获取背景图片,我的背景图片在项目资源文件中。
            Image backImage = Properties.Resources.pop_tab1_2;
            e.Graphics.DrawImage(backImage, 0, 2, tb.Width, tb.ItemSize.Height + 2);
            #endregion
            #region  设置选择的标签的背景
            if (e.Index == tb.SelectedIndex)
                e.Graphics.DrawImage(Properties.Resources.pop_tab1_1, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
            else
                e.Graphics.DrawImage(Properties.Resources.pop_tab1_2, e.Bounds.X, e.Bounds.Y, e.Bounds.Width, e.Bounds.Height);
            e.Graphics.DrawString(((TabControl)sender).TabPages[e.Index].Text,
            System.Windows.Forms.SystemInformation.MenuFont, new SolidBrush(Color.Black), e.Bounds, sf);
            #endregion
            #region 重写标签名
            ColorConverter colorConverter = new ColorConverter();
            Color cwhite = (Color)colorConverter.ConvertFromString("#2178ba");
            SolidBrush white = new SolidBrush(cwhite);
            Rectangle rect0 = tb.GetTabRect(0);
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            stringFormat.LineAlignment = StringAlignment.Center;
       e.Graphics.DrawString("申请单号", new Font("微软雅黑", 12), white, rect0, stringFormat);
           #endregion
        }

 

转载于:https://www.cnblogs.com/JohnnyBao/p/4402871.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值