共用方式為


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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 Forms 使用而設計,而且需要 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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 Forms 使用而設計,而且需要 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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 Forms 使用而設計,而且需要 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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 Forms 使用而設計,而且需要 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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 Forms 使用而設計,而且需要 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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 Forms 使用而設計,而且需要 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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
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,指定套用至繪製文字的格式化屬性,例如行距和對齊方式。

適用於