Modernes Tab Control

    • C#
    • .NET (FX) 4.5–4.8

    Es gibt 8 Antworten in diesem Thema. Der letzte Beitrag () ist von ErfinderDesRades.

      Modernes Tab Control

      Hallo Community,

      Ich habe hier den Source Code für ein
      TabControl im Windows 8 Style. Die Farben des Headers sind Anpassbar.
      Größe kann auch Festgelegt werden. Das Control Funktioniert nur, wenn
      die Tabs oben sind.

      C#-Quellcode

      1. public class TabControl : System.Windows.Forms.TabControl
      2. {
      3. public Color TabBackgroundColor { get; set; }
      4. public Color SelectedTabBackroundColor { get; set; }
      5. public Color BackgroundColor { get; set; }
      6. public Font TabFont { get; set; }
      7. public Color TabForeColor { get; set; }
      8. public TabControl() : base()
      9. {
      10. this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);
      11. this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
      12. this.ItemSize = new Size(150, 40);
      13. this.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
      14. Initial();
      15. }
      16. private void Initial()
      17. {
      18. this.TabBackgroundColor = Color.FromArgb(240, 240, 240);
      19. this.SelectedTabBackroundColor = Color.FromArgb(150, 253, 91);
      20. this.BackgroundColor = Color.White;
      21. this.TabForeColor = Color.Black;
      22. this.TabFont = new Font("Arial", 12);
      23. }
      24. protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
      25. {
      26. base.OnPaint(e);
      27. // Hintergrund Zeichnen
      28. e.Graphics.FillRectangle(new SolidBrush(this.BackgroundColor), new Rectangle(0,0,this.Width, this.Height));
      29. // Tabs Zeichnen
      30. for (int i = 0; i < base.TabPages.Count; i++)
      31. {
      32. if (i != this.SelectedIndex)
      33. {
      34. PaintTab(i, e.Graphics);
      35. }
      36. }
      37. // Gewähltes Tab Zeichnen
      38. this.PaintSelectedTab(e.Graphics);
      39. }
      40. private void PaintTab(int Index, Graphics e)
      41. {
      42. // Rechteck des Tabs
      43. Rectangle TabRect = base.GetTabRect(Index);
      44. // Abgedunkelte Farbe, für die Line
      45. Color TabLineColor =
      46. Color.FromArgb(this.TabBackgroundColor.A, this.TabBackgroundColor.R -
      47. 25, this.TabBackgroundColor.G - 25, this.TabBackgroundColor.B - 25);
      48. // Bild, auf das das Tab Gezeichnet wird
      49. Bitmap TabBmp = new Bitmap(TabRect.Width, TabRect.Height);
      50. // Graphics für das Zeichnen auf das Bild
      51. Graphics g = Graphics.FromImage(TabBmp);
      52. // Zeichnen des Oberen Rechteck
      53. g.FillRectangle(new SolidBrush(this.TabBackgroundColor), 2, 0, TabBmp.Width - 4, TabBmp.Height-10);
      54. // Zeichnen der Linie
      55. g.DrawLine(new Pen(new SolidBrush(TabLineColor), 2.5f), new
      56. Point(2, TabBmp.Height - 10), new Point(TabBmp.Width -2, TabBmp.Height -
      57. 10));
      58. // Zeichnen des String
      59. StringFormat sf = new StringFormat();
      60. sf.Alignment = StringAlignment.Center;
      61. sf.LineAlignment = StringAlignment.Center;
      62. g.DrawString(this.TabPages[Index].Text.ToUpper(),
      63. this.TabFont, new SolidBrush(this.TabForeColor), new Rectangle(2, 0,
      64. TabBmp.Width - 4, TabBmp.Height - 10), sf);
      65. // Zeichnen des Bildes auf die Tabpage
      66. e.DrawImage(TabBmp, TabRect);
      67. }
      68. private void PaintSelectedTab(Graphics e)
      69. {
      70. // Rechteck, des Gewählten Tab
      71. Rectangle TabRect = base.GetTabRect(this.SelectedIndex);
      72. // Abgedunkelte Farbe für Line
      73. Color TabLineColor =
      74. Color.FromArgb(this.SelectedTabBackroundColor.A,
      75. this.SelectedTabBackroundColor.R - 25, this.SelectedTabBackroundColor.G -
      76. 25, this.SelectedTabBackroundColor.B - 25);
      77. // Bild, auf das das Tab Gezeichnet wird
      78. Bitmap TabBmp = new Bitmap(TabRect.Width, TabRect.Height);
      79. // Graphics für das Zeichnen auf das Bild
      80. Graphics g = Graphics.FromImage(TabBmp);
      81. // Zeichnen des Oberen Rechteck
      82. g.FillRectangle(new
      83. SolidBrush(this.SelectedTabBackroundColor), new Rectangle(2, 0,
      84. TabBmp.Width - 4, TabBmp.Height - 10));
      85. // Zeichnen der Linie
      86. g.DrawLine(new Pen(new SolidBrush(TabLineColor), 2.5f), new
      87. Point(2, TabBmp.Height - 10), new Point(TabBmp.Width - 2, TabBmp.Height -
      88. 10));
      89. // Zeichnen des Dreieck
      90. g.FillPolygon(new SolidBrush(TabLineColor), new Point[] { new Point(20,
      91. TabBmp.Height - 10), new Point(40, TabBmp.Height - 10), new Point(30,
      92. TabBmp.Height) });
      93. // Zeichnen des String
      94. StringFormat sf = new StringFormat();
      95. sf.Alignment = StringAlignment.Center;
      96. sf.LineAlignment = StringAlignment.Center;
      97. g.DrawString(this.TabPages[this.SelectedIndex].Text.ToUpper(),
      98. this.TabFont, new SolidBrush(this.TabForeColor), new Rectangle(2, 0,
      99. TabBmp.Width - 4, TabBmp.Height - 10), sf);
      100. // TabBmp auf die Tabpage Zeichnen
      101. e.DrawImage(TabBmp, TabRect);
      102. }
      103. }



      Achtung, nach dem Ziehen auf die Form kommt ein Fehler. Bitte Form einmal Schließen und wieder öffnen und der Fehler ist weg


      Dieser Beitrag wurde bereits 2 mal editiert, zuletzt von „Andy16823“ ()

      Jap, vom Aussehen erstmal.

      Hab den Fehler auch. Hatte den Code in Visual Basic konvertiert, Klasse erstellt, einmal "gedebuggt" damit das Control in der Toolbox erscheint und dann auf die Form gezogen, dann bekam ich den Fehler.
      Polling is trolling!

      Achtung: Ich habe die komische Angewohnheit, simple Dinge zu verkomplizieren..
      Ja, sorry, dass ist wegen den Farbwerten. Habe den Kontruktor Vergessen.

      ----
      Habe den fehler behoben, allerdings kommt ein Weiterer Fehler, gegen den ich nichts machen kann. Einfach Drauf ziehen, Form einmal Schließen und wieder öffnen.

      Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Andy16823“ ()

      Hallo Andy,

      ich habe mir erlaubt, Dein Control ein bisschen zu modifizieren - der Fehler ist nun weg.
      Man könnte viel mehr optimieren, aber für den Anfang reicht es sicher - viel Spaß.

      C#-Quellcode

      1. using System.Drawing;
      2. using System.Windows.Forms;
      3. namespace simpelSoft
      4. {
      5. public class TabControlEx : System.Windows.Forms.TabControl
      6. {
      7. /// <summary>
      8. /// Properties
      9. /// </summary>
      10. public Color TabBackgroundColor { get; set; }
      11. public Color SelectedTabBackroundColor { get; set; }
      12. public Color BackgroundColor { get; set; }
      13. public Font TabFont { get; set; }
      14. public Color TabForeColor { get; set; }
      15. /// <summary>
      16. /// Constructor
      17. /// </summary>
      18. public TabControlEx()
      19. {
      20. // set styles
      21. SetStyle(ControlStyles.AllPaintingInWmPaint
      22. | ControlStyles.UserPaint
      23. | ControlStyles.OptimizedDoubleBuffer
      24. | ControlStyles.ResizeRedraw, true);
      25. this.ItemSize = new Size(150, 40);
      26. this.SizeMode = TabSizeMode.Fixed;
      27. this.TabBackgroundColor = Color.FromArgb(240, 240, 240);
      28. this.SelectedTabBackroundColor = Color.FromArgb(150, 253, 91);
      29. this.BackgroundColor = Color.White;
      30. this.TabForeColor = Color.Black;
      31. this.TabFont = new Font("Segoe UI", 12.0f, FontStyle.Regular, GraphicsUnit.Point);
      32. }
      33. /// <summary>
      34. /// Overridden paint event
      35. /// </summary>
      36. /// <param name="e"></param>
      37. protected override void OnPaint(PaintEventArgs e)
      38. {
      39. // System paint
      40. base.OnPaint(e);
      41. // Background
      42. e.Graphics.Clear(this.BackgroundColor);
      43. // Tabs
      44. for (int i = 0; i < base.TabPages.Count; i++)
      45. if (i != this.SelectedIndex)
      46. PaintTab(i, e.Graphics);
      47. // Draw selected tab
      48. this.PaintSelectedTab(e.Graphics);
      49. }
      50. /// <summary>
      51. /// Paint tab
      52. /// </summary>
      53. /// <param name="Index"></param>
      54. /// <param name="e"></param>
      55. private void PaintTab(int Index, Graphics e)
      56. {
      57. // Tab rectangle
      58. Rectangle TabRect = base.GetTabRect(Index);
      59. // Darker line color
      60. Color TabLineColor = this.TabBackgroundColor.Lerp(Color.Black, .2f);
      61. // Tab bitmap & graphics object
      62. Bitmap TabBmp = new Bitmap(TabRect.Width, TabRect.Height);
      63. Graphics g = Graphics.FromImage(TabBmp);
      64. // Top rectangle
      65. g.FillRectangle(new SolidBrush(this.TabBackgroundColor), 2, 0, TabBmp.Width - 4, TabBmp.Height - 10);
      66. // Line
      67. g.DrawLine(new Pen(new SolidBrush(TabLineColor), 2.5f), new Point(2, TabBmp.Height - 10), new Point(TabBmp.Width - 2, TabBmp.Height - 10));
      68. // String
      69. StringFormat sf = new StringFormat();
      70. sf.Alignment = StringAlignment.Center;
      71. sf.LineAlignment = StringAlignment.Center;
      72. g.DrawString(this.TabPages[Index].Text.ToUpper(), this.TabFont, new SolidBrush(this.TabForeColor), new Rectangle(2, 0, TabBmp.Width - 4, TabBmp.Height - 10), sf);
      73. // Draw Image on tabpage
      74. e.DrawImage(TabBmp, TabRect);
      75. }
      76. /// <summary>
      77. /// Paint selected tab
      78. /// </summary>
      79. /// <param name="e"></param>
      80. private void PaintSelectedTab(Graphics e)
      81. {
      82. // Index in range?
      83. if (this.SelectedIndex < 0)
      84. return;
      85. // Tab rectangle
      86. Rectangle TabRect = base.GetTabRect(this.SelectedIndex);
      87. // Darker line color
      88. Color TabLineColor = this.SelectedTabBackroundColor.Lerp(Color.Black, .2f);
      89. // Tab bitmap & graphics object
      90. Bitmap TabBmp = new Bitmap(TabRect.Width, TabRect.Height);
      91. Graphics g = Graphics.FromImage(TabBmp);
      92. // Top rectangle
      93. g.FillRectangle(new SolidBrush(this.SelectedTabBackroundColor), new Rectangle(2, 0, TabBmp.Width - 4, TabBmp.Height - 10));
      94. // Line
      95. g.DrawLine(new Pen(new SolidBrush(TabLineColor), 2.5f), new Point(2, TabBmp.Height - 10), new Point(TabBmp.Width - 2, TabBmp.Height - 10));
      96. // Polygon
      97. g.FillPolygon(new SolidBrush(TabLineColor), new Point[] { new Point(20, TabBmp.Height - 10), new Point(40, TabBmp.Height - 10), new Point(30, TabBmp.Height) });
      98. // String
      99. StringFormat sf = new StringFormat();
      100. sf.Alignment = StringAlignment.Center;
      101. sf.LineAlignment = StringAlignment.Center;
      102. g.DrawString(this.TabPages[this.SelectedIndex].Text.ToUpper(), this.TabFont, new SolidBrush(this.TabForeColor), new Rectangle(2, 0, TabBmp.Width - 4, TabBmp.Height - 10), sf);
      103. // Traw Image on tabpage
      104. e.DrawImage(TabBmp, TabRect);
      105. }
      106. }
      107. /// <summary>
      108. /// Lerp extensions ;)
      109. /// </summary>
      110. public static class Extensions
      111. {
      112. public static float Lerp(this float start, float end, float amount)
      113. {
      114. float difference = end - start;
      115. float adjusted = difference * amount;
      116. return start + adjusted;
      117. }
      118. public static Color Lerp(this Color color, Color to, float amount)
      119. {
      120. // Start color as 'lerpable' floats
      121. float sr = color.R, sg = color.G, sb = color.B;
      122. // End color as 'lerpable' floats
      123. float er = to.R, eg = to.G, eb = to.B;
      124. // Lerp the color to get the difference
      125. byte r = (byte)sr.Lerp(er, amount),
      126. g = (byte)sg.Lerp(eg, amount),
      127. b = (byte)sb.Lerp(eb, amount);
      128. // Return the new colour
      129. return Color.FromArgb(r, g, b);
      130. }
      131. }
      132. }
      Ruf' mal noch ​UpdateStyles auf, damit das Ganze sicher angewendet wird.
      #define for for(int z=0;z<2;++z)for // Have fun!
      Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

      Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!: