首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >如何更改每个选项卡的颜色?

如何更改每个选项卡的颜色?
EN

Stack Overflow用户
提问于 2015-05-05 04:45:35
回答 2查看 20.3K关注 0票数 3

我有一个表单,上面有四个选项卡,我希望每个选项卡都有不同的颜色。我在网上能找到的唯一一件事就是如何改变所选标签的颜色,而其余的标签保持原来的颜色。我还没有找到任何可以给每个选项卡赋予自己颜色的东西。我目前拥有的代码是。

代码语言:javascript
运行
复制
Private Sub TabControl1_DrawItem(sender As System.Object, e As System.Windows.Forms.DrawItemEventArgs) Handles TabControl1.DrawItem

        Dim g As Graphics = e.Graphics
        Dim tp As TabPage = TabControl1.TabPages(e.Index)
        Dim br As Brush
        Dim sf As New StringFormat

        Dim r As New RectangleF(e.Bounds.X, e.Bounds.Y + 2, e.Bounds.Width, e.Bounds.Height - 2)

        sf.Alignment = StringAlignment.Center

        Dim strTitle As String = tp.Text

        If TabControl1.SelectedIndex = e.Index Then

            'this is the background color of the tabpage header
            br = New SolidBrush(Color.LightSteelBlue) ' chnge to your choice
            g.FillRectangle(br, e.Bounds)

            'this is the foreground color of the text in the tab header
            br = New SolidBrush(Color.Black) ' change to your choice
            g.DrawString(strTitle, TabControl1.Font, br, r, sf)

        Else

            'these are the colors for the unselected tab pages 
            br = New SolidBrush(Color.Blue) ' Change this to your preference
            g.FillRectangle(br, e.Bounds)
            br = New SolidBrush(Color.Black)
            g.DrawString(strTitle, TabControl1.Font, br, r, sf)

        End If
    End Sub
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2015-05-05 05:28:23

您需要做两件事:

第一种方法是更改TabControl的DrawMode并将其设置为OwnerDrawFixed

第二个是处理TabControl DrawItem事件

下面是一个示例:

代码语言:javascript
运行
复制
 Private Sub TabControl1_DrawItem(sender As Object, e As DrawItemEventArgs) Handles TabControl1.DrawItem
    Select Case e.Index
        Case 0
            e.Graphics.FillRectangle(New SolidBrush(Color.Red), e.Bounds)
        Case 1
            e.Graphics.FillRectangle(New SolidBrush(Color.Blue), e.Bounds)
        Case 2
            e.Graphics.FillRectangle(New SolidBrush(Color.Magenta), e.Bounds)

    End Select

    Dim paddedBounds As Rectangle = e.Bounds
    paddedBounds.Inflate(-2, -2)
    e.Graphics.DrawString(TabControl1.TabPages(e.Index).Text, Me.Font, SystemBrushes.HighlightText, paddedBounds)


End Sub

下面是它的样子(我只更改前三个选项卡页面的选项卡颜色,其他的可以通过添加新的案例来选择案例)

票数 5
EN

Stack Overflow用户

发布于 2015-05-05 05:02:28

TabControl1是通过设计器添加到窗体的选项卡控件吗?为什么不在创建选项卡时为每个选项卡设置TabBackColor属性?

如果没有(您必须通过代码来完成),只需使用一个循环来遍历TabControl1的选项卡页集合(TabControl1.TabPages)中的每个选项卡,并为其中的每个选项卡设置TabBackColor属性。

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30039627

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档