Graphics::DrawString Method (String^, Font^, Brush^, RectangleF)
Draws the specified text string in the specified rectangle with the specified Brush and Font objects.
Assembly: System.Drawing (in System.Drawing.dll)
public: void DrawString( String^ s, Font^ font, Brush^ brush, RectangleF layoutRectangle )
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.
- layoutRectangle
-
Type:
System.Drawing::RectangleF
RectangleF structure that specifies the location of the drawn text.
| Exception | Condition |
|---|---|
| ArgumentNullException | brush is null. -or- s is null. |
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. To further manipulate how the string is drawn inside the rectangle use the DrawString overload that takes a StringFormat.
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 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 ); }
Available since 1.1