有没有一种方法,以颜色的WinForms一个的TabPage的标签? [英] Is there a way to color tabs of a tabpage in winforms?
问题描述
我在努力寻找办法颜色的TabPage的标签头在的WinForms。有解决方案中使用的OnDrawItem事件着色当前索引标签,但也可以是有可能在一个时间,使它们的直观用户一定行为上色所有不同颜色的标签。
I am struggling to find way to color the tab headers of a tabpage in WinForms. There are solutions to color the current indexed tab using OnDrawItem event but can it be possible to color all the tabs with different colors at a time to make them intuitive for user for a certain behavior.
由于提前,
拉杰夫·兰詹拉尔
推荐答案
是的,有没有需要任何的Win32代码。你只需要设置选项卡控制DrawMode属性为OwnerDrawFixed,然后处理标签控件的DrawItem事件。
Yes, there is no need for any win32 code. You just need to set the tab controls DrawMode property to 'OwnerDrawFixed' and then handle the tab control's DrawItem event.
下面的代码演示如何:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
// This event is called once for each tab button in your tab control
// First paint the background with a color based on the current tab
// e.Index is the index of the tab in the TabPages collection.
switch (e.Index )
{
case 0:
e.Graphics.FillRectangle(new SolidBrush(Color.Red), e.Bounds);
break;
case 1:
e.Graphics.FillRectangle(new SolidBrush(Color.Blue), e.Bounds);
break;
default:
break;
}
// Then draw the current tab button text
Rectangle paddedBounds=e.Bounds;
paddedBounds.Inflate(-2,-2);
e.Graphics.DrawString(tabControl1.TabPages[e.Index].Text, this.Font, SystemBrushes.HighlightText, paddedBounds);
}
设置DrawMode为OwnerDrawnFixed是指每个选项卡按钮有为相同的尺寸(即固定)。
Setting the DrawMode to 'OwnerDrawnFixed' means each tab button has to be the same size (ie Fixed).
不过,如果你想改变所有标签按钮的大小,您可以在标签控件的SizeMode属性设置为固定,然后更改ItemSize属性。
However if you want to change the size of all tab buttons, you can set the tab control's SizeMode property to 'Fixed' and then change the ItemSize property.
这篇关于有没有一种方法,以颜色的WinForms一个的TabPage的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!