Benutzer mit den meisten Antworten
Wie gesetzte StringAusrichtung auf Horizontal zurücksetzen ?

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
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
- Bearbeitet Marcel RomaModerator Montag, 24. Dezember 2012 09:37
- Als Antwort vorgeschlagen Ionut DumaModerator Freitag, 4. Januar 2013 14:24
- Als Antwort markiert Ionut DumaModerator Dienstag, 8. Januar 2013 10:58
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 -
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 -
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
-
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
- Bearbeitet Marcel RomaModerator Montag, 24. Dezember 2012 09:37
- Als Antwort vorgeschlagen Ionut DumaModerator Freitag, 4. Januar 2013 14:24
- Als Antwort markiert Ionut DumaModerator Dienstag, 8. Januar 2013 10:58