Vertical Tab Control with horizontal text in Winforms

前端 未结 5 836
再見小時候
再見小時候 2020-12-14 20:08

I would like to have the tabs on my TabControl displayed on the left, or sometimes right.
Unlike the System.Windows.Forms.TabControl, however, I would like the text to r

5条回答
  •  眼角桃花
    2020-12-14 20:29

    This is an old question but I found a good looking C# class. After adding the normal WinForms tab control. Add this to your project as a class E.G DotNetBarTabControl.cs Then make sure to replace the Winforms tab controls to this class.

    1. this.tabControl1 = new System.Windows.Forms.TabControl();

      must be changed to:

      this.tabControl1 = new TabControls.DotNetBarTabControl();


    1. private System.Windows.Forms.TabControl tabControl1;

      must be changed to:

      private TabControls.DotNetBarTabControl tabControl1;


    DotNetBarTabControl.cs Class:

    using System;
    using System.Drawing;
    using System.Drawing.Drawing2D;
    using System.Windows.Forms;
    
    // thanks to Mavamaarten~ for coding this
    
    namespace TabControls
    {
        internal class DotNetBarTabControl : TabControl
        {
            public DotNetBarTabControl()
            {
                SetStyle(
                    ControlStyles.AllPaintingInWmPaint | ControlStyles.ResizeRedraw | ControlStyles.UserPaint |
                    ControlStyles.DoubleBuffer, true);
                SizeMode = TabSizeMode.Fixed;
                ItemSize = new Size(44, 136);
                Alignment = TabAlignment.Left;
                SelectedIndex = 0;
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                Bitmap b = new Bitmap(Width, Height);
                Graphics g = Graphics.FromImage(b);
                if (!DesignMode)
                    SelectedTab.BackColor = SystemColors.Control;
                g.Clear(SystemColors.Control);
                g.FillRectangle(new SolidBrush(Color.FromArgb(246, 248, 252)),
                    new Rectangle(0, 0, ItemSize.Height + 4, Height));
                g.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(ItemSize.Height + 3, 0),
                    new Point(ItemSize.Height + 3, 999));
                g.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(0, Size.Height - 1),
                    new Point(Width + 3, Size.Height - 1));
                for (int i = 0; i <= TabCount - 1; i++)
                {
                    if (i == SelectedIndex)
                    {
                        Rectangle x2 = new Rectangle(new Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2),
                            new Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1));
                        ColorBlend myBlend = new ColorBlend();
                        myBlend.Colors = new Color[] { Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240), Color.FromArgb(232, 232, 240) };
                        myBlend.Positions = new float[] { 0f, 0.5f, 1f };
                        LinearGradientBrush lgBrush = new LinearGradientBrush(x2, Color.Black, Color.Black, 90f);
                        lgBrush.InterpolationColors = myBlend;
                        g.FillRectangle(lgBrush, x2);
                        g.DrawRectangle(new Pen(Color.FromArgb(170, 187, 204)), x2);
    
                        g.SmoothingMode = SmoothingMode.HighQuality;
                        Point[] p =
                        {
                            new Point(ItemSize.Height - 3, GetTabRect(i).Location.Y + 20),
                            new Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 14),
                            new Point(ItemSize.Height + 4, GetTabRect(i).Location.Y + 27)
                        };
                        g.FillPolygon(SystemBrushes.Control, p);
                        g.DrawPolygon(new Pen(Color.FromArgb(170, 187, 204)), p);
    
                        if (ImageList != null)
                        {
                            try
                            {
                                g.DrawImage(ImageList.Images[TabPages[i].ImageIndex],
                                    new Point(x2.Location.X + 8, x2.Location.Y + 6));
                                g.DrawString("  " + TabPages[i].Text, Font, Brushes.Black, x2, new StringFormat
                                {
                                    LineAlignment = StringAlignment.Center,
                                    Alignment = StringAlignment.Center
                                });
                            }
                            catch (Exception)
                            {
                                g.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold),
                                    Brushes.Black, x2, new StringFormat
                                    {
                                        LineAlignment = StringAlignment.Center,
                                        Alignment = StringAlignment.Center
                                    });
                            }
                        }
                        else
                        {
                            g.DrawString(TabPages[i].Text, new Font(Font.FontFamily, Font.Size, FontStyle.Bold),
                                Brushes.Black, x2, new StringFormat
                                {
                                    LineAlignment = StringAlignment.Center,
                                    Alignment = StringAlignment.Center
                                });
                        }
    
                        g.DrawLine(new Pen(Color.FromArgb(200, 200, 250)), new Point(x2.Location.X - 1, x2.Location.Y - 1),
                            new Point(x2.Location.X, x2.Location.Y));
                        g.DrawLine(new Pen(Color.FromArgb(200, 200, 250)), new Point(x2.Location.X - 1, x2.Bottom - 1),
                            new Point(x2.Location.X, x2.Bottom));
                    }
                    else
                    {
                        Rectangle x2 = new Rectangle(new Point(GetTabRect(i).Location.X - 2, GetTabRect(i).Location.Y - 2),
                            new Size(GetTabRect(i).Width + 3, GetTabRect(i).Height - 1));
                        g.FillRectangle(new SolidBrush(Color.FromArgb(246, 248, 252)), x2);
                        g.DrawLine(new Pen(Color.FromArgb(170, 187, 204)), new Point(x2.Right, x2.Top),
                            new Point(x2.Right, x2.Bottom));
                        if (ImageList != null)
                        {
                            try
                            {
                                g.DrawImage(ImageList.Images[TabPages[i].ImageIndex],
                                    new Point(x2.Location.X + 8, x2.Location.Y + 6));
                                g.DrawString("  " + TabPages[i].Text, Font, Brushes.DimGray, x2, new StringFormat
                                {
                                    LineAlignment = StringAlignment.Center,
                                    Alignment = StringAlignment.Center
                                });
                            }
                            catch (Exception)
                            {
                                g.DrawString(TabPages[i].Text, Font, Brushes.DimGray, x2, new StringFormat
                                {
                                    LineAlignment = StringAlignment.Center,
                                    Alignment = StringAlignment.Center
                                });
                            }
                        }
                        else
                        {
                            g.DrawString(TabPages[i].Text, Font, Brushes.DimGray, x2, new StringFormat
                            {
                                LineAlignment = StringAlignment.Center,
                                Alignment = StringAlignment.Center
                            });
                        }
                    }
                }
    
                e.Graphics.DrawImage(b, new Point(0, 0));
                g.Dispose();
                b.Dispose();
            }
        }
    }
    

    You can have fun making a dark theme to. In the photo Form2 uses the material skin. It can be found on NuGet as well.

提交回复
热议问题