简体   繁体   English

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

[英]Put Bold font in title of tab in a tabcontrol c#

I've got this tab control: 我有这个标签控件:

tabcontrol2

I need to put the tab name "Notes" in a bold font but I don't know how to. 我需要将标签名称“Notes”放在粗体字体中,但我不知道如何操作。

I tried this code: 我试过这段代码:

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

However, it put all tabs in bold. 但是,它将所有选项卡都加粗。 Then I tried this: 然后我尝试了这个:

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

I also tried this : How do I make a TabPage's title text bold? 我也试过这个: 如何使TabPage的标题文字变粗?

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

However, it put all the content of the tab in bold and not the title. 但是,它将选项卡的所有内容都以粗体显示,而不是标题。 I don't know how to put the title of this specific tabpage. 我不知道如何把这个特定标签的标题。 Does anyone have an idea ? 有没有人有想法?

I hope I can be of any help to you. 我希望我能为你提供任何帮助。

在此输入图像描述

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM