Laden...

Länge eines strings und die Ermittlung der Breite

Erstellt von brev vor 12 Jahren Letzter Beitrag vor 12 Jahren 3.436 Views
Hinweis von talla vor 12 Jahren

Eine Bitte an alle Antwortenden: Achtet auf die verwendete Technologie. Der Thread war ursprünglich zwar nicht im richtigen Forum, aber WPF als verwendete Technologie war im Beitrag vom Threadersteller angegeben und daher sind alle Lösungsvorschläge die Windows Forms/GDI Methoden angeben, sehr nett gemeint, aber unpassend.

B
brev Themenstarter:in
135 Beiträge seit 2007
vor 12 Jahren
Länge eines strings und die Ermittlung der Breite

Hallol zusammen,
ich habe mal wieder ein Problem:
Ich habe einen string variabler Länge. Ich kenne die Schriftart und den Schrifttyp. Gibt es eine möglichkeit, zu ermitteln, wie breit dieser String in Pixel dann in? Das ganze soll in WPF geschehen...

Vielen Dank im Vorraus!

Gruss
brev

148 Beiträge seit 2006
vor 12 Jahren

Hallo brev..

hierzu gibt es zahlreiche Forenbeiträge.. das Stichwort wonach du suchen musst ist "MeasureString"..

greetz..

I cna tpye 300 wrods pre mnuite!

B
brev Themenstarter:in
135 Beiträge seit 2007
vor 12 Jahren

Hallo!
ich hab mal gegoogled.... geht ja auch wenn man weiss, was man sucht... ich bin auf folgendes gestossen:


public Size Measure(string text, double fontSize, string typeFace)

{

    FormattedText ft = new FormattedText(text, 

        CultureInfo.CurrentCulture,

        FlowDirection.LeftToRight,

        new Typeface(typeFace),

        fontSize,

        Brushes.Black);            

    return new Size(ft.Width, ft.Height);

}

Dabei ist mir jetzt aber nicht klar, was typeFace dabei bedeutet.... Wie müsste ich es z.b. umsetzen, wenn ich Arial als Schriftart haben will?

Danke im Vorraus!

Gruss
brev

2.207 Beiträge seit 2011
vor 12 Jahren

Hallo brev,

man kann ja auch nach "TypeFace c#" googlen 😃

die MSDN sagt dazu das hier:

TypeFace-Klasse

Gruss

Coffeebean

916 Beiträge seit 2008
vor 12 Jahren

Hi brev,

ich nehm dazu immer die API von GDI32 zur Hilfe. Hier am Beispiel der Breite des Strings.


[DllImport("gdi32.dll")]
private static extern bool GetTextExtentPoint(IntPtr hdc, string lpString, int cbString, ref Size lpSize);

/// <summary>
/// Gets the width of the string by parsing the bitmap file.
/// </summary>
/// <param name="graphics">The graphics object to determine the width of the string.</param>
/// <returns>The width of the string in pixel.</returns>
private int GetStringWidth(Graphics graphics)
{
    return MeasureText(graphics, this.Text, this.Font).Width;
}

/// <summary>
/// Measures the text and return the size.
/// </summary>
/// <param name="graphics">The graphics.</param>
/// <param name="text">The text of the label.</param>
/// <param name="font">The font of the label.</param>
/// <returns>The size object to indicate the width of the string.</returns>
public static Size MeasureText(Graphics graphics, string text, Font font)
{
    if (graphics == null)
    {
        throw new ArgumentNullException("graphics");
    }
    if (text == null)
    {
        throw new ArgumentNullException("text");
    }
    if (font == null)
    {
        throw new ArgumentNullException("font");
    }
    
    // get the Graphics-Handle
    IntPtr hdc = graphics.GetHdc();
    // set the font
    IntPtr fontOld = Gdi32.SelectObject(hdc, font.ToHfont());

    Size measureSize = Size.Empty;
    GetTextExtentPoint(hdc, text, text.Length, ref measureSize);
    // delete handles
    Gdi32.DeleteObject(Gdi32.SelectObject(hdc, fontOld));
    graphics.ReleaseHdc(hdc);

    return measureSize;
}

.....
.....
float width = this.GetStringWidth(e.Graphics);

Hier in dem Beispiel ist Text und Font innerhalb der Klasse als Member deklariert. Wie du das bei dir machst ist dir überlassen!

Again what learned...