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.
Achtung, nach dem Ziehen auf die Form kommt ein Fehler. Bitte Form einmal Schließen und wieder öffnen und der Fehler ist weg
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
- public class TabControl : System.Windows.Forms.TabControl
- {
- public Color TabBackgroundColor { get; set; }
- public Color SelectedTabBackroundColor { get; set; }
- public Color BackgroundColor { get; set; }
- public Font TabFont { get; set; }
- public Color TabForeColor { get; set; }
- public TabControl() : base()
- {
- this.SetStyle(System.Windows.Forms.ControlStyles.OptimizedDoubleBuffer, true);
- this.SetStyle(System.Windows.Forms.ControlStyles.UserPaint, true);
- this.ItemSize = new Size(150, 40);
- this.SizeMode = System.Windows.Forms.TabSizeMode.Fixed;
- Initial();
- }
- private void Initial()
- {
- this.TabBackgroundColor = Color.FromArgb(240, 240, 240);
- this.SelectedTabBackroundColor = Color.FromArgb(150, 253, 91);
- this.BackgroundColor = Color.White;
- this.TabForeColor = Color.Black;
- this.TabFont = new Font("Arial", 12);
- }
- protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
- {
- base.OnPaint(e);
- // Hintergrund Zeichnen
- e.Graphics.FillRectangle(new SolidBrush(this.BackgroundColor), new Rectangle(0,0,this.Width, this.Height));
- // Tabs Zeichnen
- for (int i = 0; i < base.TabPages.Count; i++)
- {
- if (i != this.SelectedIndex)
- {
- PaintTab(i, e.Graphics);
- }
- }
- // Gewähltes Tab Zeichnen
- this.PaintSelectedTab(e.Graphics);
- }
- private void PaintTab(int Index, Graphics e)
- {
- // Rechteck des Tabs
- Rectangle TabRect = base.GetTabRect(Index);
- // Abgedunkelte Farbe, für die Line
- Color TabLineColor =
- Color.FromArgb(this.TabBackgroundColor.A, this.TabBackgroundColor.R -
- 25, this.TabBackgroundColor.G - 25, this.TabBackgroundColor.B - 25);
- // Bild, auf das das Tab Gezeichnet wird
- Bitmap TabBmp = new Bitmap(TabRect.Width, TabRect.Height);
- // Graphics für das Zeichnen auf das Bild
- Graphics g = Graphics.FromImage(TabBmp);
- // Zeichnen des Oberen Rechteck
- g.FillRectangle(new SolidBrush(this.TabBackgroundColor), 2, 0, TabBmp.Width - 4, TabBmp.Height-10);
- // Zeichnen der Linie
- g.DrawLine(new Pen(new SolidBrush(TabLineColor), 2.5f), new
- Point(2, TabBmp.Height - 10), new Point(TabBmp.Width -2, TabBmp.Height -
- 10));
- // Zeichnen des String
- StringFormat sf = new StringFormat();
- sf.Alignment = StringAlignment.Center;
- sf.LineAlignment = StringAlignment.Center;
- g.DrawString(this.TabPages[Index].Text.ToUpper(),
- this.TabFont, new SolidBrush(this.TabForeColor), new Rectangle(2, 0,
- TabBmp.Width - 4, TabBmp.Height - 10), sf);
- // Zeichnen des Bildes auf die Tabpage
- e.DrawImage(TabBmp, TabRect);
- }
- private void PaintSelectedTab(Graphics e)
- {
- // Rechteck, des Gewählten Tab
- Rectangle TabRect = base.GetTabRect(this.SelectedIndex);
- // Abgedunkelte Farbe für Line
- Color TabLineColor =
- Color.FromArgb(this.SelectedTabBackroundColor.A,
- this.SelectedTabBackroundColor.R - 25, this.SelectedTabBackroundColor.G -
- 25, this.SelectedTabBackroundColor.B - 25);
- // Bild, auf das das Tab Gezeichnet wird
- Bitmap TabBmp = new Bitmap(TabRect.Width, TabRect.Height);
- // Graphics für das Zeichnen auf das Bild
- Graphics g = Graphics.FromImage(TabBmp);
- // Zeichnen des Oberen Rechteck
- g.FillRectangle(new
- SolidBrush(this.SelectedTabBackroundColor), new Rectangle(2, 0,
- TabBmp.Width - 4, TabBmp.Height - 10));
- // Zeichnen der Linie
- g.DrawLine(new Pen(new SolidBrush(TabLineColor), 2.5f), new
- Point(2, TabBmp.Height - 10), new Point(TabBmp.Width - 2, TabBmp.Height -
- 10));
- // Zeichnen des Dreieck
- g.FillPolygon(new SolidBrush(TabLineColor), new Point[] { new Point(20,
- TabBmp.Height - 10), new Point(40, TabBmp.Height - 10), new Point(30,
- TabBmp.Height) });
- // Zeichnen des String
- StringFormat sf = new StringFormat();
- sf.Alignment = StringAlignment.Center;
- sf.LineAlignment = StringAlignment.Center;
- 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);
- // TabBmp auf die Tabpage Zeichnen
- e.DrawImage(TabBmp, TabRect);
- }
- }
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“ ()