Set TabPage Header Color

前端 未结 4 1625
再見小時候
再見小時候 2020-11-27 06:57

Greetings,

I have a tab control and I want to have 1 of the tabs have it\'s text color changed on a event. I\'ve found answers like C# - TabPage Color event and C# W

4条回答
  •  离开以前
    2020-11-27 07:36

    To add to Fun Mun Pieng's answer which works beautifully on Horizontal tabs, if you were to use Vertical tabs (like I was) then you would need something like this:

        private void tabControl2_DrawItem(object sender, DrawItemEventArgs e)
        {
            using (Brush br = new SolidBrush(tabColorDictionary[tabControl2.TabPages[e.Index]]))
            {
                // Color the Tab Header
                e.Graphics.FillRectangle(br, e.Bounds);
                // swap our height and width dimensions
                var rotatedRectangle = new Rectangle(0, 0, e.Bounds.Height, e.Bounds.Width);
    
                // Rotate
                e.Graphics.ResetTransform();
                e.Graphics.RotateTransform(-90);
    
                // Translate to move the rectangle to the correct position.
                e.Graphics.TranslateTransform(e.Bounds.Left, e.Bounds.Bottom, System.Drawing.Drawing2D.MatrixOrder.Append);
    
                // Format String
                var drawFormat = new System.Drawing.StringFormat();
                drawFormat.Alignment = StringAlignment.Center;
                drawFormat.LineAlignment = StringAlignment.Center;
    
                // Draw Header Text
                e.Graphics.DrawString(tabControl2.TabPages[e.Index].Text, e.Font, Brushes.Black, rotatedRectangle, drawFormat);
            }
        }
    

    I will echo the point that ROJO1969 made, if this is in WinForms - then you must set DrawMode to OwnerDrawFixed.

    Special thanks goes out to this wonderful blog entry which described how to do a rotation of text on a form.

提交回复
热议问题