Implementation of the Shadow text function in C ++ GDI +
Using the excellent graphic output function of GDI +, you can easily implement text effects. One of the Shadow texts is an example.
Many simple text effects simply output the text in different colors and positions once or multiple times, the shadow effect discussed in this article uses the reverse sample capability of GDI + to generate transparent shadows and semi-shadows. The method described hereFirst draw a text smaller than expected on the drawing plane, and then enlarge it.
Because the code comments are very detailed, go directly to the Code:
Authorization = NULL; // GDI + initialization void authorization: using () {using namespace Gdiplus; if (NULL = g_gdiPlusToken) {Gdiplus: GdiplusStartupInput gdiplusStartupInput; Gdiplus :: gdiplusStartup (& g_gdiPlusToken, & gdiplusStartupInput, NULL);} CRect ClientRC; CStringW strTxt ("19:20:30"); reverse (& ClientRC); RectF desRC (ClientRC. left, ClientRC. top, ClientRC. width (), ClientRC. height (); PointF txtPos (0, 0); FontFamily fontFamily (L "Times New Roman"); Gdiplus: Font font (& fontFamily, 100, FontStyleBold, UnitPixel ); graphics g (m_picBox.GetDC ()-> m_hDC); // 1.0 fill in the background color g. fillRectangle (& Gdiplus: SolidBrush (Color: LightSlateGray), desRC); // 2.0 create a small memory bitmap, set its length and width to 1/4 Bitmap bmp (ClientRC. width ()/4, ClientRC. height ()/4, & g); // 2.1 set the drawing mode to the anti-sample mode Graphics * pTempG = G Raphics: FromImage (& bmp); pTempG-> SetTextRenderingHint (TextRenderingHintAntiAlias); // 2.2 create a matrix so that the font is 1/4 of the original one, the Shadow distance also sets about 1/4 of the text Matrix mx (0.25f, 0, 0, 0.25f, 3, 3); pTempG-> SetTransform (& mx ); // 2.3 draw text on the in-place graph and use a transparent paint brush (such as 50% transparent) pTempG-> DrawString (strTxt,-1, & font, txtPos, NULL, & SolidBrush (Color (128, 0, 0, 0); // The 3.1 interpolation mode is a high quality double-cubic Interpolation method, which is very important, because the sides of the text are blurred by double interpolation, the shadow and shadow effect g appears. setInterpolationMode (Interpol AtionModeHighQualityBicubic); // 3.2 set the draw mode to the reverse sample mode to ensure the correct range g. setTextRenderingHint (TextRenderingHintAntiAlias); // 3.3 display the bitmap on the screen, which is 4 times larger in both directions. drawImage (& bmp, desRC, 0, 0, bmp. getWidth (), bmp. getHeight (), UnitPixel); // 3.4 draw the text on the drawing plane and use the white font g. drawString (strTxt,-1, & font, txtPos, NULL, & SolidBrush (Color: White); // 4.0 release the memory if (NULL! = PTempG) {delete pTempG; pTempG = NULL ;}}