C# 有没有办法在winforms中为标签页的标签着色?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2107463/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there a way to color tabs of a tabpage in winforms?
提问by Rajeev Ranjan Lal
I am struggling to find a way to color the tab headers of a tabpage in WinForms. There are solutions to color the current indexed tab using the OnDrawItem
event, but is it possible to color all the tabs with different colors to make them more intuitive for users for certain behavior?
我正在努力寻找一种方法来为 WinForms 中标签页的标签页眉着色。有使用OnDrawItem
事件为当前索引选项卡着色的解决方案,但是是否可以用不同的颜色为所有选项卡着色,以使用户对某些行为更直观?
Thanks in advance.
提前致谢。
采纳答案by Ash
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.
是的,不需要任何 win32 代码。您只需将选项卡控件的 DrawMode 属性设置为“OwnerDrawFixed”,然后处理选项卡控件的 DrawItem 事件。
The following code shows how:
以下代码显示了如何:
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);
}
Setting the DrawMode to 'OwnerDrawnFixed' means each tab button has to be the same size (ie Fixed).
将 DrawMode 设置为 'OwnerDrawnFixed' 意味着每个选项卡按钮必须具有相同的大小(即固定)。
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.
但是,如果要更改所有选项卡按钮的大小,可以将选项卡控件的 SizeMode 属性设置为“Fixed”,然后更改 ItemSize 属性。
回答by Marc Gravell
Using the current tab control, ifit is possible you'd need to hook a lot of win-32 events (there may be a pre-wrapped implementation out there). Another alternative would be a 3rd-party tabbed control replacement; I'm sure plenty of vendors will sell you one.
使用当前的选项卡控件,如果可能的话,您需要挂钩很多 win-32 事件(那里可能有一个预先包装的实现)。另一种选择是替换第 3 方选项卡式控件;我相信很多供应商会卖给你一个。
IMO, you might find it less pain to look at WPF; it is a big change, but has more control over things like this. You can host WPF inside winforms if needed (if you can't justify a full make-over, which is a pretty common reality).
IMO,您可能会发现查看 WPF 不那么痛苦;这是一个很大的变化,但对这样的事情有更多的控制权。如果需要,您可以在 winforms 中托管 WPF(如果您不能证明完全改造是合理的,这是一个非常普遍的现实)。
回答by g t
An improved version of Ash's answer:
Ash答案的改进版本:
private void tabControl_DrawItem(object sender, DrawItemEventArgs e)
{
TabPage page = tabControl.TabPages[e.Index];
e.Graphics.FillRectangle(new SolidBrush(page.BackColor), e.Bounds);
Rectangle paddedBounds = e.Bounds;
int yOffset = (e.State == DrawItemState.Selected) ? -2 : 1;
paddedBounds.Offset(1, yOffset);
TextRenderer.DrawText(e.Graphics, page.Text, e.Font, paddedBounds, page.ForeColor);
}
This code uses the TextRenderer
class to draw its text (as .NET does), fixes problems with font clipping/wrapping by not negatively inflating the bounds, and takes tab selection into account.
此代码使用TextRenderer
该类来绘制其文本(如 .NET 所做的那样),通过不对边界产生负面影响来修复字体剪切/换行问题,并考虑选项卡选择。
Thanks to Ash for the original code.
感谢 Ash 提供原始代码。