Graphics.DrawString 方法

定义

在指定位置并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects.

重载

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

使用指定 Brush 的格式化特性,用指定的 FontStringFormat 对象在指定的位置绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

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

使用指定 Brush 的格式化特性,用指定的 FontStringFormat 对象在指定的矩形中绘制指定的文本字符串。Draws the specified text string in the specified rectangle with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

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

在指定位置并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects.

DrawString(String, Font, Brush, RectangleF)

在指定矩形并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string in the specified rectangle with the specified Brush and Font objects.

DrawString(String, Font, Brush, PointF)

在指定位置并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects.

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

使用指定 Brush 的格式化特性,用指定的 FontStringFormat 对象在指定的位置绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

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

使用指定 Brush 的格式化特性,用指定的 FontStringFormat 对象在指定的位置绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified 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);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * single * single * System.Drawing.StringFormat -> unit

参数

s
String

要绘制的字符串。String to draw.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

brush
Brush

Brush,它确定所绘制文本的颜色和纹理。Brush that determines the color and texture of the drawn text.

x
Single

所绘制文本的左上角的 x 坐标。The x-coordinate of the upper-left corner of the drawn text.

y
Single

所绘制文本的左上角的 y 坐标。The y-coordinate of the upper-left corner of the drawn text.

format
StringFormat

StringFormat,它指定应用于所绘制文本的格式化特性(如行距和对齐方式)。StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.

例外

brushnullbrush is null.

-或--or- snulls is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 创建要绘制的文本字符串。Creates a text string to draw.

  • 将字体定义为 Arial (16pt 字号)。Defines the font as Arial (16pt).

  • 创建用于绘制的实心黑色画笔。Creates a solid, black brush to draw with.

  • 创建用于绘制文本的左上角的点坐标。Creates the coordinates of a point for the upper-left corner at which to draw the text.

  • 设置要垂直绘制的字符串的格式Sets the format of the string to draw vertically

  • 使用字体、画笔、目标点和格式将字符串绘制到屏幕。Draws the string to the screen using the font, brush, destination point, and format.

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(String, Font, Brush, RectangleF, StringFormat)

使用指定 Brush 的格式化特性,用指定的 FontStringFormat 对象在指定的矩形中绘制指定的文本字符串。Draws the specified text string in the specified rectangle with the specified Brush and Font objects using the formatting attributes of the specified 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);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.RectangleF * System.Drawing.StringFormat -> unit

参数

s
String

要绘制的字符串。String to draw.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

brush
Brush

Brush,它确定所绘制文本的颜色和纹理。Brush that determines the color and texture of the drawn text.

layoutRectangle
RectangleF

RectangleF 结构,它指定所绘制文本的位置。RectangleF structure that specifies the location of the drawn text.

format
StringFormat

StringFormat,它指定应用于所绘制文本的格式化特性(如行距和对齐方式)。StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.

例外

brushnullbrush is null.

-或--or- snulls is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 创建要绘制的文本字符串。Creates a text string to draw.

  • 将字体定义为 Arial (16pt 字号)。Defines the font as Arial (16pt).

  • 创建用于绘制的实心黑色画笔。Creates a solid, black brush to draw with.

  • 创建一个要在其中绘制文本的矩形。Creates a rectangle in which to draw the text.

  • 将矩形绘制到屏幕上。Draws the rectangle to the screen.

  • 设置字符串的格式,以使其在矩形中居中。Sets the format of the string to center it within the rectangle.

  • 使用字体、画笔和目标矩形在屏幕上绘制字符串。Draws the string to the screen using the font, brush, and destination rectangle.

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 参数所表示的矩形内。The text represented by the s parameter is drawn inside the rectangle represented by the layoutRectangle parameter. 如果矩形内不能容纳文本,则它会在最近的单词处被截断,除非另外用 format 参数指定。If the text does not fit inside the rectangle, it is truncated at the nearest word, unless otherwise specified with the format parameter.

另请参阅

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

在指定位置并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects.

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);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * single * single -> unit

参数

s
String

要绘制的字符串。String to draw.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

brush
Brush

Brush,它确定所绘制文本的颜色和纹理。Brush that determines the color and texture of the drawn text.

x
Single

所绘制文本的左上角的 x 坐标。The x-coordinate of the upper-left corner of the drawn text.

y
Single

所绘制文本的左上角的 y 坐标。The y-coordinate of the upper-left corner of the drawn text.

例外

brushnullbrush is null.

-或--or- snulls is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 创建要绘制的文本字符串。Creates a text string to draw.

  • 将字体定义为 Arial (16pt 字号)。Defines the font as Arial (16pt).

  • 创建用于绘制的实心黑色画笔。Creates a solid black brush to draw with.

  • 创建用于绘制文本的左上角的点。Creates a point for the upper-left corner at which to draw the text.

  • 使用字体、画笔和目标点将字符串绘制到屏幕。Draws the string to the screen using the font, brush, and destination point.

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)

在指定矩形并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string in the specified rectangle with the specified Brush and Font objects.

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);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.RectangleF -> unit

参数

s
String

要绘制的字符串。String to draw.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

brush
Brush

Brush,它确定所绘制文本的颜色和纹理。Brush that determines the color and texture of the drawn text.

layoutRectangle
RectangleF

RectangleF 结构,它指定所绘制文本的位置。RectangleF structure that specifies the location of the drawn text.

例外

brushnullbrush is null.

-或--or- snulls is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 创建要绘制的文本字符串。Creates a text string to draw.

  • 将字体定义为 Arial (16pt 字号)。Defines the font as Arial (16pt).

  • 创建用于绘制的实心黑色画笔。Creates a solid, black brush to draw with.

  • 创建一个要在其中绘制文本的矩形。Creates a rectangle in which to draw the text.

  • 将矩形绘制到屏幕上。Draws the rectangle to the screen.

  • 使用字体、画笔和目标矩形在屏幕上绘制字符串。Draws the string to the screen using the font, brush, and destination rectangle.

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 参数所表示的矩形内。The text represented by the s parameter is drawn inside the rectangle represented by the layoutRectangle parameter. 如果矩形内不能容纳文本,则该文本将在最近的单词处被截断。If the text does not fit inside the rectangle, it is truncated at the nearest word. 若要进一步操作在矩形中绘制字符串的方式,请使用采用 StringFormatDrawString 重载。To further manipulate how the string is drawn inside the rectangle use the DrawString overload that takes a StringFormat.

另请参阅

DrawString(String, Font, Brush, PointF)

在指定位置并且用指定的 BrushFont 对象绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects.

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);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.PointF -> unit

参数

s
String

要绘制的字符串。String to draw.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

brush
Brush

Brush,它确定所绘制文本的颜色和纹理。Brush that determines the color and texture of the drawn text.

point
PointF

PointF 结构,它指定所绘制文本的左上角。PointF structure that specifies the upper-left corner of the drawn text.

例外

brushnullbrush is null.

-或--or- snulls is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 创建要绘制的文本字符串。Creates a text string to draw.

  • 将字体定义为 Arial (16pt 字号)。Defines the font as Arial (16pt).

  • 创建用于绘制的实心黑色画笔。Creates a solid, black brush to draw with.

  • 创建用于绘制文本的左上角的点。Creates a point for the upper-left corner at which to draw the text.

  • 使用字体、画笔和目标点将字符串绘制到屏幕。Draws the string to the screen using the font, brush, and destination point.

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(String, Font, Brush, PointF, StringFormat)

使用指定 Brush 的格式化特性,用指定的 FontStringFormat 对象在指定的位置绘制指定的文本字符串。Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified 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);
member this.DrawString : string * System.Drawing.Font * System.Drawing.Brush * System.Drawing.PointF * System.Drawing.StringFormat -> unit

参数

s
String

要绘制的字符串。String to draw.

font
Font

Font,它定义字符串的文本格式。Font that defines the text format of the string.

brush
Brush

Brush,它确定所绘制文本的颜色和纹理。Brush that determines the color and texture of the drawn text.

point
PointF

PointF 结构,它指定所绘制文本的左上角。PointF structure that specifies the upper-left corner of the drawn text.

format
StringFormat

StringFormat,它指定应用于所绘制文本的格式化特性(如行距和对齐方式)。StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.

例外

brushnullbrush is null.

-或--or- snulls is null.

示例

下面的代码示例旨在与 Windows 窗体一起使用,并且它需要 PaintEventArgse,它是 Paint 事件处理程序的参数。The following code example is designed for use with Windows Forms, and it requires PaintEventArgse, which is a parameter of the Paint event handler. 此代码执行以下操作:The code performs the following actions:

  • 创建要绘制的文本字符串。Creates a text string to draw.

  • 将字体定义为 Arial (16pt 字号)。Defines the font as Arial (16pt).

  • 创建用于绘制的实心黑色画笔。Creates a solid, black brush to draw with.

  • 创建用于绘制文本的左上角的点。Creates a point for the upper-left corner at which to draw the text.

  • 设置要垂直绘制的字符串的格式。Sets the format of the string to draw vertically.

  • 使用字体、画笔、目标点和格式将字符串绘制到屏幕。Draws the string to the screen using the font, brush, destination point, and format.

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

另请参阅

适用于