Per Mausklick bewerten und Feedback geben
MSDN
MSDN Library
.NET Entwicklung
.NET Framework 3.5
.NET Framework 3,5
TextRenderer-Klasse
 MeasureText-Methode (IDeviceContext...
Alle reduzieren/Alle erweitern Alle reduzieren
Diese Seite ist spezifisch für
Microsoft Visual Studio 2008/.NET Framework 3.5

Andere Versionen stehen ebenfalls zur Verfügung für:
.NET Framework-Klassenbibliothek
TextRenderer..::.MeasureText-Methode (IDeviceContext, String, Font, Size)

Aktualisiert: November 2007

Gibt die Größe des angegebenen Texts in Pixel an, wenn dieser mit der angegebenen Schriftart im angegebenen Gerätekontext gezeichnet wird und dabei mithilfe der angegebenen Größe ein anfängliches umgebendes Rechteck für den Text erstellt wird.

Namespace:  System.Windows.Forms
Assembly:  System.Windows.Forms (in System.Windows.Forms.dll)
Visual Basic (Deklaration)
Public Shared Function MeasureText ( _
    dc As IDeviceContext, _
    text As String, _
    font As Font, _
    proposedSize As Size _
) As Size
Visual Basic (Verwendung)
Dim dc As IDeviceContext
Dim text As String
Dim font As Font
Dim proposedSize As Size
Dim returnValue As Size

returnValue = TextRenderer.MeasureText(dc, _
    text, font, proposedSize)
C#
public static Size MeasureText(
    IDeviceContext dc,
    string text,
    Font font,
    Size proposedSize
)
VisualC++
public:
static Size MeasureText(
    IDeviceContext^ dc, 
    String^ text, 
    Font^ font, 
    Size proposedSize
)
J#
public static Size MeasureText(
    IDeviceContext dc,
    String text,
    Font font,
    Size proposedSize
)
Jscript
public static function MeasureText(
    dc : IDeviceContext, 
    text : String, 
    font : Font, 
    proposedSize : Size
) : Size

Parameter

dc
Typ: System.Drawing..::.IDeviceContext
Der Gerätekontext, in dem der Text bemessen werden soll.
text
Typ: System..::.String
Der zu bemessende Text.
font
Typ: System.Drawing..::.Font
Der Font, der auf den bemessenen Text angewendet werden soll.
proposedSize
Typ: System.Drawing..::.Size
Die Size des anfänglichen umgebenden Rechtecks.

Rückgabewert

Typ: System.Drawing..::.Size
Die Size des mit dem angegebenen font gezeichneten text in Pixel.
AusnahmeBedingung
ArgumentNullException

dc ist nullNothingnullptrNULL-Verweis (Nothing in Visual Basic).

Die MeasureText-Methode gibt mithilfe des proposedSize-Parameters beim Bestimmen der Textgröße das Verhältnis von Höhe zu Breite an. Wenn beim Bemessen des Texts in einer einzelnen Zeile der proposedSize-Parameter eine Size darstellt, bei der eine Höhenabmessung größer als Int32..::.MaxValue ist, wird die zurückgegebene Size so angepasst, dass sie die tatsächliche Texthöhe wiedergibt.

Im folgenden Codebeispiel wird die Verwendung einer der MeasureText-Methoden veranschaulicht. Fügen Sie zum Ausführen des Beispiels den Code in ein Windows Form ein, und rufen Sie DrawALineOfText im Paint-Ereignishandler des Formulars auf, wobei Sie e als PaintEventArgs übergeben.

Visual Basic
Private Sub DrawALineOfText(ByVal e As PaintEventArgs)
    ' Declare strings to render on the form.
    Dim stringsToPaint() As String = {"Tail", "Spin", " Toys"}

    ' Declare fonts for rendering the strings.
    Dim fonts() As Font = {New Font("Arial", 14, FontStyle.Regular), _
        New Font("Arial", 14, FontStyle.Italic), _
        New Font("Arial", 14, FontStyle.Regular)}

    Dim startPoint As New Point(10, 10)

    ' Set TextFormatFlags to no padding so strings are drawn together.
    Dim flags As TextFormatFlags = TextFormatFlags.NoPadding

    ' Declare a proposed size with dimensions set to the maximum integer value.
    Dim proposedSize As Size = New Size(Integer.MaxValue, Integer.MaxValue)

    ' Measure each string with its font and NoPadding value and draw it to the form.
    For i As Integer = 0 To stringsToPaint.Length - 1
        Dim size As Size = TextRenderer.MeasureText(e.Graphics, _
            stringsToPaint(i), fonts(i), proposedSize, flags)
        Dim rect As Rectangle = New Rectangle(startPoint, size)
        TextRenderer.DrawText(e.Graphics, stringsToPaint(i), fonts(i), _
            startPoint, Color.Black, flags)
        startPoint.X += size.Width
    Next
End Sub
C#
private static void DrawALineOfText(PaintEventArgs e)
{
    // Declare strings to render on the form.
    string[] stringsToPaint = { "Tail", "Spin", " Toys" };

    // Declare fonts for rendering the strings.
    Font[] fonts = { new Font("Arial", 14, FontStyle.Regular), 
        new Font("Arial", 14, FontStyle.Italic), 
        new Font("Arial", 14, FontStyle.Regular) };

    Point startPoint = new Point(10, 10);

    // Set TextFormatFlags to no padding so strings are drawn together.
    TextFormatFlags flags = TextFormatFlags.NoPadding;

    // Declare a proposed size with dimensions set to the maximum integer value.
    Size proposedSize = new Size(int.MaxValue, int.MaxValue);

    // Measure each string with its font and NoPadding value and 
    // draw it to the form.
    for (int i = 0; i < stringsToPaint.Length; i++)
    {
        Size size = TextRenderer.MeasureText(e.Graphics, stringsToPaint[i], 
            fonts[i], proposedSize, flags);
        Rectangle rect = new Rectangle(startPoint, size);
        TextRenderer.DrawText(e.Graphics, stringsToPaint[i], fonts[i],
            startPoint, Color.Black, flags);
        startPoint.X += size.Width;
    }

}

Windows Vista, Windows XP SP2, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP Starter Edition, Windows Server 2003, Windows Server 2000 SP4, Windows Millennium Edition, Windows 98

.NET Framework und .NET Compact Framework unterstützen nicht alle Versionen sämtlicher Plattformen. Eine Liste der unterstützten Versionen finden Sie unter Systemanforderungen für .NET Framework.

.NET Framework

Unterstützt in: 3.5, 3.0, 2.0
Communityinhalt   Was ist Community Content?
Neuen Inhalt hinzufügen RSS  Anmerkungen
Processing
© 2012 Microsoft. Alle Rechte vorbehalten. Nutzungsbedingungen | Markenzeichen | Informationen zur Datensicherheit
Page view tracker