簡體   English   中英

如何在C#中使tabControl1.DrawItem為背景使用特定顏色?

[英]How can I make a tabControl1.DrawItem use a specific color for the background in C#?

我按照此處的說明創建了一個側面對齊的標簽控件。 但是,如果我更改表單本身的背景顏色,則tabControl中將留有白色區域(參見圖片)。 如何使tabControl透明或匹配表單的顏色?

在此處輸入圖片說明

    public Form1()
    {
        InitializeComponent();
        tabControl1.DrawItem += new DrawItemEventHandler(tabControl1_DrawItem);
    }

    private void tabControl1_DrawItem(Object sender, System.Windows.Forms.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.Red);
            g.FillRectangle(Brushes.Gray, e.Bounds);
        }
        else
        {
            _textBrush = new System.Drawing.SolidBrush(e.ForeColor);
            e.DrawBackground();
        }

        // Use our own font.
        Font _tabFont = new Font("Arial", (float)10.0, FontStyle.Bold, GraphicsUnit.Pixel);

        // Draw string. Center the text.
        StringFormat _stringFlags = new StringFormat();
        _stringFlags.Alignment = StringAlignment.Center;
        _stringFlags.LineAlignment = StringAlignment.Center;
        g.DrawString(_tabPage.Text, _tabFont, _textBrush, _tabBounds, new StringFormat(_stringFlags));
    }

在Windows窗體中,您可以停靠特定的錨點。 如果將點與父窗口對接,則應消除這些間隙。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM