tabcontrol 修改选中后颜色
时间: 2023-10-09 21:16:26 浏览: 89
在Windows操作系统中,Tab控件被选中时默认会显示蓝色的边框和背景颜色。如果你想修改选中后的颜色,可以通过修改样式来实现。以下是一个C# WinForms中修改TabControl选中后颜色的示例代码:
```csharp
// 设置选中的Tab的背景色和前景色
tabControl1.SelectedTab.BackColor = Color.Red;
tabControl1.SelectedTab.ForeColor = Color.White;
// 设置未选中的Tab的背景色和前景色
foreach (TabPage tabPage in tabControl1.TabPages)
{
if (tabPage != tabControl1.SelectedTab)
{
tabPage.BackColor = Color.White;
tabPage.ForeColor = Color.Black;
}
}
```
上述代码中,首先设置选中的Tab的背景色为红色,前景色为白色,然后遍历TabControl中的所有TabPage,将未选中的Tab的背景色设置为白色,前景色设置为黑色。你可以根据自己的需要修改这些颜色值。
相关问题
wpf tabcontrol 选中标签的颜色
你可以通过修改 TabControl 的样式来自定义选中标签的颜色。以下是一个示例:
```xaml
<TabControl>
<TabControl.Resources>
<Style TargetType="TabItem">
<Setter Property="Template">
<Setter.Value>
<ControlTemplate TargetType="TabItem">
<Border Name="Border" BorderThickness="1" BorderBrush="Black" Margin="-2,0,0,0" Padding="15,5,15,5">
<ContentPresenter x:Name="ContentSite"
VerticalAlignment="Center"
HorizontalAlignment="Center"
ContentSource="Header"
Margin="12,2,12,2"
RecognizesAccessKey="True"/>
</Border>
<ControlTemplate.Triggers>
<Trigger Property="IsSelected" Value="True">
<Setter TargetName="Border" Property="Background" Value="LightBlue"/>
</Trigger>
<Trigger Property="IsSelected" Value="False">
<Setter TargetName="Border" Property="Background" Value="White"/>
</Trigger>
</ControlTemplate.Triggers>
</ControlTemplate>
</Setter.Value>
</Setter>
</Style>
</TabControl.Resources>
<TabItem Header="Tab 1">
<StackPanel>
<TextBlock Text="Tab 1 content"/>
</StackPanel>
</TabItem>
<TabItem Header="Tab 2">
<StackPanel>
<TextBlock Text="Tab 2 content"/>
</StackPanel>
</TabItem>
</TabControl>
```
在上面的示例中,我们设置了 TabItem 的模板,其中包含一个 Border 和一个 ContentPresenter。我们使用 Trigger 来为选中和未选中的 TabItem 设置不同的背景颜色,以此区分选中的 Tab。你可以根据需要修改背景色和其他样式。
Winform修改tabControl标题栏的颜色
您可以使用以下代码来修改 Winform 中 tabControl 标题栏的颜色:
1. 首先,在 tabControl 的 DrawItem 事件中添加以下代码:
private void tabControl1_DrawItem(object sender, DrawItemEventArgs e)
{
Graphics g = e.Graphics;
Brush _textBrush;
// Get the item from the collection.
TabPage _tabPage = tabControl1.TabPages[e.Index];
// Get the real bounds for the tab rectangle.
Rectangle _tabBounds = tabControl1.GetTabRect(e.Index);
if (e.State == DrawItemState.Selected)
{
// Draw a different background color for the selected tab.
_textBrush = new SolidBrush(Color.White);
g.FillRectangle(Brushes.Blue, e.Bounds);
}
else
{
_textBrush = new System.Drawing.SolidBrush(e.ForeColor);
e.DrawBackground();
}
// Use our own font.
Font _tabFont = new Font("Arial", (float)10.0, FontStyle.Bold, GraphicsUnit.Pixel);
// Draw string. Center the text.
StringFormat _stringFlags = new StringFormat();
_stringFlags.Alignment = StringAlignment.Center;
_stringFlags.LineAlignment = StringAlignment.Center;
g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));
}
2. 然后,在 tabControl 的 DrawMode 属性中选择 OwnerDrawFixed。
这样就可以修改 tabControl 标题栏的颜色了。