Windows API one-day training (30) gettextmetrics Function

Source: Internet
Author: User
When making reports, you often need to arrange the output content in a wrong order to make the user look more comfortable. For example, you can use the font of the title to output the title in a smaller font. In this way, you need to know the height of each font so that the two lines of text output do not overlap, You need to calculate the height of each font. In this case, you need to use the gettextmetrics function to obtain the font height.

The gettextmetrics function declaration is as follows:

# Ifndef notextmetric

Wingdiapi bool winapi gettextmetricsa (_ in HDC, _ out lptextmetrica lptm );
Wingdiapi bool winapi gettextmetricsw (_ in HDC, _ out lptextmetricw lptm );
# Ifdef Unicode
# Define gettextmetrics gettextmetricsw
# Else
# Define gettextmetrics gettextmetricsa
# Endif //! Unicode

HDCIs the handle of the current device.
LptmIs to obtain the storage structure of the current font attribute. Its structure is defined as follows:
Typedef struct tagtextmetricw
{
Long tmheight;
Long tmascent;
Long tmdescent;
Long tminternalleading;
Long tmexternalleading;
Long tmavecharwidth;
Long tmmaxcharwidth;
Long tmweight;
Long tmoverhang;
Long tmdigitizedaspectx;
Long tmdigitizedaspecty;
Wchar tmfirstchar;
Wchar tmlastchar;
Wchar tmdefaultchar;
Wchar tmbreakchar;
Byte tmitalic;
Byte tmunderlined;
Byte tmstruckout;
Byte tmpitchandfamily;
Byte tmcharset;
} Textmetricw;

An example of calling this function is as follows:
#001 //
#002 // The output is displayed on the interface.
#003 //
#004 // Cai junsheng 2007/09/06 QQ: 9073204 Shenzhen
#005 //
#006 void ccaiwinmsg: ondraw (HDC)
#007 {
#008 //
#009 STD: wstring strshow (_ T ("Implementation of the C ++ window class, 2007-09-04 "));
#010
#011 // set the color of the output string.
#012 colorref crold = settextcolor (HDC, RGB (255, 0, 0 ));
#013
#014 rect rctext;
#015
#016 // set the new font.
#017 hgdiobj holdfont = SelectObject (HDC, getfont ());
#018
#019 // obtain the height of the current font.
#020 textmetric tmfont;
#021 if (gettextmetrics (HDC, & tmfont ))
#022 {
#023 rctext. Left = 10;
#024 rctext. Top = 40;
#025 rctext. Right = 300;
#026 rctext. Bottom = rctext. Top + tmfont. tmheight;
#027}
#028 else
#029 {
#030 rctext. Left = 10;
#031 rctext. Top = 40;
#032 rctext. Right = 300;
#033 rctext. Bottom = 80;
#034}
#035
#036 // create a black paint brush,
#037 hbrush = createsolidbrush (RGB (0, 0, 0 ));
#038
#039 // fill the color of the quadrilateral with a black brush.
#040 fillrect (HDC, & rctext, hbrush );
#041
#042 // Delete the painter.
#043 deleteobject (hbrush );
#044
#045 // set transparent background
#046 int noldmode = setbkmode (HDC, transparent );
#047
#048 // display the middle position of the string in the Quadrilateral.
#049 drawtext (HDC, strshow. c_str (), (INT) strshow. Length (), & rctext,
#050 dt_center | dt_vcenter | dt_singleline | dt_end_ellipsis );
#051
#052 // restore the original font.
#053 hgdiobj hfont = SelectObject (HDC, holdfont );
#054 deleteobject (hfont );
#055
#056 // restore the original mode.
#057 setbkmode (HDC, noldmode );
#058
#059 // restore the original color.
#060 settextcolor (HDC, crold );
#061}
#062

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.