This method uses GDI+ functionality to retrieve the height and width of text from text information.
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) |
UnitMode | Units of measurement (0: pixels, 1: 0.1mm) |
In \Visual Basic the following constants can be used: (ikPrinterPixel = 0, ikPrinterMM = 1).
Return Value
Returns True (nonzero) if successful. Returns False (0) if unsuccessful.
Explanation
The MeasureString method uses GDI+ functionality to retrieve the height and width of text from text information. To retrieve the height and width of the text set in the Text property, the following properties should be set: FontName, FontSize, HCentering, VCentering, HotkeyPrefix, Text. 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.
For scripting languages and other programs that require variant types, please refer to the MeasureStringVariant method.
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 MeasureString method in Visual Basic, the values should start with &H instead of 0x.
In Visual Basic the following constants can be used: (ikDirectionRightToLeft = 0x00000001, ikDirectionVertical = 0x00000002, ikNoFitBlackBox = 0x00000004, ikDisplayFormatControl = 0x00000020, ikNoFontFallback = 0x00000400, ikMeasureTrailingSpaces = 0x00000800, ikNoWrap = 0x00001000, ikLineLimit = 0x00002000, ikNoClip = 0x00004000).
Sample code:
Visual Basic
Dim Str_Width As Single
Dim Str_Height As Single
Dim charactersFitted As Long
Dim linesFilled As Long
Dim TxRight As Long
Dim TxBottom As Long
ImageKit1.PrintDraw.FontSize = 20
ImageKit1.PrintDraw.FontName = "Arial"
ImageKit1.PrintDraw.HotkeyPrefix = 0
ImageKit1.PrintDraw.Text = "Text"
ImageKit1.PrintDraw.MeasureString(hDC, 0, 0, Str_Width, Str_Height, charactersFitted, linesFilled, 0, 0)
TxRight = 10 + CLng(Str_Width) - 1
TxBottom = 10 + CLng(Str_Height) - 1
ImageKit1.PrintDraw.CharAngle = 0
ImageKit1.PrintDraw.TextColor1 = vbRed
ImageKit1.PrintDraw.Alpha1 = 255
ImageKit1.PrintDraw.TextColor2 = vbWhite
ImageKit1.PrintDraw.Alpha2 = 255
'use the gradiation brush
ImageKit1.PrintDraw.DrawString(hDC, 10, 10, TxRight, TxBottom, 4, 0, 0, 0, 0, 0)
Visual C++
float Str_Width, Str_Height;
long charactersFitted, linesFilled, right, bottom;
ImageKit1.GetPrintDraw().ClearProperty();
ImageKit1.GetPrintDraw().SetFontSize(20);
ImageKit1.GetPrintDraw().SetFontName("Arial");
ImageKit1.GetPrintDraw().SetHotkeyPrefix(0);
ImageKit1.GetPrintDraw().SetText("Text");
ImageKit1.GetPrintDraw().MeasureString(hDC, 0, 0, &Str_Width, &Str_Height, &charactersFitted, &linesFilled, 0, 0);
right = 10 + (long)Str_Width - 1;
bottom = 10 + (long)Str_Height - 1;
ImageKit1.GetPrintDraw().SetCharAngle(0);
ImageKit1.GetPrintDraw().SetTextColor1(RGB(255, 0, 0));
ImageKit1.GetPrintDraw().SetAlpha1(255);
ImageKit1.GetPrintDraw().SetTextColor2(RGB(255, 255, 255));
ImageKit1.GetPrintDraw().SetAlpha2(255);
//use the gradiation brush
ImageKit1.GetPrintDraw().DrawString(hDC, 10, 10, right, bottom, 4, 0, 0, 0, 0, 0);