如何使TabPage的标题文本加粗?

6

我在C# Windows应用程序中有一些TabControl。它有一些TabPage。有人知道如何使TabPage文本变为粗体吗?


你想要加粗什么?选项卡标题还是选项卡页面的内容? - Jon Seigel
Winform。我想要加粗选项卡标题。 - vts123
3
请参考这里:https://dev59.com/ZUXRa4cB1Zd3GeqPrnDt - Jon Seigel
4个回答

13

您需要处理TabControlDrawItem事件来手动绘制标题。注意:受影响控件的DrawMode应设置为TabDrawMode.OwnerDrawFixed

这是一个示例:

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, and don't paint a focus rectangle.
        _TextBrush = new SolidBrush(Color.Blue);
        g.FillRectangle(Brushes.Gray, e.Bounds);
    }
    else
    {
        _TextBrush = new System.Drawing.SolidBrush(e.ForeColor);
       // e.DrawBackground();
    }

    // Use our own font. Because we CAN.
    Font _TabFont = new Font(e.Font.FontFamily, (float)9, FontStyle.Bold, GraphicsUnit.Pixel);
    //Font fnt = new Font(e.Font.FontFamily, (float)7.5, FontStyle.Bold);

    // Draw string. Center the text.
    StringFormat _StringFlags = new StringFormat();
    _StringFlags.Alignment = StringAlignment.Center;
    _StringFlags.LineAlignment = StringAlignment.Center;
    g.DrawString(tabControl1.TabPages[e.Index].Text, _TabFont, _TextBrush,
                 _TabBounds, new StringFormat(_StringFlags));

}

只是补充一下:受影响的控件的 DrawMode 应该设置为 TabDrawMode.OwnerDrawFixed,这使得使用 DrawItem 事件成为必须(如果您将此方法的代码留空,则选项卡的标题栏也将为空白)。 - DrCopyPaste

3

在Winforms中,您可以更改DrawMode并自己绘制所有标题。

请参阅MSDN示例


0

This is an Image of TabPage.Text made Bold and with FontSize 25

只需编写以下主TabControl代码即可:
 TabControl0_1=New TabControl

 TabControl0_1.Size = New System.Drawing.Size(1900,980)

 TabControl0_1.Location=New System.Drawing.Point(5,5)

 TabControl0_1.Font = New System.Drawing.Font("Segoe UI",25!, _
                       System.Drawing.FontStyle.Bold, System.Drawing. _
                       GraphicsUnit.Point,CType(0, Byte))

这个会处理所有事情。总共有114个选项卡页。


0
另一个不太优雅的选择是将父表单/控件的字体->bold属性设置为true,这将使所有内容(包括选项卡名称)都变为粗体,然后在您不想要加粗的所有控件上将bold设置为false。

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接