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

标签 c# .net winforms tabcontrol

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

最佳答案

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

}

关于c# - 如何将 TabPage 的标题文本设为粗体?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2326406/

相关文章:

c# - 如何在 MVC3 中使用 RequireHttps 属性指定不同的端口

c# - 如何从 .net 中 wwwroot 内的多个文件夹提供 html 文件

c# - VLC 服务器与 .NET C# 的通信

c# - 用键盘输入日期只是数字,没有不必要的操作

c# - 有什么办法可以将gnuplot的图表合并到winform中

c# - 数据绑定(bind)到对象 - 如何更新对象/绑定(bind)?

c# - 使用 LINQ 从 XML 获取最大属性值

c# - 如何在 Xamarin.forms 的轮播页面中以编程方式更改页面?

c# - 如何确定信号 R 使用的传输方法

c# - 使用存储库时,ASP.NET MVC 中业务逻辑的最佳位置是什么?