在tabcontrol中将Bold字体放在选项卡的标题中c#

时间:2017-06-08 13:28:36

标签: c# winforms controls tabcontrol

我有这个标签控件:

tabcontrol2

我需要将标签名称" Notes"用粗体字体但我不知道怎么做。

我试过这段代码:

tabControl2.Font = new Font(this.Font, FontStyle.Bold);

然而,它将所有标签都以粗体显示。然后我尝试了这个:

tabControl2.TabPages["Notes"].Font = new Font(this.Font, FontStyle.Bold);

我也试过这个: How do I make a TabPage's title text bold?

Graphics g = e.Graphics;
            Brush _TextBrush;

            // Get the item from the collection.
            TabPage _TabPage = tabControl2.TabPages["Notes"];

            // Get the real bounds for the tab rectangle.
            Rectangle _TabBounds = tabControl2.GetTabRect(1);

            _TextBrush = new System.Drawing.SolidBrush(e.ForeColor);

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

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

但是,它将选项卡的所有内容都以粗体显示而不是标题。我不知道如何把这个特定标签页的标题。有没有人有想法?

1 个答案:

答案 0 :(得分:0)

我希望我能为你提供任何帮助。

enter image description here

public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();

        tabControl.DrawMode = TabDrawMode.OwnerDrawFixed;
        tabControl.DrawItem += TabControlOnDrawItem;
    }

    private FontStyle HasNotification(string tabText)
    {
        return tabText.Equals("Notes") && true 
                   ? FontStyle.Bold
                   : FontStyle.Regular;
    }

    private void TabControlOnDrawItem(object sender, DrawItemEventArgs e)
    {
        var tab = (TabControl) sender;

        var tabText = tab.TabPages[e.Index].Text;

        e.Graphics
         .DrawString(tabText
                     , new Font(tab.Font.FontFamily
                                , tab.Font.Size
                                , HasNotification(tabText))
                     , Brushes.Black
                     , e.Bounds
                     , new StringFormat
                       {
                           Alignment = StringAlignment.Center,
                           LineAlignment = StringAlignment.Center
                       });
    }
}