WinForm 修改TableControl背景和标签

 在界面设计中,TableControl控件经常使用。默认设置中,TabControl的背景和标签样式。接下来我们将学习如何修改TableControl的标签选项、修改TabControld的背景色或背景图片。页面效果如下:

简述原理

  TableControl项目属性DrawMode,将属性值设定为OwnerDrawFixed后,就可以由用户绘制标签。添加DrawItem事件(用户需要绘制Table时触发),然后在该方法中绘制自定义的标签即可。

关键代码

  设置DrawMode属性。

this.tabControl_main.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
    为TabControl添加DrawItem事件。
this.tabControl_main.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl_main_DrawItem);
   设置笔刷。
//设置笔刷
SolidBrush red = new SolidBrush(Color.Red);              // 红色
  绘制背景。
//绘制红色背景
Rectangle rectangleRed = tabControl_main.GetTabRect(0);
e.Graphics.FillRectangle(red, rectangleRed);

  设置文字对齐属性。

//设置文字居中对齐 
StringFormat stringFormat = new StringFormat();
stringFormat.Alignment = StringAlignment.Center;

  设置标签文本。

//设置文字字体和文字大小
e.Graphics.DrawString(tabControl_main.TabPages[i].Text , new Font("宋体",10) ,black ,rec , stringFormat);

   获取工作区域

 Rectangle recMain = tabControl_main.ClientRectangle;        //获取Table控件的工作区域
  添加背景图片
 //获取背景图片,我的背景图片在项目资源文件中。
Image backImage = Resources.bg_banner;
//绘制主控件的背景
e.Graphics.DrawImage(backImage, 0, 0, tabControl_main.Width, tabControl_main.Height);

 

步骤

设置标签

  • 在WinForm页面添加TableControl控件,完成基本的设置。这里设置TableControl控件名称为tabControl_main,并添加三个选项卡,选项卡的关联文本分别为红色、黄色、蓝色。 

  • 设置DrawMode为OwnerDrawFixed。DrawMode属性的含义为指示是由用户还是由系统绘制标题。由于需要自定义标签背景色,所以由用户来绘制标题。

在页面中设置tabControl_main中设置DrawMode属性。

或者*.Designer.cs文件中,设置DrawMode属性。

this.tabControl_main.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
  •  为TabControl添加DrawItem事件(每当需要绘制特定项/特定区域时发生)。

  或者在*.Designer.cs页面代码如下:

this.tabControl_main.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl_main_DrawItem);
  • 设置背景标签。包括设置笔刷、绘制背景、设置文字。代码如下:
using System.Drawing;
using System.Windows.Forms;
//……
/// <summary>
        /// 初始化tableControl选项时的颜色
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void tabControl_main_DrawItem(object sender, DrawItemEventArgs e)
        {
            //设置笔刷
            SolidBrush red = new SolidBrush(Color.Red);              // 红色
            SolidBrush yellow = new SolidBrush(Color.Yellow);        //黄色
            SolidBrush blue = new SolidBrush(Color.Blue);             //蓝色
            SolidBrush black = new SolidBrush(Color.Black);            //黑色          
            //设置背景
            //绘制红色背景
            Rectangle rectangleRed = tabControl_main.GetTabRect(0);
            e.Graphics.FillRectangle(red, rectangleRed);
            //绘制黄色背景
            Rectangle rectangleYellow = tabControl_main.GetTabRect(1);
            e.Graphics.FillRectangle(yellow, rectangleYellow);
            //绘制黄色背景
            Rectangle rectangleBlue = tabControl_main.GetTabRect(2);
            e.Graphics.FillRectangle(blue, rectangleBlue);
            //设置标签文字居中对齐
            StringFormat stringFormat = new StringFormat();
            stringFormat.Alignment = StringAlignment.Center;
            //设置标签文字
            for (int i = 0; i <tabControl_main.TabPages.Count; i ++)
            {
                Rectangle rec = tabControl_main.GetTabRect(i);
                //设置文字字体和文字大小
                e.Graphics.DrawString(tabControl_main.TabPages[i].Text , new Font("宋体",10) ,black ,rec , stringFormat);
            }

        }
//……
启动后运行效果如下:

设置背景色或背景图片

  • 在WinForm页面添加TableControl控件(这里命名为tabControl_main),默认情况下,可以修改Tab的背景色为上一个容器的颜色。

  当修改DrawMode属性为OwnerDrawFixed时,背景色默认为系统颜色(灰色)。如下图:

 

 

  •  设置DrawMode为OwnerDrawFixed。DrawMode属性的含义为指示是由用户还是由系统绘制标题。由于需要自定义标签背景色,所以由用户来绘制标题。

  在页面中设置tabControl_main中设置DrawMode属性。

或者*.Designer.cs文件中,设置DrawMode属性:

this.tabControl_main.DrawMode = System.Windows.Forms.TabDrawMode.OwnerDrawFixed;
  • 为TabControl添加DrawItem事件(每当需要绘制特定项/特定区域时发生)。

  或者在*.Designer.cs页面代码如下:

this.tabControl_main.DrawItem += new System.Windows.Forms.DrawItemEventHandler(this.tabControl_main_DrawItem);
  • 在tabControl_main_DrawItem中添加背景颜色代码如下:
using System.Drawing;
using System.Windows.Forms;
//……
private void tabControl_main_DrawItem(object sender, DrawItemEventArgs e) {
//……
SolidBrush tab_blackColr = new SolidBrush(Color.Black);     // 笔刷背景
            Rectangle recMain = tabControl_main.ClientRectangle;        //获取Table控件的工作区域
            e.Graphics.FillRectangle(tab_blackColr , recMain);          //绘制TabControl背景
//……
}
//…

   运行效果如下:

 

 

  • 在tabControl_main_DrawItem中添加背景图片代码如下:
using System.Drawing;
using System.Windows.Forms;
//……
private void tabControl_main_DrawItem(object sender, DrawItemEventArgs e) {
//……
//获取背景图片,我的背景图片在项目资源文件中。
            Image backImage = Resources.bg_banner;
            //绘制主控件的背景
            e.Graphics.DrawImage(backImage, 0, 0, tabControl_main.Width, tabControl_main.Height);
//……
}
//……

 

  运行效果如下:

参考网址

  [1] https://jingyan.baidu.com/article/8ebacdf0caf35649f75cd562.html

  [2] https://www.cnblogs.com/jfbloc/archive/2011/11/12/2246549.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值