Graphics.DrawString Method (String, Font, Brush, Single, Single, StringFormat)
Assembly: System.Drawing (in system.drawing.dll)
public: void DrawString ( String^ s, Font^ font, Brush^ brush, float x, float y, StringFormat^ format )
public void DrawString ( String s, Font font, Brush brush, float x, float y, StringFormat format )
public function DrawString ( s : String, font : Font, brush : Brush, x : float, y : float, format : StringFormat )
Not applicable.
Parameters
- s
String to draw.
- font
Font that defines the text format of the string.
- brush
Brush that determines the color and texture of the drawn text.
- x
The x-coordinate of the upper-left corner of the drawn text.
- y
The y-coordinate of the upper-left corner of the drawn text.
- format
StringFormat that specifies formatting attributes, such as line spacing and alignment, that are applied to the drawn text.
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 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.get_Black());
// Create point for upper-left corner of drawing.
float x = 150;
float y = 50;
// Set format of string.
StringFormat drawFormat = new StringFormat();
drawFormat.set_FormatFlags(StringFormatFlags.DirectionVertical);
// Draw string to screen.
e.get_Graphics().DrawString(drawString, drawFont, drawBrush, x, y,
drawFormat);
} //DrawStringFloatFormat
Windows 98, Windows Server 2000 SP4, Windows Millennium Edition, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
The Microsoft .NET Framework 3.0 is supported on Windows Vista, Microsoft Windows XP SP2, and Windows Server 2003 SP1.