如何将 TabPage 的标题文本设为粗体?

发布于 2024-08-22 13:20:56 字数 74 浏览 7 评论 0原文

我在 C# Windows 应用程序中有一些 tabControl。它有一些标签页。有谁知道如何使 tabPage 文本变为粗体..?

I have some tabControl in C# Windows app. It has some tabPages. Does anyone kwows how to make the tabPage Text to become Bold..?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

花开柳相依 2024-08-29 13:20:56

您需要处理 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));

}

You'll need to handle the DrawItem event of the TabControl to manually draw the caption. Note: DrawMode of affected control should be set to TabDrawMode.OwnerDrawFixed.

Here is a sample:

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));

}
合久必婚 2024-08-29 13:20:56

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

请参阅 MSDN 示例< /a>.

In Winforms you can change the DrawMode and paint all the captions on yourself.

See the MSDN Example.

闻呓 2024-08-29 13:20:56

另一个不太优雅的选项是将父窗体/控件的 font->bold 属性设置为 true,这将使所有内容变为粗体,包括选项卡名称,然后在所有不需要粗体的控件上将粗体设置为 false。

Another, less elegant option is to set the font->bold property of the parent form/control to true, which will make everything bold including the tab names and then set bold to false on all the controls you don't want bold.

如痴如狂 2024-08-29 13:20:56

这是将 TabPage.Text 的图像设为粗体并使用 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 个选项卡页。

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

All one has to do is write the main TabControl code as follows:

 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))

This takes care of everything. There are total 114 TabPages.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文