Graphics.DrawString(const WCHAR*, INT, const Font*, const RectF, const StringFormat*, const Brush*) method

Applies to: desktop apps only

The Graphics::DrawString method draws a string based on a font, a layout rectangle, and a format.

Syntax

Status DrawString(
  [in]       const WCHAR *string,
  [in]       INT length,
  [in]       const Font *font,
  [in, ref]  const RectF &layoutRect,
  [in]       const StringFormat *stringFormat,
  [in]       const Brush *brush
);

Parameters

  • string [in]
    Type: const WCHAR*

    Pointer to a wide-character string to be drawn.

  • length [in]
    Type: INT

    Integer that specifies the number of characters in the string array. The length parameter can be set to –1 if the string is null terminated.

  • font [in]
    Type: const Font*

    Pointer to a Font object that specifies the font attributes (the family name, the size, and the style of the font) to use.

  • layoutRect [in, ref]
    Type: const RectF

    Reference to a rectangle that bounds the string.

  • stringFormat [in]
    Type: const StringFormat*

    Pointer to a StringFormat object that specifies text layout information and display manipulations to be applied to the string.

  • brush [in]
    Type: const Brush*

    Pointer to a Brush object that is used to fill the string.

Return value

Type:

Type: Status

If the method succeeds, it returns Ok, which is an element of the Status enumeration.

If the method fails, it returns one of the other elements of the Status enumeration.

Remarks

Note that GDI+ does not support PostScript fonts or OpenType fonts which do not have TrueType outlines.

When you use the GDI+ API, you must not allow your application to download arbitrary fonts from untrusted sources. The operating system requires elevated privileges to assure that all installed fonts are trusted.

Examples

The following example uses the specified formatting to draw a string in a layout rectangle.

VOID Example_DrawString(HDC hdc)
{
   Graphics graphics(hdc);

   // Create a string.
   WCHAR string[] = L"Sample Text";
   
   // Initialize arguments.
   Font myFont(L"Arial", 16);
   RectF layoutRect(0.0f, 0.0f, 200.0f, 50.0f);
   StringFormat format;
   format.SetAlignment(StringAlignmentCenter);
   SolidBrush blackBrush(Color(255, 0, 0, 0));

   // Draw string.
   graphics.DrawString(
   string,
   11,
   &myFont,
   layoutRect,
   &format,
   &blackBrush);

   // Draw layoutRect.
   graphics.DrawRectangle(&Pen(Color::Black, 3), layoutRect);
}

Requirements

Minimum supported client

Windows XP, Windows 2000 Professional

Minimum supported server

Windows 2000 Server

Product

GDI+ 1.0

Header

Gdiplusgraphics.h (include Gdiplus.h)

Library

Gdiplus.lib

DLL

Gdiplus.dll

See also

Graphics

Font

Pen

RectF

StringFormat

 

 

Send comments about this topic to Microsoft

Build date: 3/6/2012