MFC is centered on the character width

Source: Internet
Author: User

Gdiplus::font Font (_t ("Microsoft Jas Black"), (Gdiplus::real) 130); GDIPLUS::RECTF orgin (0.0f, 100.0f, nwidth, 200.0f); Gdiplus::stringformat Stringformat;stringformat. Setalignment (gdiplus::stringalignmentcenter);//stringformat. Setlinealignment (Gdiplus::stringalignmentcenter); Gdiplus::solidbrush SolidBrush (Gdiplus::color (255, 255, 0, 0)), graphics. DrawString (Strfailedcontent.c_str (),-1, &font, Orgin, &stringformat, &solidbrush);

function as follows

Float Gettextwidthfloor (const wstring& strText, const gdiplus::font& Font) {Gdiplus::stringformat StringFormat (Gdiplus::stringalignmentnear); Gdiplus::graphicspath Graphicspathobj; Gdiplus::fontfamily Fontfamily;font. Getfamily (&fontfamily); GDIPLUS::RECTF rcbound;graphicspathobj.addstring (Strtext.c_str (),-1, &fontfamily, font. GetStyle (), font. GetSize (), Gdiplus::P ointf (0, 0), &stringformat); Graphicspathobj.getbounds (&rcbound); return (float) ( Rcbound.width);} int gettextwidth (const wstring& strText, const gdiplus::font& Font) {//Calculate spaces width of text head//due t o Gdiplus do don't care about itint nheadspace = 0;int Nlen = Strtext.size (), for (int i=0; i<nlen; ++i) {if (0x20 = = Strte xt.at (i)) Nheadspace++;else{break;}} Calculate space widthstatic Float fspacewidth = 0.0f;if ((fspacewidth >= -0.00001f && fspacewidth <= 0.00 001f) && nheadspace > 0) {fspacewidth = Gettextwidthfloor (_t ("a B"), font)-Gettextwidthfloor (_t ("AB"), foNT);} float Fresult = Gettextwidthfloor (strText, font) + (float) nheadspace * fspacewidth;return (int) Fresult + 1;}

  

MFC is centered on the character width

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.