none
Wie gesetzte StringAusrichtung auf Horizontal zurücksetzen ? RRS feed

  • Frage

  • Hallo,

    1. wie kann ich diesen gesetzten vertikalen Text-Ausrichtung ?:

    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;

    auf horizontalen zurücksetzen:

    drawFormat.FormatFlags = StringFormatFlags.DirectionHorizontal;

    2. TextAusrichtung um beliebigen Grad drehen ?

    MFG

    Freitag, 21. Dezember 2012 10:34

Antworten

  • Hallo,

    die Antwort erinnert mich an eine Grafik, die ich diese Tage gesehen habe:

    Wie soll ich's denn anders noch formulieren: Lass doch bitte das drawFormat weg! Dann wird der Text normal, d.h. horizontal gezeichnet. Mit "zurückschalten" ist nichts in der GDI+. Du mußt das Bild einfach neu zeichnen, indem Du [Graphics].DrawString() erneut aufrufst (mit genau der verlinkten Überladung). Hier ein letztes Beispiel:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private CheckBox checkBoxDrawVertical;
            private string stringToDraw = "Hallo Welt!";
            StringFormat formatVertical = new StringFormat() { FormatFlags = StringFormatFlags.DirectionVertical };
    
            public Form1()
            {
                InitializeComponent();
                InitializeComboBox();
            }
    
            private void InitializeComboBox()
            {
                this.checkBoxDrawVertical.AutoSize = true;
                this.checkBoxDrawVertical.Location = new System.Drawing.Point(19, 22);
                this.checkBoxDrawVertical.Name = "checkBoxVertical";
                this.checkBoxDrawVertical.Size = new System.Drawing.Size(80, 17);
                this.checkBoxDrawVertical.TabIndex = 0;
                this.checkBoxDrawVertical.Text = "Text vertikal ausrichten";
                this.checkBoxDrawVertical.UseVisualStyleBackColor = true;
                this.checkBoxDrawVertical.CheckedChanged += this.checkBoxVertical_CheckedChanged;
    
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                DrawText(e);
            }
    
            private void DrawText(PaintEventArgs e)
            {
                SizeF stringSize = e.Graphics.MeasureString(stringToDraw, Font);
    
                float x;
                float y;
    
                if (checkBoxDrawVertical.Checked)
                {
                    x = (this.ClientRectangle.Width - stringSize.Height) / 2f;
                    y = (this.ClientRectangle.Height - stringSize.Width) / 2f;
                    PointF point = new PointF(x, y);
    
                    e.Graphics.DrawString(stringToDraw, this.Font, Brushes.Navy, point, formatVertical);
                }
                else
                {
                    x = (this.ClientRectangle.Width - stringSize.Width) / 2f;
                    y = (this.ClientRectangle.Height - stringSize.Height) / 2f;
                    PointF point = new PointF(x, y);
    
                    e.Graphics.DrawString(stringToDraw, this.Font, Brushes.Navy, point);
                }
            }
    
    
            private void checkBoxVertical_CheckedChanged(object sender, EventArgs e)
            {
                Invalidate();
                Update();
            }
        }
    
    }

    Arbeite nicht zuviel. Schöne Feiertage.

    Marcel


    Montag, 24. Dezember 2012 09:33
    Moderator

Alle Antworten

  • Hallo,

    Über die Graphics.TranslateTransform()-Methode. Siehe auch die Antwort auf SO: How do I rotate a label in C#, wo Beispielcode für ein benutzerdefiniertes Label angezeigt wird. Im OnPaint()-Handler findest Du was dich interessiert.

    Gruß
    Marcel

    Freitag, 21. Dezember 2012 11:21
    Moderator
  • Hallo,

    bitte um sourcecode, ich kann wieder nicht zurück auf Horizontale Ausrichtung kommen.

    MFG

    Samstag, 22. Dezember 2012 10:11
  • Hallo,

    Was genau aus meinem Beitrag hast Du denn nicht verstanden? - Für Quellcode zu einem Label, welches Text in beliebigen Winkeln dreht, siehe den oben verlinkten SO-Artikel. Kompiliere den Code, ziehe das OrientedTextLabel-Control aus der Toolbox auf den Windows Forms Designer und stelle den gewünschten Winkel (RotationAngle) und die Drehrichtung (TextDirection) ein.

    Und wenn Du nicht über Graphics.TranslateTransform() arbeiten möchtest, lass einfach die vertikale Formatierung weg (ein StringFormatFlags.DirectionHorizontal gibt's nicht). Also statt:

    StringFormat formatVertical = new StringFormat() { FormatFlags = StringFormatFlags.DirectionVertical };
    e.Graphics.DrawString(stringToDraw, this.Font, Brushes.Navy, point, formatVertical);

    schreibst Du nur noch:

    e.Graphics.DrawString(stringToDraw, this.Font, Brushes.Navy, point);

    Alles klar soweit?

    Gruß
    Marcel

    Samstag, 22. Dezember 2012 11:38
    Moderator
  • Hallo,

    ich nutze folgendes:

    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
    g.DrawString("Week", new Font(new FontFamily("arial"), 15), Brushes.Black, new Rectangle(2, 71, 100, 120), drawFormat);

    und nach diesem Befehle wie kann ich zurückschalten auf Horizontal ?

    MFG

    Montag, 24. Dezember 2012 08:52
  • Hallo,

    die Antwort erinnert mich an eine Grafik, die ich diese Tage gesehen habe:

    Wie soll ich's denn anders noch formulieren: Lass doch bitte das drawFormat weg! Dann wird der Text normal, d.h. horizontal gezeichnet. Mit "zurückschalten" ist nichts in der GDI+. Du mußt das Bild einfach neu zeichnen, indem Du [Graphics].DrawString() erneut aufrufst (mit genau der verlinkten Überladung). Hier ein letztes Beispiel:

    using System;
    using System.Drawing;
    using System.Windows.Forms;
    
    namespace WindowsFormsApplication1
    {
        public partial class Form1 : Form
        {
            private CheckBox checkBoxDrawVertical;
            private string stringToDraw = "Hallo Welt!";
            StringFormat formatVertical = new StringFormat() { FormatFlags = StringFormatFlags.DirectionVertical };
    
            public Form1()
            {
                InitializeComponent();
                InitializeComboBox();
            }
    
            private void InitializeComboBox()
            {
                this.checkBoxDrawVertical.AutoSize = true;
                this.checkBoxDrawVertical.Location = new System.Drawing.Point(19, 22);
                this.checkBoxDrawVertical.Name = "checkBoxVertical";
                this.checkBoxDrawVertical.Size = new System.Drawing.Size(80, 17);
                this.checkBoxDrawVertical.TabIndex = 0;
                this.checkBoxDrawVertical.Text = "Text vertikal ausrichten";
                this.checkBoxDrawVertical.UseVisualStyleBackColor = true;
                this.checkBoxDrawVertical.CheckedChanged += this.checkBoxVertical_CheckedChanged;
    
            }
    
            protected override void OnPaint(PaintEventArgs e)
            {
                base.OnPaint(e);
                DrawText(e);
            }
    
            private void DrawText(PaintEventArgs e)
            {
                SizeF stringSize = e.Graphics.MeasureString(stringToDraw, Font);
    
                float x;
                float y;
    
                if (checkBoxDrawVertical.Checked)
                {
                    x = (this.ClientRectangle.Width - stringSize.Height) / 2f;
                    y = (this.ClientRectangle.Height - stringSize.Width) / 2f;
                    PointF point = new PointF(x, y);
    
                    e.Graphics.DrawString(stringToDraw, this.Font, Brushes.Navy, point, formatVertical);
                }
                else
                {
                    x = (this.ClientRectangle.Width - stringSize.Width) / 2f;
                    y = (this.ClientRectangle.Height - stringSize.Height) / 2f;
                    PointF point = new PointF(x, y);
    
                    e.Graphics.DrawString(stringToDraw, this.Font, Brushes.Navy, point);
                }
            }
    
    
            private void checkBoxVertical_CheckedChanged(object sender, EventArgs e)
            {
                Invalidate();
                Update();
            }
        }
    
    }

    Arbeite nicht zuviel. Schöne Feiertage.

    Marcel


    Montag, 24. Dezember 2012 09:33
    Moderator
  • Hallo ati.sah,

    Wenn Dir die Antwort von Marcel geholfen hat bitte markiere diese als Antwort.

    Vielen Dank,

    Ionut

    Freitag, 4. Januar 2013 14:25
    Moderator
  • Hallo ati.sah,

    Wir gehen davon aus, dass die Antwort Dir weitergeholfen hat.
    Wenn nein, neue Rückfragen oder Ergänzungen zu diesem Thread bleiben weiterhin möglich.

    Danke und viele Grüße,
    Ionut

    Dienstag, 8. Januar 2013 10:57
    Moderator