The following example creates a
StringFormat object, sets tab stops, and uses the
StringFormat object to draw a string that contains tab characters. The code also draws the string's layout rectangle.
C++
VOID Example_SetTabStops(HDC hdc)
{
Graphics graphics(hdc);
REAL tabs[] = {150, 100, 100};
FontFamily fontFamily(L"Courier New");
Font font(&fontFamily, 12, FontStyleRegular, UnitPoint);
SolidBrush solidBrush(Color(255, 0, 0, 255));
StringFormat stringFormat;
stringFormat.SetTabStops(0, 3, tabs);
graphics.DrawString(
L"Name\tTest 1\tTest 2\tTest 3",
25,
&font,
RectF(20, 20, 500, 100),
&stringFormat,
&solidBrush);
// Draw the rectangle that encloses the text.
Pen pen(Color(255, 255, 0, 0));
graphics.DrawRectangle(&pen, 20, 20, 500, 100);
}
PowerBASIC
SUB GDIP_SetStringFormatTabStops (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 pStringFormat AS DWORD
LOCAL pSolidBrush AS DWORD
LOCAL strFontName AS STRING
LOCAL strText AS STRING
LOCAL rcf AS RECTF
hStatus = GdipCreateFromHDC(hdc, pGraphics)
DIM tabs(2) AS SINGLE
ARRAY ASSIGN tabs() = 150, 100, 100
' // Create the font
strFontName = UCODE$("Courier New")
hStatus = GdipCreateFontFamilyFromName(STRPTR(strFontName), %NULL, pFontFamily)
IF hStatus = %StatusOk AND pFontFamily <> %NULL THEN
hStatus = GdipCreateFont(pFontFamily, 12, %FontStyleRegular, %UnitPoint, pFont)
GdipDeleteFontFamily(pFontFamily)
END IF
' // Create a solid brush
hStatus = GdipCreateSolidFill(GDIP_ARGB(255, 255, 0, 0), pSolidBrush)
' Note: You can use the wrapper function GdiPlusCreateFontFromName to create the font:
' hStatus = GdiPlusCreateFontFromName("Courier New", 12, %FontStyleRegular, %UnitPoint, pFont)
hStatus = GdipCreateStringFormat(0, %LANG_NEUTRAL, pStringFormat)
hStatus = GdipSetStringFormatTabStops(pStringFormat, 0, 3, tabs(0))
rcf.x = 20 : rcf.y = 20 : rcf.Width = 500 : rcf.Height = 100
strText = UCODE$("Name" & $TAB & "Test 1" & $TAB & "Test 2" & $TAB & "Test 3")
hStatus = GdipDrawString(pGraphics, STRPTR(strText), LEN(strText) \ 2, pFont, rcf, pStringFormat, pSolidBrush)
' // Draw the rectangle that encloses the text.
hStatus = GdipCreatePen1(GDIP_ARGB(255, 255, 0, 0), 1, %UnitPixel, pPen)
hStatus = GdipDrawRectangle(pGraphics, pPen, rcf.x, rcf.y, rcf.Width, rcf.Height)
' // Cleanup
IF pFont THEN GdipDeleteFont(pFont)
IF pSolidBrush THEN GdipDeleteBrush(pSolidBrush)
IF pPen THEN GdipDeletePen(pPen)
IF pStringFormat THEN GdipDeleteStringFormat(pStringFormat)
IF pGraphics THEN GdipDeleteGraphics(pGraphics)
END SUB
The following illustration shows the output of the preceding code.
(http://www.jose.it-berater.org/captures/GdipSetStringFormatTabStops.png)