Precisely obtain the character position in cricheditctrl

Source: Internet
Author: User

The gettextmetrics function can get a font text measurement and put it into a data structure of textmetric type. The structure is as follows:

Typedef struct tagtextmetric {// TM
Long tmheight; // character height
Long tmascent; // upper height of the character (baseline or above)
Long tmdescent; // lower part of the character height (below baseline)
Long tminternalleading; // number of spaces at the top of the string height defined by tmheight
Long tmexternalleading; // number of spaces added between two rows
Long tmavecharwidth; // average character width
Long tmmaxcharwidth; // The width of the widest character
Long tmweight; // The font width and weight
Long tmoverhang; // Add the additional height on some spliced Fonts
Long tmdigitizedaspectx; // The horizontal resolution of 96 DPI devices for the font design
Long tmdigitizedaspecty; // The vertical resolution of 96 DPI devices for the font design
Bchar tmfirstchar; // the first character defined for the font
Bchar tmlastchar; // The last character defined for the font
Bchar tmdefaultchar; // a replacement character that does not exist in the font
Bchar tmbreakchar; // character used for word splitting
Byte tmitalic; // non-zero when the font is Italic
Byte tmunderlined; // when the font is underline, It is not zero.
Byte tmstruckout; // non-zero when the font is deleted
Byte tmpitchandfamily; // font spacing (4 low bits) and Family (4 high bits)
Byte tmcharset; // Character Set of the font
} Textmetric;

Calculate the actual region occupied by characters in the cricheditctrl control:

1. Calculate the line height of each row by a single factor: line height lineheight

2. calculate the maximum font size of each line. charheight = font size * 96/72 PX character format font size = yheight/20. For mm_text ing, you can use the following formula to determine the height of a font that specifies the dot size: nheight =-muldiv (pointsize, getdevicecaps (HDC, logpixelsy), 72)

4. Calculate the baseline position of each row. Baseline = charheight * (tmdescent)/(tmheight) + (lineheight-charheight)/2. The baseline position is determined by the font height of the row, the largest font.

5. Calculate the upper-left coordinate getcharpos (char) of the area occupied by each line of characters. The position of the character is at the position of tmdescent/(tmheight) of the Character Font. So far, you can accurately determine the character height.

The specific calculation is quite easy and will not be repeated here.


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: 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.