Graphics.DrawString 方法

定義

在指定位置繪製指定的文字字串,並使用指定的 BrushFont 物件。

多載

名稱 Description
DrawString(String, Font, Brush, Single, Single, StringFormat)

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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

在指定位置繪製指定的文字字串,並使用指定的 BrushFont 物件。

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

利用指定的格式屬性,在指定的矩形中繪製指定的文字字串,並BrushFont使用指定的 StringFormat和 物件。

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

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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

在指定位置繪製指定的文字字串,並使用指定的 BrushFont 物件。

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

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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)

利用指定的格式屬性,在指定的矩形中繪製指定的文字字串,並BrushFont使用指定的 StringFormat和 物件。

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

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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 該系統規定了套用於繪製文字的格式屬性,如行距與對齊。

例外狀況

brushnull

-或-

snull

範例

以下程式碼範例是為 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
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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
來源:
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 座標。

例外狀況

brushnull

-或-

snull

範例

以下程式碼範例是為 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
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

利用指定的格式屬性,在指定的矩形中繪製指定的文字字串,並BrushFont使用指定的 StringFormat和 物件。

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 該系統規定了套用於繪製文字的格式屬性,如行距與對齊。

例外狀況

brushnull

-或-

snull

範例

以下程式碼範例是為 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
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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 該系統規定了套用於繪製文字的格式屬性,如行距與對齊。

例外狀況

brushnull

-或-

snull

範例

以下程式碼範例是為 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
來源:
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
來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs

在指定位置繪製指定的文字字串,使用指定的 BrushFont 和 的物件,使用指定的 StringFormat格式屬性。

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
來源:
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 結構中指定繪製文字的位置。

例外狀況

brushnull

-或-

snull

範例

以下程式碼範例是為 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 參數所代表的矩形內。 如果文字不符合矩形內,則會在最接近的單字處截斷。 要進一步操作字串在矩形 DrawString 內的繪製方式,可以使用 過載,該參數會取 StringFormat

另請參閱

適用於

DrawString(String, Font, Brush, PointF)

來源:
Graphics.cs
來源:
Graphics.cs
來源:
Graphics.cs
來源:
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 指定繪製文字左上角的結構。

例外狀況

brushnull

-或-

snull

範例

以下程式碼範例是為 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
來源:
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 參數所代表的矩形內。 如果文字不符合矩形內,則會在最接近的單字處截斷。 要進一步操作字串在矩形 DrawString(ReadOnlySpan<Char>, Font, Brush, RectangleF, StringFormat) 內的繪製方式,可以使用 過載,該參數會取 StringFormat

適用於

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

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

利用指定的格式屬性,在指定的矩形中繪製指定的文字字串,並BrushFont使用指定的 StringFormat和 物件。

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 該系統規定了套用於繪製文字的格式屬性,如行距與對齊。

適用於