Graphics::DrawString Method (String^, Font^, Brush^, PointF, StringFormat^)

 

Draws the specified text string at the specified location with the specified Brush and Font objects using the formatting attributes of the specified StringFormat.

Namespace:   System.Drawing
Assembly:  System.Drawing (in System.Drawing.dll)

public:
void DrawString(
	String^ s,
	Font^ font,
	Brush^ brush,
	PointF point,
	StringFormat^ format
)

Parameters

s
Type: System::String^

String to draw.

font
Type: System.Drawing::Font^

Font that defines the text format of the string.

brush
Type: System.Drawing::Brush^

Brush that determines the color and texture of the drawn text.

point
Type: System.Drawing::PointF

PointF structure that specifies the upper-left corner of the drawn text.

format
Type: System.Drawing::StringFormat^

StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.

Exception Condition
ArgumentNullException

brush is null.

-or-

s is null.

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.

  • 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 );
   }

.NET Framework
Available since 1.1
Return to top
Show: