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