The size of the static text control on the interface is automatically adjusted according to the text content)

Source: Internet
Author: User

Function : Calculates the height and width of the specified body string.
Function prototype : Bool gettextextentpoint32 (HDC, lptstr lpstring, int cbstring, lpsize );
Parameter : HDC: Device environment handle.
Lpstring: pointer to the body string. This string does not end with \ 0 because cbstring specifies the length of the string.
Cbstring: the number of characters in the string.
Lpsize: pointer to the size structure. The size of the string in this structure is returned.
Return Value : If the function is successfully called, the returned value is a non-zero value. If the function fails to be called, the returned value is 0.
Windows NT: To obtain more error information, call the getlasterror function.
Secondary note : Gettextextentpoint32 uses the selected font to calculate the string size. The height and width calculated by logical unit do not consider cropping.
Because some devices compress characters, the sum of character ranges in a string may not be equal to the string range.
The calculated character width takes into account the character interval of the settextcharacterextra device.

Gettextextentpoint32 (HDC, sztext, lstrlen (sztext), & size );

Specifically, size. CX and size. Cy are the length and height of the. String (in pixels). In Windows CE, gettextextentpoint32 is the same as gettextextentpoint.

One step is required, that is, to obtain the font of the main window, then set the font of the DC, and then calculate it. This is correct. refer to the following Code
Cstatic * temp = (cstatic *) getdlgitem (idc_static1 );
Crect rect;
Csize size (0, 0 );
Temp-> getwindowrect (rect );
Screentoclient (& rect );
Cclientdc DC (temp );
Cfont * poldfont = Dc. SelectObject (this-> getfont ());
Cstring STR;
Temp-> getwindowtext (STR );
If (: gettextextentpoint32 (HDC) DC, STR, str. getlength (), & size ))
{
Rect. Right = rect. Left + size. CX;
Rect. Bottom = rect. Top + size. Cy;
}
Else

{
Temp-> setwindowtext ("gettextextentpoint32 fail to get the size of text! ");

}
Temp-> movewindow (rect );
DC. SelectObject (poldfont );

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.