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. Then, the code gets the tab stops and proceeds to use or inspect the tab stops in some way.
C++
VOID Example_GetTabStop(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);
// Get the tab stops.
INT tabStopCount = 0;
REAL firstTabOffset = 0;
REAL* tabStops = NULL;
tabStopCount = stringFormat.GetTabStopCount();
tabStops = (REAL*)malloc(tabStopCount*sizeof(REAL));
stringFormat.GetTabStops(tabStopCount, &firstTabOffset, tabStops);
for(INT j = 0; j < tabStopCount; ++j)
{
// Inspect or use the value in tabStops[j].
}
}
PowerBASIC
SUB GDIP_GetStringFormatTabStops (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)
' // Get the tab stops.
LOCAL tabStopCount AS LONG
LOCAL firstTabOffset AS SINGLE
DIM tabStops(0) AS SINGLE
hStatus = GdipGetStringFormatTabStopCount(pStringFormat, tabStopCount)
REDIM tabStops(tabStopCount - 1)
hStatus = GdipGetStringFormatTabStops(pStringFormat, tabStopCount, firstTabOffset, tabStops(0))
LOCAL j AS LONG
FOR j = 0 TO tabStopCount - 1
' // Inspect or use the value in tabStops[j]
NEXT
' // 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)