Graphics.DrawString 方法

定义

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

重载

DrawString(String, Font, Brush, Single, Single, StringFormat)

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

DrawString(ReadOnlySpan<Char>, Font, Brush, Single, Single, StringFormat)

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

DrawString(String, Font, Brush, Single, Single)

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

DrawString(String, Font, Brush, RectangleF, StringFormat)

使用指定 Brush 指定矩形中的指定文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

DrawString(String, Font, Brush, PointF, StringFormat)

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

DrawString(ReadOnlySpan<Char>, Font, Brush, Single, Single)

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

DrawString(ReadOnlySpan<Char>, Font, Brush, PointF, StringFormat)

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

DrawString(String, Font, Brush, RectangleF)

使用指定的 BrushFont 对象在指定的矩形中绘制指定的文本字符串。

DrawString(String, Font, Brush, PointF)

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

DrawString(ReadOnlySpan<Char>, Font, Brush, RectangleF)

使用指定的 BrushFont 对象在指定的矩形中绘制指定的文本字符串。

DrawString(ReadOnlySpan<Char>, Font, Brush, PointF)

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

DrawString(ReadOnlySpan<Char>, Font, Brush, RectangleF, StringFormat)

使用指定 Brush 指定矩形中的指定文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

DrawString(String, Font, Brush, Single, Single, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

public:
 void DrawString(System::String ^ s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, float x, float y, System::Drawing::StringFormat ^ format);
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y, System.Drawing.StringFormat? format);
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y, System.Drawing.StringFormat format);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * single * single * System.Drawing.StringFormat -> unit
Public Sub DrawString (s As String, font As Font, brush As Brush, x As Single, y As Single, format As StringFormat)

参数

s
String

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

x
Single

绘制文本左上角的 x 坐标。

y
Single

绘制文本左上角的 y 坐标。

format
StringFormat

StringFormat,用于指定应用于绘制文本的格式属性,例如行距和对齐方式。

例外

brush null

-或-

s null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建要绘制的文本字符串。

  • 将字体定义为 Arial (16pt)。

  • 创建要绘制的纯色黑色画笔。

  • 为绘制文本的左上角创建点的坐标。

  • 设置要垂直绘制的字符串的格式

  • 使用字体、画笔、目标点和格式将字符串绘制到屏幕。

public:
   void DrawStringFloatFormat( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black );

      // Create point for upper-left corner of drawing.
      float x = 150.0F;
      float y = 50.0F;

      // Set format of string.
      StringFormat^ drawFormat = gcnew StringFormat;
      drawFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, x, y, drawFormat );
   }
public void DrawStringFloatFormat(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create point for upper-left corner of drawing.
    float x = 150.0F;
    float y =  50.0F;
             
    // Set format of string.
    StringFormat drawFormat = new StringFormat();
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y, drawFormat);
}
Public Sub DrawStringFloatFormat(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create point for upper-left corner of drawing.
    Dim x As Single = 150.0F
    Dim y As Single = 50.0F

    ' Set format of string.
    Dim drawFormat As New StringFormat
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, _
    x, y, drawFormat)
End Sub

另请参阅

适用于

DrawString(ReadOnlySpan<Char>, Font, Brush, Single, Single, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

public:
 void DrawString(ReadOnlySpan<char> s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, float x, float y, System::Drawing::StringFormat ^ format);
public void DrawString (ReadOnlySpan<char> s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y, System.Drawing.StringFormat? format);
member this.DrawString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Brush * single * single * System.Drawing.StringFormat -> unit
Public Sub DrawString (s As ReadOnlySpan(Of Char), font As Font, brush As Brush, x As Single, y As Single, format As StringFormat)

参数

s
ReadOnlySpan<Char>

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

x
Single

绘制文本左上角的 x 坐标。

y
Single

绘制文本左上角的 y 坐标。

format
StringFormat

StringFormat,用于指定应用于绘制文本的格式属性,例如行距和对齐方式。

适用于

DrawString(String, Font, Brush, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

public:
 void DrawString(System::String ^ s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, float x, float y);
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y);
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * single * single -> unit
Public Sub DrawString (s As String, font As Font, brush As Brush, x As Single, y As Single)

参数

s
String

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

x
Single

绘制文本左上角的 x 坐标。

y
Single

绘制文本左上角的 y 坐标。

例外

brush null

-或-

s null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建要绘制的文本字符串。

  • 将字体定义为 Arial (16pt)。

  • 创建一个要绘制的实心黑色画笔。

  • 为要在其中绘制文本的左上角创建一个点。

  • 使用字体、画笔和目标点将字符串绘制到屏幕。

public:
   void DrawStringFloat( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black );

      // Create point for upper-left corner of drawing.
      float x = 150.0F;
      float y = 150.0F;

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, x, y );
   }
public void DrawStringFloat(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create point for upper-left corner of drawing.
    float x = 150.0F;
    float y = 150.0F;
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y);
}
Public Sub DrawStringFloat(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create point for upper-left corner of drawing.
    Dim x As Single = 150.0F
    Dim y As Single = 150.0F

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, x, y)
End Sub

另请参阅

适用于

DrawString(String, Font, Brush, RectangleF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定 Brush 指定矩形中的指定文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

public:
 void DrawString(System::String ^ s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::RectangleF layoutRectangle, System::Drawing::StringFormat ^ format);
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle, System.Drawing.StringFormat? format);
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle, System.Drawing.StringFormat format);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.RectangleF * System.Drawing.StringFormat -> unit
Public Sub DrawString (s As String, font As Font, brush As Brush, layoutRectangle As RectangleF, format As StringFormat)

参数

s
String

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

layoutRectangle
RectangleF

RectangleF 结构,用于指定绘制文本的位置。

format
StringFormat

StringFormat,用于指定应用于绘制文本的格式属性,例如行距和对齐方式。

例外

brush null

-或-

s null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建要绘制的文本字符串。

  • 将字体定义为 Arial (16pt)。

  • 创建要绘制的纯色黑色画笔。

  • 创建一个要在其中绘制文本的矩形。

  • 将矩形绘制到屏幕。

  • 设置字符串的格式以在矩形中居中。

  • 使用字体、画笔和目标矩形将字符串绘制到屏幕。

public:
   void DrawStringRectangleFFormat( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black );

      // Create rectangle for drawing.
      float x = 150.0F;
      float y = 150.0F;
      float width = 200.0F;
      float height = 50.0F;
      RectangleF drawRect = RectangleF(x,y,width,height);

      // Draw rectangle to screen.
      Pen^ blackPen = gcnew Pen( Color::Black );
      e->Graphics->DrawRectangle( blackPen, x, y, width, height );

      // Set format of string.
      StringFormat^ drawFormat = gcnew StringFormat;
      drawFormat->Alignment = StringAlignment::Center;

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, drawRect, drawFormat );
   }
public void DrawStringRectangleFFormat(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create rectangle for drawing.
    float x = 150.0F;
    float y = 150.0F;
    float width = 200.0F;
    float height = 50.0F;
    RectangleF drawRect = new RectangleF(x, y, width, height);
             
    // Draw rectangle to screen.
    Pen blackPen = new Pen(Color.Black);
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
             
    // Set format of string.
    StringFormat drawFormat = new StringFormat();
    drawFormat.Alignment = StringAlignment.Center;
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect, drawFormat);
}
Public Sub DrawStringRectangleFFormat(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create rectangle for drawing.
    Dim x As Single = 150.0F
    Dim y As Single = 150.0F
    Dim width As Single = 200.0F
    Dim height As Single = 50.0F
    Dim drawRect As New RectangleF(x, y, width, height)

    ' Draw rectangle to screen.
    Dim blackPen As New Pen(Color.Black)
    e.Graphics.DrawRectangle(blackPen, x, y, width, height)

    ' Set format of string.
    Dim drawFormat As New StringFormat
    drawFormat.Alignment = StringAlignment.Center

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, _
    drawRect, drawFormat)
End Sub

注解

s 参数表示的文本在由 layoutRectangle 参数表示的矩形内绘制。 如果文本不适合矩形内,则它将被截断为最接近的单词,除非使用 format 参数指定。

另请参阅

适用于

DrawString(String, Font, Brush, PointF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

public:
 void DrawString(System::String ^ s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::PointF point, System::Drawing::StringFormat ^ format);
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point, System.Drawing.StringFormat? format);
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point, System.Drawing.StringFormat format);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.PointF * System.Drawing.StringFormat -> unit
Public Sub DrawString (s As String, font As Font, brush As Brush, point As PointF, format As StringFormat)

参数

s
String

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

point
PointF

PointF 指定绘制文本左上角的结构。

format
StringFormat

StringFormat,用于指定应用于绘制文本的格式属性,例如行距和对齐方式。

例外

brush null

-或-

s null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建要绘制的文本字符串。

  • 将字体定义为 Arial (16pt)。

  • 创建要绘制的纯色黑色画笔。

  • 为要在其中绘制文本的左上角创建一个点。

  • 设置要垂直绘制的字符串的格式。

  • 使用字体、画笔、目标点和格式将字符串绘制到屏幕。

public:
   void DrawStringPointFFormat( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black );

      // Create point for upper-left corner of drawing.
      PointF drawPoint = PointF(150.0F,50.0F);

      // Set format of string.
      StringFormat^ drawFormat = gcnew StringFormat;
      drawFormat->FormatFlags = StringFormatFlags::DirectionVertical;

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, drawPoint, drawFormat );
   }
public void DrawStringPointFFormat(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create point for upper-left corner of drawing.
    PointF drawPoint = new PointF(150.0F, 50.0F);
             
    // Set format of string.
    StringFormat drawFormat = new StringFormat();
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical;
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint, drawFormat);
}
Public Sub DrawStringPointFFormat(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create point for upper-left corner of drawing.
    Dim drawPoint As New PointF(150.0F, 50.0F)

    ' Set format of string.
    Dim drawFormat As New StringFormat
    drawFormat.FormatFlags = StringFormatFlags.DirectionVertical

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, _
    drawPoint, drawFormat)
End Sub

另请参阅

适用于

DrawString(ReadOnlySpan<Char>, Font, Brush, Single, Single)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

public:
 void DrawString(ReadOnlySpan<char> s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, float x, float y);
public void DrawString (ReadOnlySpan<char> s, System.Drawing.Font font, System.Drawing.Brush brush, float x, float y);
member this.DrawString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Brush * single * single -> unit
Public Sub DrawString (s As ReadOnlySpan(Of Char), font As Font, brush As Brush, x As Single, y As Single)

参数

s
ReadOnlySpan<Char>

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

x
Single

绘制文本左上角的 x 坐标。

y
Single

绘制文本左上角的 y 坐标。

适用于

DrawString(ReadOnlySpan<Char>, Font, Brush, PointF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定 Brush 指定位置绘制指定的文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

public:
 void DrawString(ReadOnlySpan<char> s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::PointF point, System::Drawing::StringFormat ^ format);
public void DrawString (ReadOnlySpan<char> s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point, System.Drawing.StringFormat? format);
member this.DrawString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Brush * System.Drawing.PointF * System.Drawing.StringFormat -> unit
Public Sub DrawString (s As ReadOnlySpan(Of Char), font As Font, brush As Brush, point As PointF, format As StringFormat)

参数

s
ReadOnlySpan<Char>

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

point
PointF

PointF 指定绘制文本左上角的结构。

format
StringFormat

StringFormat,用于指定应用于绘制文本的格式属性,例如行距和对齐方式。

适用于

DrawString(String, Font, Brush, RectangleF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定的 BrushFont 对象在指定的矩形中绘制指定的文本字符串。

public:
 void DrawString(System::String ^ s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::RectangleF layoutRectangle);
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle);
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.RectangleF -> unit
Public Sub DrawString (s As String, font As Font, brush As Brush, layoutRectangle As RectangleF)

参数

s
String

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

layoutRectangle
RectangleF

RectangleF 结构,用于指定绘制文本的位置。

例外

brush null

-或-

s null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建要绘制的文本字符串。

  • 将字体定义为 Arial (16pt)。

  • 创建要绘制的纯色黑色画笔。

  • 创建一个要在其中绘制文本的矩形。

  • 将矩形绘制到屏幕。

  • 使用字体、画笔和目标矩形将字符串绘制到屏幕。

public:
   void DrawStringRectangleF( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black );

      // Create rectangle for drawing.
      float x = 150.0F;
      float y = 150.0F;
      float width = 200.0F;
      float height = 50.0F;
      RectangleF drawRect = RectangleF(x,y,width,height);

      // Draw rectangle to screen.
      Pen^ blackPen = gcnew Pen( Color::Black );
      e->Graphics->DrawRectangle( blackPen, x, y, width, height );

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, drawRect );
   }
public void DrawStringRectangleF(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create rectangle for drawing.
    float x = 150.0F;
    float y = 150.0F;
    float width = 200.0F;
    float height = 50.0F;
    RectangleF drawRect = new RectangleF(x, y, width, height);
             
    // Draw rectangle to screen.
    Pen blackPen = new Pen(Color.Black);
    e.Graphics.DrawRectangle(blackPen, x, y, width, height);
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect);
}
Public Sub DrawStringRectangleF(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create rectangle for drawing.
    Dim x As Single = 150.0F
    Dim y As Single = 150.0F
    Dim width As Single = 200.0F
    Dim height As Single = 50.0F
    Dim drawRect As New RectangleF(x, y, width, height)

    ' Draw rectangle to screen.
    Dim blackPen As New Pen(Color.Black)
    e.Graphics.DrawRectangle(blackPen, x, y, width, height)

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawRect)
End Sub

注解

s 参数表示的文本在由 layoutRectangle 参数表示的矩形内绘制。 如果文本不适合矩形内,则会在最近的单词处截断文本。 若要进一步操作矩形内绘制字符串的方式,请使用采用 StringFormatDrawString 重载。

另请参阅

适用于

DrawString(String, Font, Brush, PointF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

public:
 void DrawString(System::String ^ s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::PointF point);
public void DrawString (string? s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point);
public void DrawString (string s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.PointF -> unit
Public Sub DrawString (s As String, font As Font, brush As Brush, point As PointF)

参数

s
String

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

point
PointF

PointF 指定绘制文本左上角的结构。

例外

brush null

-或-

s null

示例

下面的代码示例设计用于 Windows 窗体,它需要 PaintEventArgse,这是 Paint 事件处理程序的参数。 该代码执行以下操作:

  • 创建要绘制的文本字符串。

  • 将字体定义为 Arial (16pt)。

  • 创建要绘制的纯色黑色画笔。

  • 为要在其中绘制文本的左上角创建一个点。

  • 使用字体、画笔和目标点将字符串绘制到屏幕。

public:
   void DrawStringPointF( PaintEventArgs^ e )
   {
      // Create string to draw.
      String^ drawString = "Sample Text";

      // Create font and brush.
      System::Drawing::Font^ drawFont = gcnew System::Drawing::Font( "Arial",16 );
      SolidBrush^ drawBrush = gcnew SolidBrush( Color::Black );

      // Create point for upper-left corner of drawing.
      PointF drawPoint = PointF(150.0F,150.0F);

      // Draw string to screen.
      e->Graphics->DrawString( drawString, drawFont, drawBrush, drawPoint );
   }
public void DrawStringPointF(PaintEventArgs e)
{
             
    // Create string to draw.
    String drawString = "Sample Text";
             
    // Create font and brush.
    Font drawFont = new Font("Arial", 16);
    SolidBrush drawBrush = new SolidBrush(Color.Black);
             
    // Create point for upper-left corner of drawing.
    PointF drawPoint = new PointF(150.0F, 150.0F);
             
    // Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint);
}
Public Sub DrawStringPointF(ByVal e As PaintEventArgs)

    ' Create string to draw.
    Dim drawString As [String] = "Sample Text"

    ' Create font and brush.
    Dim drawFont As New Font("Arial", 16)
    Dim drawBrush As New SolidBrush(Color.Black)

    ' Create point for upper-left corner of drawing.
    Dim drawPoint As New PointF(150.0F, 150.0F)

    ' Draw string to screen.
    e.Graphics.DrawString(drawString, drawFont, drawBrush, drawPoint)
End Sub

另请参阅

适用于

DrawString(ReadOnlySpan<Char>, Font, Brush, RectangleF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定的 BrushFont 对象在指定的矩形中绘制指定的文本字符串。

public:
 void DrawString(ReadOnlySpan<char> s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::RectangleF layoutRectangle);
public void DrawString (ReadOnlySpan<char> s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle);
member this.DrawString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Brush * System.Drawing.RectangleF -> unit
Public Sub DrawString (s As ReadOnlySpan(Of Char), font As Font, brush As Brush, layoutRectangle As RectangleF)

参数

s
ReadOnlySpan<Char>

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

layoutRectangle
RectangleF

RectangleF 结构,用于指定绘制文本的位置。

注解

s 参数表示的文本在由 layoutRectangle 参数表示的矩形内绘制。 如果文本不适合矩形内,则会在最近的单词处截断文本。 若要进一步操作矩形内绘制字符串的方式,请使用采用 StringFormatDrawString(ReadOnlySpan<Char>, Font, Brush, RectangleF, StringFormat) 重载。

适用于

DrawString(ReadOnlySpan<Char>, Font, Brush, PointF)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定的 BrushFont 对象在指定位置绘制指定的文本字符串。

public:
 void DrawString(ReadOnlySpan<char> s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::PointF point);
public void DrawString (ReadOnlySpan<char> s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.PointF point);
member this.DrawString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Brush * System.Drawing.PointF -> unit
Public Sub DrawString (s As ReadOnlySpan(Of Char), font As Font, brush As Brush, point As PointF)

参数

s
ReadOnlySpan<Char>

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

point
PointF

PointF 指定绘制文本左上角的结构。

适用于

DrawString(ReadOnlySpan<Char>, Font, Brush, RectangleF, StringFormat)

Source:
Graphics.cs
Source:
Graphics.cs
Source:
Graphics.cs

使用指定 Brush 指定矩形中的指定文本字符串,并使用指定 StringFormat的格式属性 Font 对象。

public:
 void DrawString(ReadOnlySpan<char> s, System::Drawing::Font ^ font, System::Drawing::Brush ^ brush, System::Drawing::RectangleF layoutRectangle, System::Drawing::StringFormat ^ format);
public void DrawString (ReadOnlySpan<char> s, System.Drawing.Font font, System.Drawing.Brush brush, System.Drawing.RectangleF layoutRectangle, System.Drawing.StringFormat? format);
member this.DrawString : ReadOnlySpan<char> * System.Drawing.Font * System.Drawing.Brush * System.Drawing.RectangleF * System.Drawing.StringFormat -> unit
Public Sub DrawString (s As ReadOnlySpan(Of Char), font As Font, brush As Brush, layoutRectangle As RectangleF, format As StringFormat)

参数

s
ReadOnlySpan<Char>

要绘制的字符串。

font
Font

定义字符串文本格式的 Font

brush
Brush

确定所绘制文本的颜色和纹理的 Brush

layoutRectangle
RectangleF

RectangleF 结构,用于指定绘制文本的位置。

format
StringFormat

StringFormat,用于指定应用于绘制文本的格式属性,例如行距和对齐方式。

适用于