This function uses GDI+ functionality to retrieve the height and width of text from text information.

[C++Builder]     BOOL IKMeasureString(HDC hDC, int AreaWidth, int AreaHeight, LPFLOAT Width, LPFLOAT Height, LPINT charactersFitted, LPINT linesFilled, int FormatFlags, PTR_IKPRINT_TEXTINFO TextInfo, LPCTSTR Text, BYTE UnitMode);
[Delphi]         function IKMeasureString(hDC: HDC; AreaWidth, AreaHeight: Integer; var Width, Height: Single; var charactersFitted, linesFilled: Integer; FormatFlags: Integer; var TextInfo: IKPRINT_TEXTINFO; Text: PChar; UnitMode: Byte): LongBool;

Parameters

Name Explanation
hDC The device context
AreaWidth The maximum width (from 0)
AreaHeight The maximum height (from 0)
Width Retrieves the width of the text
Height Retrieves the height of the text
charactersFitted The number of characters in the retrieved text
linesFilled The number of lines in the retrieved text
FormatFlags The string format information (default is 0)
TextInfo Structure whose members set the drawing information
Text The string parameter containing the text
UnitMode Units of measurement (0: pixels, 1: 0.1mm)

Return Value

Returns True (nonzero) if successful. Returns False (0) if unsuccessful.

Explanation

The IKMeasureString function uses GDI+ functionality to retrieve the height and width of text from text information. To retrieve the height and width of the text, the following member variables of the TextInfo structure should be set: FontName, FontSize, HCentering, VCentering, and HotkeyPrefix. If 0 is set in either the AreaWidth or AreaHeight parameters, the height and width of the whole text will be retrieved. However if the AreaWidth and AreaHeight parameters contain values other than 0, then only the height and width of the text contained within that area will be retrieved. The charactersFitted and linesFilled work in the same way. If 0 is set in either the AreaWidth or AreaHeight parameters, then the number of characters or the number of lines in the whole text will be retrieved and returned in the charactersFitted and linesFilled parameters. However if the AreaWidth and AreaHeight parameters contain values other than 0, then the values in the charactersFitted and linesFilled parameters will relate only to the text contained within the area defined by the AreaWidth and AreaHeight parameters.

To execute this function, the IKPrintGdipStart function must be executed prior to this function.

If the UnitMode parameter is 0 then the AreaWidth, AreaHeight, Width, Height parameters are in pixel units.

If the UnitMode parameter is 1 then AreaWidth, AreaHeight, Width, Height parameters are in 0.1mm units.

String format info

Value Explanation
0x00000001 Text is displayed from right to left
0x00000002 Text is vertically aligned.
0x00000004 Parts of characters are allowed to overhang the string's layout rectangle. By default, characters are repositioned to avoid any overhang.
0x00000020 Control characters such as the left-to-right mark are shown in the output with a representative glyph.
0x00000400 Fallback to alternate fonts for characters not supported in the requested font is disabled. Any missing characters are displayed with the fonts missing glyph, usually an open square.
0x00000800 Includes the trailing space at the end of each line. Set this flag to include that space in measurement.
0x00001000 Text wrapping between lines when formatting within a rectangle is disabled. This flag is implied when a point is passed instead of a rectangle, or when the specified rectangle has a zero line length.
0x00002000 Only entire lines are laid out in the formatting rectangle. By default layout continues until the end of the text, or until no more lines are visible as a result of clipping, whichever comes first. Note that the default settings allow the last line to be partially obscured by a formatting rectangle that is not a whole multiple of the line height. To ensure that only whole lines are seen, specify this value and be careful to provide a formatting rectangle at least as tall as the height of one line.
0x00004000 Overhanging parts of glyphs, and unwrapped text reaching outside the formatting rectangle are allowed to show. By default all text and glyph parts reaching outside the formatting rectangle are clipped.

The values of the IKMeasureString function in Delphi should start with $ instead of 0x as written above.

Sample code:

In C++Builder

    IKPRINT_TEXTINFO TextInfo;
    float Str_Width, Str_Height;
    int charactersFitted, linesFilled;
    RECT PrintRect;
    ULONG_PTR token;//DWORD token;

    token = IKPrintGdipStart();
    if (token == 0) return;

    memset(&TextInfo, 0, sizeof(IKPRINT_TEXTINFO));
    TextInfo.FontSize = 20;
    lstrcpy(TextInfo.FontName, "MS Gothic");
    TextInfo.HotkeyPrefix = 0;
    IKMeasureString(hDC, 0, 0, &Str_Width, &Str_Height, &charactersFitted, &linesFilled, 0, &TextInfo, "Text", 0);

    PrintRect.left = 10;
    PrintRect.top = 10;
    PrintRect.right = PrintRect.left + (long)Str_Width - 1;
    PrintRect.bottom = PrintRect.top + (long)Str_Height - 1;
    TextInfo.CharAngle = 0;
    TextInfo.TextColor1 = RGB(255, 0, 0);
    TextInfo.Alpha1 = 255;
    TextInfo.TextColor2 = RGB(255, 255, 255);
    TextInfo.Alpha2 = 255;
    //using the Gradiation Brush
    IKDrawString(hDC, &PrintRect, 4, 0, 0, 0, 0, &TextInfo, "Text", 0);
    IKPrintGdipEnd(token)

In Delphi

    TextInfo: IKPRINT_TEXTINFO;
    Str_Width, Str_Height: Single;
    CharactersFitted, linesFilled: Integer;
    PrintRect: TRect;
    token: DWORD;

    token := IKPrintGdipStart();
    if token = 0 then Exit;

    FillChar(TextInfo, SizeOf(TextInfo), 0);
    TextInfo.FontSize := 20;
    StrPCopy(TextInfo.FontName, 'MS Gothic');
    TextInfo.HotkeyPrefix := 0;
    IKMeasureString(DC, 0, 0, Str_Width, Str_Height, charactersFitted, linesFilled, 0, TextInfo, 'Text', 0);

    PrintRect.Left := 10;
    PrintRect.Top := 10;
    PrintRect.Right := PrintRect.Left + Trunc(Str_Width) - 1;
    PrintRect.Bottom := PrintRect.Top + Trunc(Str_Height) - 1;
    TextInfo.CharAngle := 0;
    TextInfo.TextColor1 := clRed;
    TextInfo.Alpha1 := 255;
    TextInfo.TextColor2 := clWhite;
    TextInfo.Alpha2 := 255;
    //using the Gradiation Brush
    IKDrawString(DC, PrintRect, 4, 0, 0, 0, 0, TextInfo, 'Text', 0);
    IKPrintGdipEnd(token)

 

The ImageKit10 VCL is a product created by Newtone Corporation