The following example uses the specified formatting to draw a string in a layout rectangle.
C++
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);
}
PowerBASIC
SUB GDIP_CreateStringFormat (BYVAL hdc AS DWORD)
LOCAL hStatus AS LONG
LOCAL pGraphics AS DWORD
LOCAL pPen AS DWORD
LOCAL pFont AS DWORD
LOCAL pFontFamily AS DWORD
LOCAL pFormat AS DWORD
LOCAL pBlackBrush AS DWORD
LOCAL strFontName AS STRING
LOCAL strText AS STRING
LOCAL rcf AS RECTF
hStatus = GdipCreateFromHDC(hdc, pGraphics)
' // Create the font
strFontName = UCODE$("Arial")
hStatus = GdipCreateFontFamilyFromName(STRPTR(strFontName), %NULL, pFontFamily)
IF hStatus = %StatusOk AND pFontFamily <> %NULL THEN
hStatus = GdipCreateFont(pFontFamily, 16, %FontStyleRegular, %UnitPoint, pFont)
GdipDeleteFontFamily(pFontFamily)
END IF
' Note: You can use the wrapper function GdiPlusCreateFontFromName to create the font:
' hStatus = GdiPlusCreateFontFromName("Arial", 16, %FontStyleRegular, %UnitPoint, pFont)
' // Create a solid brush
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 0, 0, 0), pBlackBrush)
' // Draw the string
rcf.x = 30.0! : rcf.y = 30.0! : rcf.Width = 200.0! : rcf.Height = 25.0!
hStatus = GdipCreateStringFormat(0, %LANG_NEUTRAL, pFormat)
hStatus = GdipSetStringFormatAlign(pFormat, %StringAlignmentCenter)
strText = UCODE$("Sample text")
hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, pFormat, pBlackBrush)
' // Create a Pen
hStatus = GdipCreatePen1(GDIP_ARGB(255,0, 0, 0), 3, %UnitPixel, pPen)
' // Draw a rectangle
hStatus = GdipDrawRectangle(pGraphics, pPen, rcf.x, rcf.y, rcf.Width, rcf.Height)
' // Cleanup
IF pFont THEN GdipDeleteFont(pFont)
IF pBlackBrush THEN GdipDeleteBrush(pBlackBrush)
IF pPen THEN GdipDeletePen(pPen)
IF pFormat THEN GdipDeleteStringFormat(pFormat)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
(http://www.jose.it-berater.org/captures/GdipDrawString3.png)