Windows Client development-how to measure the physical length of a string

Source: Internet
Author: User
Tags vector font

Windows Client development-how to measure the physical length of a string

The first thing to note is that the length of the string I mentioned is neither the length of the string nor the size of the string. I mean the display length, that is, the physical length.

Reason:
This is because of some problems.
When duilib is used for development, it is found that the label control cannot adapt to the width.

Thoughts:
This shows that this library is insufficient, but we can try to modify it. But in the spirit of open source, maybe the original design of this library is that the label cannot adapt the width of the string?

All values only follow the second path. First, we obtain the width value of the string to be displayed, and then set the width of the label to width.

Implementation:
Now the question is, on windows, how can we get the width of a string?

At this time, you must have thought of using the device description table, HDC.

I will not go into details about how to obtain DC and how to release DC. I have explained this in my previous blog.

At this time, you will ask, How can I determine the display length of a string?
For the same string, different fonts of different sizes are displayed.

Therefore, we must also use fonts.

Here is a function:

HFONT hFont = CreateFont(27, 0, 0, 0, FW_DONTCARE, 0, 0, 0,        ANSI_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS,        DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, NULL);

Take a look at this function. There are many parameters:

CHeight is the font height.
CWidth is the font width.
CEscapement is the tilt angle of the font.
COrientation is the tilt angle of the font.
CWeight is the font width.
BItalic indicates whether the font is italic.
BUnderline indicates whether the font contains underscores.
BStrikeOut indicates whether the font contains strikethrough.
ICharSet is the character set used by the font.
IOutPrecision specifies how to select a proper font.
IClipPrecision is used to determine the cropping precision.
How does iQuality match the selected font.
IPitchAndFamily is the spacing flag and attribute flag.
PszFaceName is the font name.
Function: This function creates a special logical font that can be selected by any device.
Function prototype: HFONT CreateFont (int nHeight, int nWidth, int nEscapement, int nOrientation, int fnWeight, DWORD fdwltalic, DWORD fdwUnderline, DWORD fdwStrikeOut, DWORD

FdwCharSet, DWORD fdwOutputPrecision, DWORD fdwClipPrecision, DWORD fdwQuality, DWORD fdwPitchAndFamily, LPCTSTR lpszFace );

Parameters:
NHeight: Specifies the logical unit height of the character unit or character in the font. The height value (also known as em height) of the character unit minus the internal header value. The font er interprets the nHeight value in the following way.
Is:

0: The font er converts the value to the unit of the device and matches the unit height of the existing font.

0: Use a default height value for the font er conversion.

<0: The font er converts the value to the device unit and matches the absolute value with the character height of an existing font.

Compare all heights. The font er selects the maximum font size that does not exceed the required size.

This ing occurs when the font is used for the first time.

For MM_TEXT ing, you can use the following formula to determine the height of a font with a specified dot size:

NHeight =-MulDiv (PointSize, GetDeviceCaps (hDC, LOGPIXELSY), 72)

NWidth: Specifies the average width of the logical unit of Characters in the required font. If this value is 0, the font er selects a closest match value, the closest match value is determined by the absolute value of the difference between the feature coefficient of the current device and the digital feature coefficient of the available font.

NEscapement: Specifies an angle between the shift vector and the X-axis of the device, in the unit of 10 to 10 degrees. The shift vector is parallel to the baseline of the positive line.

Windows NT: When the graphics device is set to GM_ADVANCED, the position angle of the string can be specified without relying on the Character positioning angle of the string.

When the graphic mode is set to GM_COMPATIBLE, nEscapement specifies both the shift angle and the positioning angle. You can set nEscapement and nOrientation to the same value.

Windows 95: nEscapement specifies both the shift angle and positioning angle. You can set nEscapement and nOrientation to the same value.

NOrientation: Specifies the angle between the baseline of each character and the X axis of the device.

FnWeight: Specifies the font weight between 0 and 1000. For example, 400 indicates the standard body and 700 indicates the Black (thick) body. If this value is 0, the default weight is used.

For easy definition, you can use the following values:

FW_DONTCARE: 0; FW_THIN; 100; FW_EXTRALIGHT; 200; FW_ULTRALIGHT; 200; FW_LIGHT; 300;

FW_NORMAL: 400; FW_REGULAR; 400; FW_MEDIUM; 500; FW_SEMIBOLD; 600; FW_DEMIBOLD; 600;

FW_BOLD: 700; FW_EXTRABOLD; 800; FW_ULTRABOLD; 800; FW_HEAVY; 900; FW_BLACK; 900.

FdwItalic: italic is specified if it is set to TRUE.

FdwUnderline: If this parameter is set to TRUE, all characters with underscores are specified.

FdwStrikeOut: if it is set to TRUE, strikeout specifies the font.

FdwCharSet: Specifies the character set. The following values are predefined:

ANSI_CHARSET; BALTIC_CHARSET; CHINESEBIG5_CHARSET; DEFAULT_CHARSET;

EASTEUROPE_CHARSET; GB2312_CHARSET; GREEK_CHARSET; HANGUL_CHARSET; MAC_CHARSET; OEM_CHARSET; RUSSIAN_CHARSET; SHIFTJIS_CHARSET;
SYMBOL_CHARSET; TURKISH_CHARSET.
Korean Windows: JOHAB_CHARSET;
Windows in the Middle East: HEBREW_CHARSSET; ARABIC_CHARSET
Windows: THAI_CHARSET in Thailand

The character set specified by OEM_CHARSET is related to the operating system.

You can use the DEFAULT_CHARSET value to allow the font name and size to fully describe the logical font. If the specified font name does not exist, the font of any character set can replace the specified font. Therefore, use DEFAULT_CHARSET to avoid unexpected results.
The operating system contains fonts of other character sets. If an application uses a font of an unknown character set, the application will not try to translate or interpret the string written in that font.

This parameter is important in the font ing process. To ensure consistent results, specify a special character set. If a font name is specified in the lpszFace parameter, check whether the fdwCharSet value matches the font character set specified by lpszFace.
FdwOutputPrecision: Specifies the output precision. The output precision is a matching program that matches the required font height, width, character location, shift, Character spacing, and character type. It can take one of the following values:

OUT_CHARACTER_PRECIS; unused.

OUT_DEFAULT_PRECIS: Specifies the default font ER status.

OUT_DEVICE_PRECIS: indicates that the font er selects a device font when multiple fonts in the system use the same font with the same name.

OUT_OUTLINE_PRCIS: in Windows NT, this value indicates that the font er selects from TrueType and other border-based fonts.

OUT_RASTER_PRECIS: indicates that the font er selects a grating font when multiple fonts in the system use the same name.

OUT_STRING_PRECIS: this value is not used by the word er, but is returned when the scan font is listed.

OUT_STROKE_PRECIS: in Windows NT, this value is not used by the font er, but is returned when the TrueType font, other border-based fonts, and Vector Fonts are listed.

Windows 95: this value is not used by the font er, but is returned when the TrueType font or vector font is listed.

OUT_TT_ONLY_PRECIS: indicates that the font er selects only the TrueType font. If the TrueType font is not installed in the system, the default font ing status is returned. ,

OUT_TT_PRECIS: indicates that the font er selects a TrueType font when there are multiple fonts with the same name in the system.

When the operating system contains a variety of fonts with the same name as the specified name, the application can use the OUT_DEVICE_PRECIS, OUT_RASTER_PRECIS, and OUT_TT_PRECIS values to control how the font er chooses a font. For example, if the operating system contains two fonts: the grating and TrueType, specify OUT_TT_PRECIS to enable the font er to select the TrueType mode. Specify OUT_TT_ONLY_PRECIS to select a TrueType font for the font er, although this will change the TrueType font name.

FdwClipPrecision; specifies the cropping precision. The cropping precision defines how to crop a part of a character that exceeds the cropping area. It can take one or more of the following values:

CLIP_DEFAULT_PRECIS: Specifies the default cropping status. CLIP_CHARACTER_PRECIS: unused.

CLIP_STROKE_PRECIS: it is not used by the font er, but is returned when the grating font, vector font, or TrueType font is listed. In Windows, to ensure compatibility, this value is always returned when the font is listed.

CLIP_MASK: unused. CLIP_EMBEDDED: this flag is required to use an embedded read-only font.

CLIP_LH_ANGLES: When this value is used, the rotation of all fonts depends on the positioning of the coordinate system to the left or to the right.

If this value is not used, the font of the device is always rotated counterclockwise, but the rotation of other fonts depends on the orientation of the coordinate system. To get

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.