Laden...

Label um 90 / 180 / 270 Grad drehen

Erstellt von Kaladial vor 6 Jahren Letzter Beitrag vor 6 Jahren 2.214 Views
K
Kaladial Themenstarter:in
54 Beiträge seit 2017
vor 6 Jahren
Label um 90 / 180 / 270 Grad drehen

Hallo,

ich verzweifel grad mal wieder an beschreibungen anderer 😃
ich versuche ein dargestelltes label in meiner gui zu drehen.

ich hab dafür so ziehmlich jeden code von:
C# vertical label in a Windows Forms

ausprobiert und lande doch immer wieder beim selben ergebnis:
bei 90 ° sieht man den original text + nen fitzelchen von dem gedrehten
bei 180 ° sieht man beide texte (orginal + gedrehte)
bei 270 ° das selbe wie bei 90 °

was ich nicht hinbekomm ist: das man nur den gedrehten text sieht
geschweige denn das sich bei 90 oder 270 ° die größe des Labels anpasst.

using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace LabelDrehen.PinterObjects
{
    public class PrintObjectLabel : System.Windows.Forms.Label
    {
        int puid = 0;
        string ptype = "Text";
        string pfont = "";
        int protateangle = 0;
        string pnewText = "";
        public FormProps formProperties;

        #region properties region
        public int PoUid
        {
            get
            {
                return this.puid;
            }
            set
            {
                this.puid = value;
            }
        }

        public int PoRotateAngle
        {
            get
            {
                return this.protateangle;
            }
            set
            {
                this.protateangle = value;
            }
        }

        internal string PoType
        {
            get
            {
                return this.ptype;
            }
            set
            {
                this.ptype = value;
            }
        }

        internal string PoNewText
        {
            get
            {
                return this.pnewText;
            }
            set
            {
                this.pnewText = value;
            }
        }

        internal string PoText
        {
            get
            {
                return this.Text;
            }
            set
            {
                this.Text = value;
            }
        }

        internal string PoFont
        {
            get
            {
                return this.pfont;
            }
            set
            {
                this.pfont = value;
            }
        }

        internal Point PoPos
        {
            get
            {
                return this.Location;
            }
            set
            {
                this.Location = value;
            }
        }

        #endregion

        public PrintObjectLabel()
        {
            formProperties = new FormProps(this);
        }
        
        protected override void OnPaint(System.Windows.Forms.PaintEventArgs e)
        {
            string NewText = this.Text;
            if (this.PoRotateAngle != 0)
            {
                Graphics g = e.Graphics;

                StringFormat stringFormat = new StringFormat();
                stringFormat.Alignment = StringAlignment.Center;
                stringFormat.Trimming = StringTrimming.None;
                stringFormat.FormatFlags = StringFormatFlags.DirectionVertical;

                Brush textBrush = new SolidBrush(this.ForeColor);

                Matrix storedState = g.Transform;

                g.RotateTransform(this.PoRotateAngle);
                g.TranslateTransform(-ClientRectangle.Width, -ClientRectangle.Height);

                g.DrawString( this.Text, this.Font, textBrush, ClientRectangle, stringFormat);

                g.Transform = storedState;
            }
            base.OnPaint(e);
        }
    }
}


        private void addPrintObject()
        {
            curPrtObj = new PrintObjectLabel();
            curPrtObj.PoUid = pObjLabels.Count;
            curPrtObj.MouseDown += new MouseEventHandler(printObj_MouseDown);
            curPrtObj.Text = "Text or AI-Name";
            curPrtObj.Name = curPrtObj.Text;
            curPrtObj.AutoSize = true;
            curPrtObj.PoFont = "SF116";
            //_currentPrtObj.Font = new System.Drawing.Font("Microsoft Sans Serif", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            curPrtObj.Font = new System.Drawing.Font("OCRB", 14.25F, System.Drawing.FontStyle.Regular, System.Drawing.GraphicsUnit.Point, ((byte)(0)));
            curPrtObj.Location = new System.Drawing.Point(
                (20 + curPrtObj.PoUid * 20),
                (30 + curPrtObj.PoUid * 20)
            );

            ControlMover.Init(curPrtObj);

            if (curPrtObj.formProperties.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                _notSaved = true;
                pObjLabels.Add(curPrtObj);
                pLabel.Controls.Add(curPrtObj);
                pLabel.SuspendLayout();
            }
        }

wieso ist das so?

mfg Andreas

W
196 Beiträge seit 2008
vor 6 Jahren

was ich nicht hinbekomm ist: das man nur den gedrehten text sieht

Im überschriebenen OnPaint wird am Ende base.OnPaint aufgerufen. Damit wird die Standardzeichenroutine nach der Anzeige Deines eigenen Textes aufgerufen. Das ist falsch...

geschweige denn das sich bei 90 oder 270 ° die größe des Labels anpasst

Das geschieht nicht automatisch, dass musst Du selbst umsetzen... (Stichwort: MeasureString)

K
Kaladial Themenstarter:in
54 Beiträge seit 2017
vor 6 Jahren

hi

habs mittlerweile hinbekommen.
problem bestand darin das ich mein abgeleitetes label auf auto size hatte.

nachdem ich das geändert hab hatte ich noch ein paar darstellungsprobleme
mit der position die hab ich aber durch probieren hinbekommen.

zum schluss musste ich mir noch was wegen der aktuallisierung und neuzeichung
einfallen lassen aber auch das wurde per refresh gelöst.

mfg kala