Beim scrollen zu RotatingLabel springt das Programm nach oben
-
Hallo,
ich habe folgenden Code:
Form1.csusing System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Windows.Forms; namespace Test_RotatingLabel { public partial class Form1 : Form { RotatingLabel l1, l2, l3, l4, l5, l6, l7, l8; public Form1() { InitializeComponent(); } private void Form1_Load(object sender, EventArgs e) { l1 = new RotatingLabel(); l1.NewText = "test1"; l1.RotateAngle = -90; l1.Location = new Point(20,100); l1.Visible = false; l2 = new RotatingLabel(); l2.NewText = "test2"; l2.RotateAngle = -90; l2.Location = new Point(20, 500); l2.Visible = false; l3 = new RotatingLabel(); l3.NewText = "test3"; l3.RotateAngle = -90; l3.Location = new Point(20, 1000); l3.Visible = false; l4 = new RotatingLabel(); l4.NewText = "test4"; l4.RotateAngle = -90; l4.Location = new Point(20, 1500); l4.Visible = false; l5 = new RotatingLabel(); l5.NewText = "test5"; l5.RotateAngle = -90; l5.Location = new Point(20, 2000); l5.Visible = false; l6 = new RotatingLabel(); l6.NewText = "test6"; l6.RotateAngle = -90; l6.Location = new Point(20, 2500); l6.Visible = false; l7 = new RotatingLabel(); l7.NewText = "test7"; l7.RotateAngle = -90; l7.Location = new Point(20, 3000); l7.Visible = false; l8 = new RotatingLabel(); l8.NewText = "test8"; l8.RotateAngle = -90; l8.Location = new Point(20, 3500); l8.Visible = false; this.Controls.Add(l1); this.Controls.Add(l2); this.Controls.Add(l3); this.Controls.Add(l4); this.Controls.Add(l5); this.Controls.Add(l6); this.Controls.Add(l7); this.Controls.Add(l8); this.WindowState = FormWindowState.Maximized; } private void button1_Click(object sender, EventArgs e) { l1.Visible = true; l2.Visible = true; l3.Visible = true; l4.Visible = true; l5.Visible = true; l6.Visible = true; l7.Visible = true; l8.Visible = true; } } }
RotatingLabel.cs
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Windows.Forms; using System.Drawing; namespace Test_RotatingLabel { public class RotatingLabel : System.Windows.Forms.Label { private int m_RotateAngle = 0; private string m_NewText = string.Empty; public int RotateAngle { get { return m_RotateAngle; } set { m_RotateAngle = value; Invalidate(); } } public string NewText { get { return m_NewText; } set { m_NewText = value; Invalidate(); } } protected override void OnPaint(System.Windows.Forms.PaintEventArgs e) { Func<double, double> DegToRad = (angle) => Math.PI * angle / 180.0; Brush b = new SolidBrush(this.ForeColor); SizeF size = e.Graphics.MeasureString(this.NewText, this.Font, this.Parent.Width); int normalAngle = ((RotateAngle % 360) + 360) % 360; double normaleRads = DegToRad(normalAngle); int hSinTheta = (int)Math.Ceiling((size.Height * Math.Sin(normaleRads))); int wCosTheta = (int)Math.Ceiling((size.Width * Math.Cos(normaleRads))); int wSinTheta = (int)Math.Ceiling((size.Width * Math.Sin(normaleRads))); int hCosTheta = (int)Math.Ceiling((size.Height * Math.Cos(normaleRads))); int rotatedWidth = Math.Abs(hSinTheta) + Math.Abs(wCosTheta); int rotatedHeight = Math.Abs(wSinTheta) + Math.Abs(hCosTheta); this.Width = rotatedWidth; this.Height = rotatedHeight; int numQuadrants = (normalAngle >= 0 && normalAngle < 90) ? 1 : (normalAngle >= 90 && normalAngle < 180) ? 2 : (normalAngle >= 180 && normalAngle < 270) ? 3 : (normalAngle >= 270 && normalAngle < 360) ? 4 : 0; int horizShift = 0; int vertShift = 0; if (numQuadrants == 1) { horizShift = Math.Abs(hSinTheta); } else if (numQuadrants == 2) { horizShift = rotatedWidth; vertShift = Math.Abs(hCosTheta); } else if (numQuadrants == 3) { horizShift = Math.Abs(wCosTheta); vertShift = rotatedHeight; } else if (numQuadrants == 4) { vertShift = Math.Abs(wSinTheta); } e.Graphics.TranslateTransform(horizShift, vertShift); e.Graphics.RotateTransform(this.RotateAngle); e.Graphics.DrawString(this.NewText, this.Font, b, 0f, 0f); base.OnPaint(e); } } }
Wird das Programm geladen werden die Labels positioniert aber Visable auf false gesetzt.
Erst beim drücken des einen Buttons, werden diese auf Visable true gesetzt.
Jetzt wenn ich im Programm nach unten scrolle springt das Programm jedesmal, sobald er zu einem noch nicht sichtbaren Label kommt nach oben.
Wäre super wenn dies mal einer testen könnte und mir einen Tipp gibt wie ich das ganze Verhalten "normalisieren" kann.Da die OnPaint-Methode ja von Label überschrieben wurde, könnte ich mir vorstellen das dort der Hund begraben ist. Aber die Klasse habe ich aus dem Internet kopiert und soweit bin ich noch nicht, dass ich den Fehler selbst sehen könnte.
Gruß Gustl
-
Also, mal zu der Leserlichkeit deines codes, mach doch sowas hier:
public void Form1_Load(object sender, EventArgs e) { createRoatingLabel(l1, "test1", -90, 20, 100, false); createRoatingLabel(l2, "test2", -90, 20, 500, false); createRoatingLabel(l3, "test3", -90, 20, 1000, false); createRoatingLabel(l4, "test4", -90, 20, 1500, false); createRoatingLabel(l5, "test5", -90, 20, 2000, false); createRoatingLabel(l6, "test6", -90, 20, 2500, false); createRoatingLabel(l7, "test7", -90, 20, 3000, false); createRoatingLabel(l8, "test8", -90, 20, 3500, false); this.WindowState = FormWindowState.Maximized; } private void createRoatingLabel(RotatingLabel rotatingLabel, string text, int rotateAngle, int x, int y, bool visibility) { rotatingLabel = new RotatingLabel { NewText = text, RotateAngle = rotateAngle, Location = new Point(x, y), Visible = visibility }; Controls.Add(rotatingLabel); }
EDIT: Hmm... scheint das er immer wieder zum button scrollt.
Wenn du diesen also nach dem klick entfernen würdest, solltest du das Problem nicht mehr haben.
Weiss natürlich nicht ob du den noch brauchst oder nicht^^.